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 |2 Answers
Reset to default 0get_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
版权声明:本文标题:translation - How to get dashboard langauge not the website language? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736679141a1947335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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