admin管理员组

文章数量:1126163

Sometimes you may have a language for the dashboard that differs from the site language, how to get the dashboard language. i tried to use get_locale and get_user_locale but both get the site language.

say the frontend is in English but the dashboard set to German.. i need to get the german

Sometimes you may have a language for the dashboard that differs from the site language, how to get the dashboard language. i tried to use get_locale and get_user_locale but both get the site language.

say the frontend is in English but the dashboard set to German.. i need to get the german

Share Improve this question edited Jun 5, 2019 at 15:25 Makiomar asked Jun 5, 2019 at 14:43 MakiomarMakiomar 1517 bronze badges 2
  • 1 get_user_locale() is a right function, and returns the language set for the dashboard for a given user. Are you sure that the user you are checking has set the language for dashboard? If it is not set, the function will return the language of the site (front-end). – nmr Commented Jun 5, 2019 at 16:33
  • I will check again, thank you – Makiomar Commented Jun 6, 2019 at 9:43
Add a comment  | 

2 Answers 2

Reset to default 0

get_locale() and get_user_locale() are functions to retrieve the locale that is already set. To change it, you want to "filter" the locale value.

For that, you want to use the locale filter hook. The following should do it:

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

    $locale = ( is_admin() ) ? "en_US" : "de_DE";
    return $locale;

}

This should set the admin locale to English, otherwise display German.

To answer the titular question, "How to get dashboard langauge not the website language?" the dashboard language is dependent on the currently logged-in user.

The site's language depends on what you've set in Settings > General > Site Language. In a fresh install of WordPress, this language is what is shown on the frontend and in the dashboard and you can get this language using the get_locale() function.

When you're editing your user profile in Users > Profile, you can choose your own language (or edit another user's profile to change their language). The default setting for new users is Site Default which is the same as the site's language.

To get the user's language, you can use the get_user_locale() function. The function also accepts a single parameter, a user ID or WP_User object if you want to get the language from a specific user. The default value of the parameter is 0 which attempts to get the currently logged in user's language, but for anonymous users, it defaults to using get_locale().

To answer your more nuanced question, "say the frontend is in English but the dashboard set to German.. i need to get the german" you can try something like this.

Let's say in Settings > General, you have the Administration Email Address set to [email protected] and Language set to English. Let's also say you have an Administrator user account with the same email and their language is set to German, you could write a function like this:

/**
 * Get Site Admin's Language
 *
 * @return string The locale of the site administrator, or current user on failure.
 */
function au_get_admin_language()
{
    $email = sanitize_email(get_bloginfo('admin_email'));
    if ($email) {
        $user = get_user_by('email', $email);
        if ($user !== false) {
            return get_user_locale($user);
        }
    }
    return get_user_locale();
}

So when you call au_get_admin_language(), it'd return German for you.

And if the administration user email address isn't the same as any user account, you can always hardcode it for your case, like $user = get_user_by('email','[email protected]'); or even $user = get_user_by('ID',123); if you know the user ID.

本文标签: translationHow to get dashboard langauge not the website language