Jump to content

Recommended Posts

Posted

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?

Posted
  • 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.

 

Posted

@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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...