最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

localization - Multisite: How can I have the admin bar (toolbar) use the language of the user instead of the language of the sub

programmeradmin2浏览0评论

By default in a network install, when viewing a sub-site, the sub-site language is used for the admin bar (toolbar).

How can I have the admin bar (toolbar) always use the language of the user?

By default in a network install, when viewing a sub-site, the sub-site language is used for the admin bar (toolbar).

How can I have the admin bar (toolbar) always use the language of the user?

Share Improve this question asked Jun 17, 2021 at 13:50 henrywrighthenrywright 3,1076 gold badges39 silver badges65 bronze badges 6
  • Some quick experimenting shows me that the language of the Admin Bar changes when the user's Language setting (on their Profile page) is set to use a specific language (instead of the Site Default setting). At least, all the items added by Core seemed to change for me. – Pat J Commented Jun 17, 2021 at 14:53
  • 1 @PatJ are you sure? There is an unresolved ticket open describing the problem core.trac.wordpress.org/ticket/38643 – henrywright Commented Jun 17, 2021 at 15:13
  • All I can say for sure is that, once I installed a 2nd language in my local WP installation, the Admin Bar seemed to respect the user's language settings. I didn't dig deep into it, and I don't have a lot of experience with translations — all the sites I deal with are English only. – Pat J Commented Jun 17, 2021 at 16:08
  • @PatJ thanks. Is it possible your site locale and the user locale are both set to English? That may explain why the user's setting appears to be used. For example English – henrywright Commented Jun 17, 2021 at 17:36
  • No, the 2nd language I installed was Canadian French, since I wanted something I could still read. When I switched the user's language (in the edit profile) screen to French, the Admin Bar core items were rendered in French, whether the site's language settings were French or English. – Pat J Commented Jun 17, 2021 at 17:47
 |  Show 1 more comment

1 Answer 1

Reset to default 1

Until there is a fix for it in Wordpress Core you can use this 'dirty' way:

Switch to user locale before admin bar items are processed:

add_filter( 'admin_bar_menu', 'ws390690_admin_bar_in_user_locale', 10, 1 );
function ws390690_admin_bar_in_user_locale($wp_admin_bar){
    switch_to_locale( get_user_locale() );
    return $wp_admin_bar;
}

Switch back to site locale after admin bar has been rendered:

add_filter( 'wp_after_admin_bar_render', 'ws390690_admin_bar_in_user_locale_back', 10, 2 );
function ws390690_admin_bar_in_user_locale_back(){
    restore_previous_locale();
}

As mentioned by @henrywright, its also in works here https://core.trac.wordpress.org/ticket/38643

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论