您可以臨時更改當前語言以檢索翻譯後的字符串。喜歡的東西:
// Backup the current language
$current_lang = $sitepress->get_current_language(); // Say it's "en"
// Switch to another language. E.g. $desired_lang = "nl";
$sitepress->switch_lang($desired_lang);
// Get your translated string...
_e('My string here', 'text_domain');
// Back to the original language to not interfere
$sitepress->switch_lang($current_lang);
我已經在頁面模板測試這個(比如index.php
)和它的作品...然後我試圖建立一個函數來完成這項工作。喜歡的東西:
// Put this in your functions.php
function get_all_translations($string, $languages) {
global $sitepress;
if (empty($languages)) {
$languages = array_keys(
icl_get_languages('skip_missing=0&orderby=code&order=asc')
);
}
$current_lang = $sitepress->get_current_language();
$translations = [];
foreach ($languages as $lang) {
$sitepress->switch_lang($lang, true);
$translations[$lang] = __($string, 'text_domain');
}
$sitepress->switch_lang($current_lang);
return $translations;
}
和:
// This on index.php:
var_dump(get_all_translations('My string here'));
var_dump(get_all_translations('My string here', ['nl', 'fr']));
但它不工作,我想不通的原因......我希望這有助於反正。
謝謝。這有幫助。我只用3種語言切換語言,將字符串存儲在變量中並使用它們。 –