TODO: To change the header's content go to Dr.Explain menu Options : Project Settings : HTML (CHM) Export : Setup HTML Template and Layout
×
Menu
Index

Dịch đa ngôn ngữ

File ngôn ngữ dịch .mo biên dịch từ file .po/.pot
Công cụ: sử dụng trình biên dịch ngôn ngữ PO Editor,.
 
File dịch ngôn ngữ.
Cấu trúc file PO có dạng:
"Project-Id-Version: WordPress Notification Bar\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2012-12-12 12:25-0500\n"
"PO-Revision-Date: 2013-05-27 21:03+0100\n"
"Last-Translator: Daniel Hoffmann <[email protected]>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Poedit-KeywordsList: __;_e\n"
"X-Poedit-Basepath: ../\n"
"X-Generator: Poedit 1.5.5\n"
"Language: de\n"
"X-Poedit-SearchPath-0: .\n"
 
#: wordpress-notification-bar.php:27
msgid "WordPress Notification Bar"
msgstr "WordPress Notification Bar"
....
Có 2 phần:
+ phần đầu lưu thông tin cho ngôn ngữ.
+ Tiếp đó là các chuỗi có sử dụng trong website, chuỗi gốc msgid và chuỗi được dịch lưu ở msgstr.
 
- Sử dụng ký tự “-” trong file .PO để ngăn cách giữa tên textdomain và local code. VD: seed_wnb-de_DE.po
Lưu ý:
Tên file ngôn ngữ cho plugin phân biệt bởi textdomain. VD: hoangweb-vi.mo
Tên file ngôn ngữ cho theme không chứa tên textdomain đằng trước. VD: vi.mo
 
- Công cụ biên soạn ngôn ngữ dịch: PO Editor (http://www.poedit.net/).
Sau khi sửa xong, nhấn lưu, PoEdit sẽ tạo file .mo đây là binary file wordpress sẽ đọc file này. Chép file này lên hosting.
 
Thiết lập ngôn ngữ trong wordpress.
Tiếp theo, bạn cần kích hoạt sử dụng một ngôn ngữ trong wordpress. Thiết lập mã (local code) bằng cách khai báo hằng WPLANG trong file wp-config.php
//tiếng pháp
define ('WPLANG', 'fr_FR');
//tiếng việt
define ('WPLANG', 'vi_VI');
 
Hàm cơ bản dịch chuỗi:
Sau khi dịch cố định chuỗi hiển thị trong file .mo textdomain , bạn cũng có thể thay đổi hiển thị bất kỳ chuỗi nào với hàm __, _e, _x(
__() & _e() là 2 hàm cơ bản trả về chuỗi đã được dịch.
 
Chuỗi dịch với Ngữ cảnh khác nhau:
Hàm _x( sử dụng khác một chút:
string _x (string $text, string $context, [string $domain = 'default'])
 
Bằng cách sử dụng tham số $context bạn có thể nhận dạng các chuỗi dịch trong ngữ cảnh khác nhau. Ví dụ: dịch thành 2 kết quả khác nhau từ một chuỗi vậy thì làm thế nào, nó không quá khó để thực hiện, hình dung chúng ta có thêm 1 biến ngữ cảnh.
msgctxt "test1"
msgid "testing"
msgstr "context1"
 
msgctxt "test2"
msgid "testing"
msgstr "context2"
 
bí mật là ở tham số msgctxt, trong code bạn xác định giá trị của  nó như sau:
echo 'Context: test1 -> ' . _x('testing', 'test1', 'test');
echo '<br>Context: test2 -> ' . _x('testing', 'test2', 'test');
 
Và cho kết quả là:
Context: test1 -> context1
Context: test2 -> context2
 
Single & plural:
Ngoài ra còn có hàm _n (các bạn tham khảo trên Wordpress Codex).
_n sử dụng với mục đích lấy chuỗi có giá trị với số ít (single) hoặc số nhiều (plural) dựa trên 1 con số, thì chúng ta sử dụng  hàm dịch này. Cú pháp:
_n( $single, $plural, $number, $domain = 'default' )
 
Nếu bạn dự định hiển thị chuỗi chứa số bằng cách sử dụng hàm sprintft, hãy xem ví dụ sau :
$approved = 1;                   
echo sprintf( _n( '%s comment approved', '%s comments approved', $approved, 'test' ), $approved);
echo '<br/>';
$approved = 2;                   
echo sprintf( _n( '%s comment approved', '%s comments approved', $approved, 'test' ), $approved );
 
Trong file .po bạn nhập với cấu trúc sau:
msgid "%s comment approved"
msgid_plural "%s comments approved"
msgstr[0] "%s Kommentar genehmigt"   #single translate
msgstr[1] "%s Kommentare genehmigt"   #plural translate
 
Thay đổi hiển thị chuỗi dịch:
- Sau khi dịch cố định chuỗi hiển thị trong file .mo textdomain, bạn cũng có thể thay đổi hiển thị bất kỳ chuỗi nào với hàm __, _e, _(
function graphene_filter_readmore( $translated, $original, $domain ) {
    $strings = array(
                'Continue reading &raquo;' => 'CONTINUE READING!!',
        'Read the rest of this entry &raquo;' => 'CONTINUE READING!!',
    );
    //_print($original);
    if ( ! empty( $strings[$original] ) ) {
        $translations = &get_translations_for_domain( $domain );
        $translated = $translations->translate( $strings[$original] );
    }
    return $translated;
}
add_filter( 'gettext', 'graphene_filter_readmore', 10, 3 );
add_filter('ngettext',  'translate_text');
 
Một ví dụ khác.
add_filter('gettext',  'translate_text');
add_filter('ngettext',  'translate_text');
 
function translate_text($translated) {
     $translated = str_ireplace('Choose and option',  'Select',  $translated);
     return $translated;
}
 
 
Made with help of Dr.Explain

Unregistered version