I have a user website with Wordpress, and from their profile settings they can choose the language, this information and other settings are set for each user in user_meta.
I know how to translate, but is there a way to set the theme language programmatically?
Thank!
Edit: There are no plugins, please, I need to make this as simple as possible.
I found another solution:
// 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'); }
Via: http://codex.wordpress.org/Function_Reference/load_theme_textdomain
I think you are looking for a filter override_load_textdomainthat is called only at the beginning of load_textdomaina function call.
override_load_textdomain
load_textdomain
- :
function my_load_textdomain ($retval, $domain, $mofile) { if ($domain != 'theme_domain') return false; $user = get_currentuserinfo() $user_lang = get_user_lang($user); if ($new_mofile = get_my_mofile($user_lang)) { load_textdomain('theme_domain', $new_mofile); return true; } return false; } add_filter('override_load_textdomain', 'my_load_textdomain');
, . .
, - :
global $locale; $locale = 'en_CA'; load_plugin_textdomain('invoice', false, 'my-plugin/languages/'); generateInvoice(); // produce the English localized invoice $locale = 'fr_CA'; load_plugin_textdomain('invoice', false, 'my-plugin/languages/'); generateInvoice(); // produce the French localized invoice
:
, : $userLocale = get_user_locale($userObject->ID);
$userLocale = get_user_locale($userObject->ID);
theme_textdomain . , WP, locale:
/** * Loads text domain by custom locale * Based on a WP function, only change is the custom $locale * parameter so we can get translated strings on demand during runtime */ function load_theme_textdomain_custom_locale($domain, $path = false, $locale) { /** * Filter a theme locale. * * @since 3.0.0 * * @param string $locale The theme current locale. * @param string $domain Text domain. Unique identifier for retrieving translated strings. */ $locale = apply_filters('theme_locale', $locale, $domain); if (!$path) { $path = get_template_directory(); } // Load the textdomain according to the theme $mofile = "{$path}/{$locale}.mo"; if ($loaded = load_textdomain($domain, $mofile)) { return $loaded; } // Otherwise, load from the languages directory $mofile = WP_LANG_DIR . "/themes/{$domain}-{$locale}.mo"; return load_textdomain($domain, $mofile); }
: http://queryposts.com/function/load_theme_textdomain/
WP 4.7 :
switch_to_locale('en_US');
: https://developer.wordpress.org/reference/functions/switch_to_locale/