天天看點

程式國際化(I18N)快速起步

翻譯:高瑩婷

Gettexta是GNU國際化(i18n)的函數庫,并且是開源技術标準。翻譯人員了解該函數庫後,就可以應用與其相關的大量工具。本文将提供關于開始進行項目國際化工作的關鍵步驟的簡短概要。

注意:如果你正準備開始一個新項目,那麼Linux項目生成器(Linux Project Generator)将幫助你正确地建立支援項目國際化的自動安裝檔案。

改變項目代碼

第一步:更新主程式的頭檔案

使用main()函數,在C子產品的前面添加下面内容:

#ifdef USE_GETTEXT

#include "libintl.h"

#include "locale.h"

#define _(String) gettext (String)

#else

#define _(String) String

#endif

第二步:主函數

在你的main()函數的開始添加以下内容:

#ifdef USE_GETTEXT

setlocale (LC_MESSAGES, "");

setlocale (LC_CTYPE, "");

setlocale (LC_COLLATE, "");

textdomain ("my-program");

bindtextdomain ("my-program", NULL);

#endif

第三步:更新頭檔案

在每個國際化字元串的前面添加以下内容:

#ifdef USE_GETTEXT

#include "libintl.h"

#define _(String) gettext (String)

#else

#define _(String) String

#endif

第四步:字元串字首

在寫字元串代碼的時候,使用格式: _( <string> )。例如,将下清單達式:

(Not internationalized)

printf ("some string");

替代為:

(Internationalized)

printf (_("some string") )

第五步:建立标記

使用-DUSE_GETTEXT指令編譯程式,或者添加下面的内容在主頭檔案:

#define USE_GETTEXT

第六步:生成pot及po檔案

pot檔案包含了可以定域的(可移植的)字元串清單。pot檔案通常是一個項目的辨別,甚至可以是多個項目的。

建立pot檔案:

xgettext -k_ -o my-program.pot *.c *.h --from-code=iso-8859-1

The .pot file that is generated from the above command is the template that contains the English strings and a corresponding empty string field for the translator to add the translation. Once the translations are complete, the file is saved with a .po extension to indicate it is complete. There is one .po file for each language.

.pot檔案是由以上的指令生成的一個模闆,包含了英文字元串和相應的空字元串檔案,用于翻譯者添加翻譯。每次翻譯結束,就可以儲存為一個擴充名為po的檔案表示完成。po檔案可以使用任何一種語言。

最後,為每個po檔案建立一個對應的mo檔案。mo檔案是被納入安裝包的二進位制譯本。

msgfmt my-program.es.po -o my-program.es.mo

第七步:安裝檔案

在正确的目錄下安裝mo檔案:

cp my-program.es.mo /usr/share/locale/es/LC_MESSAGES/my-program.mo

您可以通過Internationalization Guidelines (PDF, 450KB)查找詳細的步驟說明。

原文位址:

http://moblin.org/documentation/moblin-sdk/coding-tutorials/getting-started-internationalization-i18n

預告:7月将進行Moblin V2.0線上、線下研讨會,請您随時關注!

繼續閱讀