【发布时间】:2011-11-23 01:33:54
【问题描述】:
我有一个使用 Wordpress 的基于用户的网站,他们可以从他们的个人资料设置中选择语言、此信息和其他设置是为 user_meta 中的每个用户设置的。
我知道如何翻译,但是有没有办法以编程方式设置主题语言?
谢谢!
编辑:请不要使用插件,我需要尽可能简单地做到这一点。
【问题讨论】:
标签: wordpress wordpress-theming
我有一个使用 Wordpress 的基于用户的网站,他们可以从他们的个人资料设置中选择语言、此信息和其他设置是为 user_meta 中的每个用户设置的。
我知道如何翻译,但是有没有办法以编程方式设置主题语言?
谢谢!
编辑:请不要使用插件,我需要尽可能简单地做到这一点。
【问题讨论】:
标签: wordpress wordpress-theming
从 WP 4.7 开始,您可以使用:
switch_to_locale('en_US');
参考:https://developer.wordpress.org/reference/functions/switch_to_locale/
【讨论】:
我找到了一个不同的解决方案:
// 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');
}
通过:http://codex.wordpress.org/Function_Reference/load_theme_textdomain
【讨论】:
unload_textdomain('my-plugin-txt-domain');load_plugin_textdomain('my-plugin-txt-domain', __DIR__ . '/../../languages');
我想出了以下解决方案,因为我需要在同一请求的范围内从不同语言的插件生成发票:
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
【讨论】:
我猜您正在寻找 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');
从大脑到键盘的代码,未经测试。你应该做更多的验证等等。
【讨论】:
对我来说,只有这两种解决方案一起工作。
switch_to_locale($locale);
load_textdomain('example_domain', $mo_file_full_path);
【讨论】:
我遇到了类似的问题,然后这样解决了:
在我的例子中,我想使用用户语言环境检索语言环境:$userLocale = get_user_locale($userObject->ID);
我创建了一个自定义函数来加载具有动态语言环境的正确主题文本域。跟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's locale.
*
* @since 3.0.0
*
* @param string $locale The theme's 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);
}
【讨论】:
实际上 switch_to_locale 对我不起作用,在调试过程中我发现 load_textdomain make out of memory 致命错误:/
【讨论】:
A 有同样的问题,我是这样解决的:
第一步,您必须创建一个带有文本域的 .mo。您可以使用本地翻译插件:https://br.wordpress.org/plugins/loco-translate/
加载您的文本域链接 .mo 文件:
load_textdomain('your_text_domain', '/your/file.mo');
通过钩子更改 wordpress 位置。创建一个返回所需位置的函数。曾经使用过 Wordpress 语言环境 ID,您可以在此处查看所有语言环境 ID:https://wpastra.com/docs/complete-list-wordpress-locale-codes/
让我们编写函数:
function wpsx_redefine_locale($locale){
return 'ja';
}
add_filter('locale','wpsx_redefine_locale',10);
您可以根据用户位置进行更改。这样,wordpress 设置的语言与用户在其控制面板中设置的语言相同:
$user_locale = get_user_locale();
function wpsx_redefine_locale($locale){
global $user_locale;
return $user_locale;
}
add_filter('locale','wpsx_redefine_locale',10);
Obs:我的 wordpress 版本是 5.7。这种方式在其他版本中是行不通的。好看!
【讨论】: