admin管理员组文章数量:1130349
I wonder, is there a way to change the locale at runtime using WordPress ?
What I mean, is, I have create a custom URL in order to create an XML file with data for integration with another web application. The URL has also a language portion. Lets say this is my URL
where last portion of the URL is the english language. In that case what I like is the WordPress to generate an English XML. But how can I instruct the WordPress to change the locale in that point of the execution ?
Note that I have to change the locale because I use also system variables that are translated, such us __(), _e() and so on.
I wonder, is there a way to change the locale at runtime using WordPress ?
What I mean, is, I have create a custom URL in order to create an XML file with data for integration with another web application. The URL has also a language portion. Lets say this is my URL
http://example/custom/url/en
where last portion of the URL is the english language. In that case what I like is the WordPress to generate an English XML. But how can I instruct the WordPress to change the locale in that point of the execution ?
Note that I have to change the locale because I use also system variables that are translated, such us __(), _e() and so on.
Share Improve this question edited Nov 4, 2018 at 16:35 T.Todua 5,8909 gold badges52 silver badges81 bronze badges asked Apr 18, 2012 at 16:56 KodeFor.MeKodeFor.Me 3052 gold badges3 silver badges17 bronze badges 1- possible duplicate of Change language by clicking a button – kaiser Commented Apr 18, 2012 at 23:29
3 Answers
Reset to default 16I'm trying to do a similiar thing, and the experts on the wp-hackers mailing list (Otto, Nacin) told me this:
Don't try to change WPLANG, you can't change a define'd constant. Instead, change the global $locale, or put a filter on 'locale'.
So the best solution is to apply a filter on the 'locale' global variable. The only way to do that is by creating a custom plugin. If you put the following piece of code into your functions.php file, it won't work properly because it will run too late in the WP loading sequence.
Your plugin could look like this (I'm just reusing the URI testing part from OneTrickPony, you can replace it with another conditional testing method):
<?php
/*
Plugin Name: Change locale at runtime
Plugin URI: http://wordpress.stackexchange/questions/49451/change-locale-at-runtime
*/
function wpsx_redefine_locale($locale) {
// run your tests on the URI
$lang = explode('/', $_SERVER['REQUEST_URI']);
// here change to english if requested
if(array_pop($lang) === 'en'){
$locale = 'en_US';
// otherwise stick to your default language
}else{
$locale = 'gr_GR';
}
return $locale;
}
add_filter('locale','wpsx_redefine_locale',10);
?>
I hope this can help anyone!
A few more warnings (quoting Andrew Nacin), regarding the cost in terms of performance when switching the locale:
It is possible to "switch out" a locale after the default locale is loaded, but I would advise against that as it is inefficient, unless your default language is English, in which case it is not so bad.
Default textdomain files are loaded after
plugins_loadedandsetup_theme, but before the theme is loaded and beforeafter_setup_themefires. Loading English then re-loading the textdomain into German on theinithook would be fine, performance-wise, as English has no mo files. But loading Spanish by default then switching to German would not.
See http://codex.wordpress/Plugin_API/Action_Reference for useful info about the loading sequence.
I don't know if it's possible to do this within a plugin, because of the constants WP requires defined before a certain point, but check for the requested language in wp-config.php and define the necessary constant:
// split URI
$lang = explode('/', $_SERVER['REQUEST_URI']);
// here change to english if requested
if(array_pop($lang) === 'en'){
define('WPLANG', 'en_US');
// otherwise stick to your default language
}else{
define('WPLANG', 'gr_GR');
}
At first, ensure that your desired locale is in a list (under "has a language pack"). If confirmed, then use such code:
//set desired locale
add_filter( 'locale', function($locale) {
if ( !is_admin() )
$locale = "zh_TW";
return $locale;
});
If you haven't chosen that language in WP dashboard, then you might need to use installer hook programatically:
//check if it's not install, then install
add_action('wp', 'my_locale_implemention');
function my_locale_implemention()
{
$my_locale = get_locale(); //get_user_locale
if ( !empty($my_locale) && !in_array( $my_locale, get_available_languages() ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
$language = wp_download_language_pack( $my_locale );
if ( $language ) {
if(empty($_POST)) header("location: ".$_SERVER['REQUEST_URI']); exit; //reload page
}
}
}
I wonder, is there a way to change the locale at runtime using WordPress ?
What I mean, is, I have create a custom URL in order to create an XML file with data for integration with another web application. The URL has also a language portion. Lets say this is my URL
where last portion of the URL is the english language. In that case what I like is the WordPress to generate an English XML. But how can I instruct the WordPress to change the locale in that point of the execution ?
Note that I have to change the locale because I use also system variables that are translated, such us __(), _e() and so on.
I wonder, is there a way to change the locale at runtime using WordPress ?
What I mean, is, I have create a custom URL in order to create an XML file with data for integration with another web application. The URL has also a language portion. Lets say this is my URL
http://example/custom/url/en
where last portion of the URL is the english language. In that case what I like is the WordPress to generate an English XML. But how can I instruct the WordPress to change the locale in that point of the execution ?
Note that I have to change the locale because I use also system variables that are translated, such us __(), _e() and so on.
Share Improve this question edited Nov 4, 2018 at 16:35 T.Todua 5,8909 gold badges52 silver badges81 bronze badges asked Apr 18, 2012 at 16:56 KodeFor.MeKodeFor.Me 3052 gold badges3 silver badges17 bronze badges 1- possible duplicate of Change language by clicking a button – kaiser Commented Apr 18, 2012 at 23:29
3 Answers
Reset to default 16I'm trying to do a similiar thing, and the experts on the wp-hackers mailing list (Otto, Nacin) told me this:
Don't try to change WPLANG, you can't change a define'd constant. Instead, change the global $locale, or put a filter on 'locale'.
So the best solution is to apply a filter on the 'locale' global variable. The only way to do that is by creating a custom plugin. If you put the following piece of code into your functions.php file, it won't work properly because it will run too late in the WP loading sequence.
Your plugin could look like this (I'm just reusing the URI testing part from OneTrickPony, you can replace it with another conditional testing method):
<?php
/*
Plugin Name: Change locale at runtime
Plugin URI: http://wordpress.stackexchange/questions/49451/change-locale-at-runtime
*/
function wpsx_redefine_locale($locale) {
// run your tests on the URI
$lang = explode('/', $_SERVER['REQUEST_URI']);
// here change to english if requested
if(array_pop($lang) === 'en'){
$locale = 'en_US';
// otherwise stick to your default language
}else{
$locale = 'gr_GR';
}
return $locale;
}
add_filter('locale','wpsx_redefine_locale',10);
?>
I hope this can help anyone!
A few more warnings (quoting Andrew Nacin), regarding the cost in terms of performance when switching the locale:
It is possible to "switch out" a locale after the default locale is loaded, but I would advise against that as it is inefficient, unless your default language is English, in which case it is not so bad.
Default textdomain files are loaded after
plugins_loadedandsetup_theme, but before the theme is loaded and beforeafter_setup_themefires. Loading English then re-loading the textdomain into German on theinithook would be fine, performance-wise, as English has no mo files. But loading Spanish by default then switching to German would not.
See http://codex.wordpress/Plugin_API/Action_Reference for useful info about the loading sequence.
I don't know if it's possible to do this within a plugin, because of the constants WP requires defined before a certain point, but check for the requested language in wp-config.php and define the necessary constant:
// split URI
$lang = explode('/', $_SERVER['REQUEST_URI']);
// here change to english if requested
if(array_pop($lang) === 'en'){
define('WPLANG', 'en_US');
// otherwise stick to your default language
}else{
define('WPLANG', 'gr_GR');
}
At first, ensure that your desired locale is in a list (under "has a language pack"). If confirmed, then use such code:
//set desired locale
add_filter( 'locale', function($locale) {
if ( !is_admin() )
$locale = "zh_TW";
return $locale;
});
If you haven't chosen that language in WP dashboard, then you might need to use installer hook programatically:
//check if it's not install, then install
add_action('wp', 'my_locale_implemention');
function my_locale_implemention()
{
$my_locale = get_locale(); //get_user_locale
if ( !empty($my_locale) && !in_array( $my_locale, get_available_languages() ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
$language = wp_download_language_pack( $my_locale );
if ( $language ) {
if(empty($_POST)) header("location: ".$_SERVER['REQUEST_URI']); exit; //reload page
}
}
}
本文标签: localizationChange locale manually at runtime
版权声明:本文标题:localization - Change locale manually at runtime? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749206304a2332624.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论