Simple Multilanguage Switcher

时间:2011-08-23 作者:Reddy \'Rk\'

首先,我已经安装了以印尼语为默认语言的不同语言的wp,我有计划,想用其他语言翻译,如何简单地转换开关,但保持印尼语为默认语言。具有采购订单文件

谢谢你的帮助。。。

1 个回复
SO网友:moraleida

假设您讨论的是前端,并且字符串已正确国际化,那么只需插入load_theme_textdomain() 函数告诉wordpress您的位置。采购订单文件。

工具箱主题就是这样做的:

/**
 * Make theme available for translation
 * Translations can be filed in the /languages/ directory
 * If you\'re building a theme based on martins, use a find and replace
 * to change \'martins\' to the name of your theme in all the template files
 */
load_theme_textdomain( \'martins\', TEMPLATEPATH . \'/languages\' );

$locale = get_locale();
$locale_file = TEMPLATEPATH . "/languages/$locale.php";
if ( is_readable( $locale_file ) )
    require_once( $locale_file );

UPDATE:

我曾经尝试过通过更改用户的语言环境来自动实现这一点,从$\\u服务器superglobal获取用户语言。

我想到了这个,但它还不起作用。也许你可以从中学习。

WARNING: THIS IS UNTESTED AND MIGHT BEHAVE WEIRDLY OR EVEN BREAK YOUR SITE, BE CAREFUL

function rm_get_locale($lang) {
    global $locale;
    // This gets the users\' primary browser settings for acceptable languages
    // and transforms the string so it looks like en_US or pt_BR rather than
    // en-us and pt-br. It takes only the first value returned, no all of them.
    $langcode = explode(";", $_SERVER[\'HTTP_ACCEPT_LANGUAGE\']);
    $langcode = explode(",", $langcode[\'0\']);
    $langcode = $langcode[\'0\'];
    $langcode = preg_split(\'/-/\', $langcode);
    $upper = strtoupper($langcode[1]);
    $lower = $langcode[0];

    // now we get the native wp locale and the parsed user locale
    $wplocale = get_locale();
    $userlocale = implode(\'_\',array($lower,$upper));

    // compare them and apply the user\'s locale if they don\'t match.
    if ($userlocale != $wplocale) {
        return $userlocale;
    } else {
        return $lang;
    }


}
add_filter(\'locale\',\'rm_get_locale\');

结束

相关推荐