以编程方式设置Wordpress语言?

Vla*_*d.P 7 wordpress wordpress-theming

我有一个基于用户的WordPress网站,从他们的个人资料设置,他们可以选择语言,这个信息和其他设置是为user_meta中的每个用户设置的.

我知道如何翻译但是,有没有办法以编程方式设置主题语言?

谢谢!

编辑:请不要插件,我需要尽可能简单.

kri*_*ris 13

从 WP 4.7 开始,您可以使用:

switch_to_locale('en_US');
Run Code Online (Sandbox Code Playgroud)

参考:https : //developer.wordpress.org/reference/functions/switch_to_locale/


Vla*_*d.P 8

我找到了一个不同的解决方案:

// Set user selected language by loading the lang.mo file
if ( is_user_logged_in() ) {

    // add local filter
    add_filter('locale', 'language');

    function language($locale) {
        /* Note: user_meta and user_info are two functions made by me,
           user_info will grab the current user ID and use it for
           grabbing user_meta */

        // grab user_meta "lang" value
        $lang = user_meta(user_info('ID', false), 'lang', false); 

        // if user_meta lang is not empty
        if ( !empty($lang) ) {
           $locale = $lang; /* set locale to lang */
        }

        return $locale; 
    }

    // load textdomain and .mo file if "lang" is set
    load_theme_textdomain('theme-domain', TEMPLATEPATH . '/lang');

}
Run Code Online (Sandbox Code Playgroud)

通过: http: //codex.wordpress.org/Function_Reference/load_theme_textdomain