theme development - How to set different localization file for different users?

admin2025-06-06  11

I am trying to load localization file depends on users settings. Is this way is correct or there are better ways to implement locale changing depends on user settings?

    <?php
           add_filter( 'locale', 'theme_localized' );

            function theme_localized( )
            {
                if(is_user_logged_in()) {
                    $locale = get_user_locale();
                     return $locale;
                }

            }
?>

This code is working but I am not sure that this is the conventional way of translating website.

I am trying to load localization file depends on users settings. Is this way is correct or there are better ways to implement locale changing depends on user settings?

    <?php
           add_filter( 'locale', 'theme_localized' );

            function theme_localized( )
            {
                if(is_user_logged_in()) {
                    $locale = get_user_locale();
                     return $locale;
                }

            }
?>

This code is working but I am not sure that this is the conventional way of translating website.

Share Improve this question asked Nov 5, 2018 at 18:36 Nikolai MaksimovNikolai Maksimov 331 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

By default WordPress only loads translations according to the user's language when they're viewing admin pages.

You can see that in the code for the load_theme_textdomain function:

$locale = apply_filters( 'theme_locale',
    is_admin() ? get_user_locale() : get_locale(),
//  ^^^^^^^^
$domain );

So your code is fine if you want to override that behaviour for the front end of your site.

Is it conventional? Well, this is what filters are for. However, I'd say this isn't normally done unless the language of the actual content is also changing.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749199941a317201.html

最新回复(0)