最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far