問題描述
目標是提供一個按鈕/選擇 drop-down 或類似的方式來切換公共介面 1) 語言。
我在尋找什麼
-
外掛或主題程式碼…
-
… 或想法
-
使用 work-around 與
load_textdomain()將不勝感激,將翻譯留給.po /.mo 檔案 -
關於如何在不依賴於某些文字域功能的情況下將字串解析為介面的想法 (例如 ajax /plain php /ini,json,xml 檔案)
注意:
1) 不是用不同的語言出版。
2) 我不需要程式碼使實際的 drop-down /按鈕/任何。這只是關於為 UI 傳遞字串的程式碼/系統。
謝謝!
最佳解決方案
到目前為止,最好的 (最簡單的) 方式是使用區域設定過濾器 (get_locale()內) 。
首先設定一個快速功能,用於檢索在 locale 過濾器上使用的其他語言。
/**
* A function returns with returns the user's selectd locale, if stored.
*/
function wpse35622_get_new_locale($locale=false){
$new_locale = get_user_meta(get_current_user_id(), 'wpse_locale', true);
if($new_locale)
return $new_locale;
return $locale;
}
我把這作為一個全域性功能,可能會方便嗎?其餘的 plug-in 我已經包裝在一個類。
它建立一個可以選擇的語言的 drop-down 。
Drawbacks
-
搜尋
wp-content/languagesxx_xx.mo形式的檔案。如果它不是那種形式 (並不是所有的.mo 檔案),plug-in 將不會拾起它! -
下拉式清單中列出了可以選擇的找到的區域設定,但是這些列表顯示在 「xx_xx」 格式的區域設定中,而不是人性化的方式 – 任何想法?
-
$locale是否可以做任何其他語言?如果是這樣,仍然可以獲得相同的語言環境,但提供替代翻譯。這是一個很多 messier 雖然…
程式碼
class SH_Pick_Lang{
/**
* A unique name for this plug-in
*/
static $name ='sh_pick_lang';
/**
* Hook the functions
*/
public function __construct(){
if( isset($_POST[self::$name]) ){
add_action('locale',array($this,'update_user'));
}
add_filter( 'locale', 'wpse35622_get_new_locale',20 );
add_action( 'wp_before_admin_bar_render', array( $this, 'admin_bar') );
}
/**
* Update the user's option just in time! Only once mind...
*/
function update_user($locale){
$save_locale = $_POST[self::$name];
update_user_meta(get_current_user_id(), 'wpse_locale', $save_locale);
remove_filter(current_filter(),__FUNCTION__);
return $locale;
}
/**
* Add a really horrible looking dropdown menu that I'm sure Kaiser will make look fantastic.
*/
function admin_bar(){
global $wp_admin_bar;
$wp_admin_bar->add_menu( array(
'id' => 'shlangpick',
'title' => $this->wpse_language_dropown(),
) );
}
/**
* Generates a list of available locales.
* Searches the wp-content/languages folder for files of the form xx_xx.mo
*
* TODO Not all locales are of the form xx_xx.mo - we might miss some.
* TODO Better way of gettin gthe wp-content/languages folder without a constant?
*/
function wpse_language_dropown(){
$name = self::$name;
$locale = get_locale();
$langs = get_available_languages();
$langs[] = 'en_US';
//For the labels (format_code_lang)
require_once( ABSPATH . 'wp-admin/includes/ms.php');
$html = "<form method='post'> ";
$html .= "<select name='{$name}'>";
foreach($langs as $lang){
$label = format_code_lang( $lang );
$html .= "<option ".selected( $lang, $locale, false)." value='{$lang}'>{$label}</option>";
}
$html .= "</select>";
$html .= get_submit_button('Change Language','secondary', 'submit', true);
$html .= "</form >";
return $html;
}
} // END Class
//Initiate...
$sh_pick_lang = new SH_Pick_Lang();
次佳解決方案
如果您可以安排重新整理頁面,則可以選擇重新定義 WPLANG 常量。我正在兩個網站上進行多語言的內容,其中多重外掛無法觸發 UI 翻譯。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。