1

In a custom component I'm making, I need the user, in his private area, to choose the default language for the web.

It is a custom form, and I already render the language selector, but I don´t know how to change the user's parameters programmatically.

I need the user to choose the language and when saving the web has to change language if a different one is selected.

Any help is very appreciated.

Thanks

  • Do you want to change language of your component or complete site. – Pratyush Sep 18 '18 at 19:22
  • I want to change a user default site language – Piero Marsilio Sep 18 '18 at 20:51
1

Run this after saving other data in your model:

$user = JFactory::getUser();

if (!$user->guest)
{
    $user->setParam('language', $language);
    $user->save();
}

Where $language is the language from form data.

You also have to bind the data to form so that selected option is shown as selected.

  • Thanks a lot. It worked. Now once I change the user default language I have to change the site language. I´ll try to do it :) – Piero Marsilio Sep 23 '18 at 9:18
2

If you want to change the language of the site, you can store the language in the user param language and you will have to load it on your condition on plugin events such as onAfterRoute or onBeforeCompileHead that suits you better, as loading language should be done before the content is prepared.

As plugin event such as this is triggered at both front and admin end so you will need to place your code in proper conditions.

Set desired language using :

$lang = JFactory::getLanguage();
$lang->setLanguage("hi-IN");

But place this code in proper condition so only the intended user's language is updated.

Hope this helps, if anyone has a better way please post an answer.

  • Hi, thank, this "you can store the language in the user param language", should be easy to do but it is precisely what I haven't been able to accomplish. I am not that worried now about changing the sile language but to store the user's preference in his/her profile. – Piero Marsilio Sep 19 '18 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.