d'Hinnisdaël Posted January 12, 2020 Posted January 12, 2020 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?
dragan Posted January 12, 2020 Posted January 12, 2020 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.
d'Hinnisdaël Posted January 12, 2020 Author Posted January 12, 2020 @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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now