2011-11-23 9 views
5

Tôi có một trang web dựa trên người dùng với Wordpress và từ cài đặt tiểu sử của họ, họ có thể chọn ngôn ngữ, thông tin này và các cài đặt khác được đặt cho mọi người dùng trong user_meta.Đặt ngôn ngữ Wordpress theo lập trình?

Tôi biết cách dịch nhưng, có cách nào để đặt ngôn ngữ chủ đề theo lập trình không?

Cảm ơn bạn!

Chỉnh sửa: Không cần plugin, tôi cần thực hiện việc này đơn giản nhất có thể.

Trả lời

2

Tôi tìm thấy một giải pháp khác nhau:

// 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'); 

} 

Via: http://codex.wordpress.org/Function_Reference/load_theme_textdomain

2

Tôi đoán bạn đang tìm kiếm bộ lọc override_load_textdomain, được gọi ngay khi bắt đầu cuộc gọi hàm load_textdomain.

Đó sẽ là một cái gì đó như:

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'); 

Mã từ não đến bàn phím, chưa được thử nghiệm. Bạn nên làm một số xác nhận nhiều hơn và như vậy.

+0

Cảm ơn bạn, tôi sẽ cung cấp cho nó một thử nghiệm và cho bạn biết. –

+0

Tôi nhận được "Cảnh báo: Thiếu đối số 2 cho my_load_textdomain() trong [...]", mã giống như bạn đã đăng. –

+0

Xin lưu ý rằng mã tôi đăng sẽ không hoạt động. Nó chỉ là một tài liệu tham khảo cho bạn để áp dụng cho môi trường trang web của bạn. Tôi vừa sửa lời gọi hàm để chấp nhận các đối số mà bộ lọc cần. – vmassuchetto

2

tôi đã đưa ra giải pháp sau đây như tôi cần phải tạo hóa đơn từ một plugin bằng các ngôn ngữ khác nhau trong phạm vi cùng một yêu cầu:

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

tôi đã có một vấn đề tương tự và giải quyết nó như thế này:

0.123.
  1. Trong trường hợp của tôi, tôi muốn lấy lại miền địa phương bằng cách sử dụng các địa phương sử dụng: $userLocale = get_user_locale($userObject->ID);

  2. Tôi tạo ra một chức năng tùy chỉnh để nạp theme_textdomain đúng với miền địa phương năng động. Nó gần giống như chức năng WP, nhưng bạn có thể thêm một biến ngôn ngữ:

    /** 
    * 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); 
    } 
    

Dựa trên: http://queryposts.com/function/load_theme_textdomain/