登入

po檔和mo檔-wordpress的theme和plugin的翻譯

作者:neo yang 時間:2023/07/03 讀: 8051
對於wordpress的theme作者和plugin作者,翻譯自己的theme和plugin就避不開po檔和[…]

對於wordpress的theme作者和plugin作者,翻譯自己的theme和plugin就避不開po檔和mo檔。

po文件,是對theme和plugin的翻譯文件。但,wordpress並不會使用這個檔案。因為wordpress用的是mo檔。

mo檔是根據po檔產生的二進位。

所以翻譯自己的theme和plugin,需要編輯po文件,然後將這個po檔轉換為mo檔。

 

翻譯自己的theme和plugin的步驟:

1、在theme或plugin的根目錄中建立「languages」資料夾。

2.建立一個po文件,例如:zh-CN.po

3.編輯這個po文件,文件內容結構大致如下:

msgid “Title”

msgstr “標題”

 

msgid “Time”

msgstr “時間”

注意:msgid是程式碼中的文字。 msgstr是翻譯後的文字。

4.翻譯完之後,將這個po檔轉換成mo文件

有線上轉換工具:https://po2mo.net/

5.將languages資料夾和po檔、mo檔全部上傳到伺服器。

6.在theme或plugin的functions檔案中加入:

function the_languages_setup()

{

//~ 載入本地化語言文件

load_theme_textdomain('textdomain', get_template_directory() . '/languages');

}

add_action('after_setup_theme', 'the_languages_setup');

 

這樣,程式碼中,_e('Title','textdomain')或__('Title','textdomain')顯示的「Title」就會被翻譯成「標題」。

 

標籤:


copyright © www.lyustu.com all rights reserve.
Theme: TheMoon V3.0. Author:neo yang