Jump to content
d'Hinnisdaël

Hook after each language change

Recommended Posts

I need to run code each time the user's language is changed. What's the best way to go about that?

Reading the source code, I found documentation on a hook called Languages::languageChanged, but it doesn't exist and is never called. Maybe @ryan knows if the hook does indeed exist and I'm just doing it wrong? Ideally, this would work:

wire()->addHookAfter('Languages::languageChanged', function (HookEvent $event) {
    $newLanguage = $event->arguments(1);
    Carbon::setLocale($newLanguage->locale)
});

Maybe there's a better way of doing that?

Share this post


Link to post
Share on other sites
  • Do you need this in the frontend or backend?
  • Do you want to detect when a logged-in user switches language in the backend?
  • Do you want to detect when a user in the frontend switches from page language A to page language version B?

You could try and keep track of $user->language->title changes or something similar, with cookies/session.

The code instance you linked to seems indeed to be the only instance in the entire PW core. I don't think it does anything useful right now as is, nor do I think it is really hookable.

 

Share this post


Link to post
Share on other sites

@dragan I want to detect whenever $user->language is changed during runtime, i.e. any changes after startup, including all related API calls like $languages->setLanguage(). I need to change the locale setting of the library I'm using for displaying dates in the frontend and would like to cover the scenario of dynamically setting the language for output purposes.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...