在wordpress中获取字符串的翻译

在wordpress中获取字符串的翻译,wordpress,Wordpress,我在wordpress中有一个字符串(英文),我知道它在.po和.mo文件中有翻译,当浏览该语言的网站时,它会在前端正确显示 但是,我需要在PHP脚本中获得翻译。不是.po/.mo文件,只是翻译的字符串。注意:我不是说用当前语言显示文本 我正在寻找一个函数,我可以提供原始字符串和语言代码,并检索翻译。我需要在一个脚本/请求中对所有可用语言执行此操作,因此一次检索所有这些数据也是可以接受的 我尝试过这样的测试($languages是一个lang代码字符串数组) foreach($languages

我在wordpress中有一个字符串(英文),我知道它在.po和.mo文件中有翻译,当浏览该语言的网站时,它会在前端正确显示

但是,我需要在PHP脚本中获得翻译。不是.po/.mo文件,只是翻译的字符串。注意:我不是说用当前语言显示文本

我正在寻找一个函数,我可以提供原始字符串和语言代码,并检索翻译。我需要在一个脚本/请求中对所有可用语言执行此操作,因此一次检索所有这些数据也是可以接受的

我尝试过这样的测试($languages是一个lang代码字符串数组)

foreach($languages作为$language){
应用过滤器('locale',$language);
$test=translate($entry_id,'root');
打印(测试);
打印“
”; }
然而,这似乎只是给了我英文字符串(en或任何变体都不是$languages中的语言)

谁能给我指出正确的方向吗


我希望我可以自己处理.po文件,但这似乎没有必要,因为wordpress确实能够使用模板中常用的getText函数来提取当前语言中的翻译,所以我认为我所做的一定有一条较短的路径。

您应该能够使用
全局$locale
来“设置”一个值(实际上,您可以将其设置为任意值,然后应用过滤器)

然后可以将要转换的字符串传递到函数中。以获取转换后的值

add_action( 'init', 'init_locale' );
function init_locale(){
    // initialise the global variable on init, set it to whatever you want
    global $locale;
    $locale = "en_US";
}

// use a filter to set the locale
add_filter( 'locale', 'set_locale' );
function set_locale(){
    // whatever logic you want to set the country
    if ( isset( $_REQUEST['foo'] ) && $_REQUEST['foo'] == 'spanish' ){
        return 'es_ES';
    }
    // or use the global
    global $locale;
    return $locale;
}

// once everything is loaded you can get the translated text. 
add_function( 'wp_loaded', 'translated_text' );
function translated_text(){
    // you might have used a filter or defined the global somewhere and can use...
    $spanish = __( 'text to translate' );

    // or you could define the global here and then get the text.
    global $locale;
    $locale = 'fr_FR';
    $french = __( 'text to translate' );
}
如果您想要一个单独的函数来执行此操作,则应使用以下方法:

function get_translated_text( $text, $domain = 'default', $the_locale = 'en_US' ){
    // get the global
    global $locale;
    // save the current value
    $old_locale = $locale;
    // override it with our desired locale
    $locale = $the_locale;
    // get the translated text (note that the 'locale' filter will be applied)
    $translated = __( $text, $domain );
    // reset the locale
    $locale = $old_locale;
    // return the translated text
    return $translated;
}

最后,我只是处理.mo文件来获得翻译,但这比我想象的要容易

$translations = array();
$languages = get_available_languages(ABSPATH . '../wp-content/themes/MY-THEME/lang');
foreach ($languages as $language) {
    $mo = new MO();
    if ($mo->import_from_file(ABSPATH . '../wp-content/themes/MY-THEME/lang/' . $language . '.mo')) {
        $translations[$language] = $mo->entries;
    }
}
然后翻译在
$translations[$language][$entry\u id]->translations[0]

其中$entry\u id是英文字符串。

谢谢,这看起来很有希望,但我到目前为止运气不好。我继续得到英文字符串。我猜这段代码只能在特定的上下文或环境中工作,我不知道如何在PHP脚本中设置。我可以访问wordpress函数,但需要在中进行一些调试wp includes/l10n.php使用显示的技术更改区域设置似乎没有达到预期效果“但是l10n函数总是试图在en_US.mo文件中找到翻译,这当然是不存在的。伙计,我会把你的答案标记为最佳答案,因为在这个网站上尝试回答我的问题是不寻常的,但我也会发布我使用的解决方案。我不知道为什么会被否决。我是最初的海报,这是有效的解决方案。我保证。
$translations = array();
$languages = get_available_languages(ABSPATH . '../wp-content/themes/MY-THEME/lang');
foreach ($languages as $language) {
    $mo = new MO();
    if ($mo->import_from_file(ABSPATH . '../wp-content/themes/MY-THEME/lang/' . $language . '.mo')) {
        $translations[$language] = $mo->entries;
    }
}