2011-11-23 91 views
5

我有一個基於用戶的網站與Wordpress和他們的個人資料設置,他們可以選擇語言,這個信息和其他設置是爲user_meta中的每個用戶設置的。以編程方式設置Wordpress語言?

我知道如何翻譯,但是,有沒有辦法通過編程設置主題語言?

謝謝!

編輯:請插件,我需要儘可能簡單地做到這一點。

回答

2

我發現了一個不同的解決方案:

// Set user selected language by loading the lang.mo file 
if (is_user_logged_in()) { 

    // add local filter 
    add_filter('locale', 'language'); 

    function language($locale) { 
     /* Note: user_meta and user_info are two functions made by me, 
      user_info will grab the current user ID and use it for 
      grabbing user_meta */ 

     // grab user_meta "lang" value 
     $lang = user_meta(user_info('ID', false), 'lang', false); 

     // if user_meta lang is not empty 
     if (!empty($lang)) { 
      $locale = $lang; /* set locale to lang */ 
     } 

     return $locale; 
    } 

    // load textdomain and .mo file if "lang" is set 
    load_theme_textdomain('theme-domain', TEMPLATEPATH . '/lang'); 

} 

途經:http://codex.wordpress.org/Function_Reference/load_theme_textdomain

2

我想你正在尋找override_load_textdomain過濾器,只是在load_textdomain函數調用的開始處調用過濾器。

這將是這樣的:從大腦

function my_load_textdomain ($retval, $domain, $mofile) { 

    if ($domain != 'theme_domain') 
     return false; 

    $user = get_currentuserinfo() 
    $user_lang = get_user_lang($user); 

    if ($new_mofile = get_my_mofile($user_lang)) { 
     load_textdomain('theme_domain', $new_mofile); 
     return true; 
    } 

    return false; 
} 
add_filter('override_load_textdomain', 'my_load_textdomain'); 

守則鍵盤,未經測試。你應該做更多的驗證等等。

+0

謝謝你,我會給它一個測試,讓你知道。 –

+0

我得到「警告:my_load_textdomain()在[...]中缺少參數2」,代碼與您發佈的代碼相同。 –

+0

請注意,我發佈的代碼將無法正常工作。這僅僅是適用於您的網站環境的參考。我只是糾正了函數調用來接受過濾器需要的參數。 – vmassuchetto

2

我想出了以下解決方案,我需要從在同一請求的範圍不同語言的插件生成發票:

global $locale; 

    $locale = 'en_CA'; 
    load_plugin_textdomain('invoice', false, 'my-plugin/languages/'); 
    generateInvoice(); // produce the English localized invoice 

    $locale = 'fr_CA'; 
    load_plugin_textdomain('invoice', false, 'my-plugin/languages/'); 
    generateInvoice(); // produce the French localized invoice 
0

我也有類似的問題,解決它像這樣:

  1. 在我的情況,我想通過使用用戶的語言環境來檢索語言環境:$userLocale = get_user_locale($userObject->ID);

  2. 我創建了一個自定義函數加載動態語言環境的正確theme_textdomain。這幾乎等同於WP功能,但你可以添加一個語言環境變量:基於

    /** 
    * Loads text domain by custom locale 
    * Based on a WP function, only change is the custom $locale 
    * parameter so we can get translated strings on demand during runtime 
    */ 
    function load_theme_textdomain_custom_locale($domain, $path = false, $locale) 
    { 
        /** 
        * Filter a theme's locale. 
        * 
        * @since 3.0.0 
        * 
        * @param string $locale The theme's current locale. 
        * @param string $domain Text domain. Unique identifier for retrieving translated strings. 
        */ 
        $locale = apply_filters('theme_locale', $locale, $domain); 
    
        if (!$path) { 
         $path = get_template_directory(); 
        } 
    
        // Load the textdomain according to the theme 
        $mofile = "{$path}/{$locale}.mo"; 
        if ($loaded = load_textdomain($domain, $mofile)) { 
         return $loaded; 
        } 
    
        // Otherwise, load from the languages directory 
        $mofile = WP_LANG_DIR . "/themes/{$domain}-{$locale}.mo"; 
        return load_textdomain($domain, $mofile); 
    } 
    

http://queryposts.com/function/load_theme_textdomain/

相關問題