1

I want to change the language of my WordPress installation based on a choice the user can make in his profile (dropdown menu).

As I want to change frontend (prepared .mo files for the theme) and backend language I thought I could change the $locale variable and it would work out.

So I came up with the following code in my functions.php but that won´t do the job:

function change_lang() {
   get_currentuserinfo();
   $lang = get_user_meta($current_user->ID, 'user_lang');
   $locale = $lang[0];
   return $locale;
}
add_filter('locale', 'change_lang');

When I return the value of $locale, WP returns a simple NULL, nothing more. When I release the hook, the defined value from WP_LANG() is returned.

So, I am obviously wrong somewhere, but I can´t find where. Any hints?

Besides: I read this for inspiration and code, but I don´t want to have it as a plugin if possible.

Thanks in advance!

Edit: Fixed my code, but still no language change happens.

3 Answers 3

1

Your use of $current_user is a little wrong, you should include the global $current_user or assign it to a variable, or shorter and cleaner just get_current_user_id():

add_filter('locale', 'change_lang');
function change_lang( $locale ) {

   if( $lang = get_user_meta( get_current_user_id(), 'user_lang', true) ) {
       return $lang;
   }

   return $locale;

}
6
  • Thank you, I fixed my code. But still no language change at all. Any more ideas?
    – flomei
    Jan 19, 2015 at 12:45
  • If you have implemented the user meta field "user_lang" and teh user has set the language, this code should work. Have you implemented that field?
    – cybmeta
    Jan 19, 2015 at 14:08
  • I have, see this pastebin for my code: pastebin.com/v4pmV0xu Also, showing the selected language in the frontend works, so updating etc. is doing fine.
    – flomei
    Jan 26, 2015 at 11:48
  • 1
    @kaiser coded a user language switcher, may he can say to us why this code is not working. I've been trying to find what can be wrong with no success.
    – cybmeta
    Feb 6, 2015 at 17:13
  • Thank you very much for pointing that out. I just had a look at the code, but I´m somehow just missing, where he sets the new locale.
    – flomei
    Feb 9, 2015 at 7:52
0

I had a similar problem in my plugin and nothing in here worked.

After days looking into it. I've realized that it is impossible to get the current user inside the filter 'locale'. Not even calling the global $user_locale.

Apparently the hook 'locale' is triggered before init, or anythig else.

After all i found out that I was loading my plugin text domain before setting the locale. So, calling it right after switch_to_locale() fixed the problem.

This worked for me:

function set_user_locale(){

   switch_to_locale(get_user_locale());

   load_plugin_textdomain( 'plugin-texdomain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); 

}
add_filter('init', 'set_user_locale');
-1

Did you mean this option ?

enter image description here

If yes, then...

$user_locale = get_user_locale();
function change_userlang_by_userlocale($locale){
    global $user_locale;
    return $user_locale ? $user_locale : $locale;
}
add_filter('locale','change_userlang_by_userlocale');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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