Translations / i18n / localizations for programs/ja
│
Deutsch (de) │
English (en) │
español (es) │
français (fr) │
日本語 (ja) │
한국어 (ko) │
polski (pl) │
português (pt) │
русский (ru) │
中文(中国大陆) (zh_CN) │
概要
このページでは、プログラムで用いる文字列を英語、中国語、ドイツ語、フィンランド語、イタリア語…といった各国語にあわせて変更することができるようにする方法を紹介します。基本的には、次のようになります : 各々のキャプションに resourcestring (リソース文字列定義)を加え、コンパイルして .rst ファイルないしは .po ファイルを得ます(IDE が自動的に行います)。各国語用にそれぞれ一つの翻訳済み .po ファイルを生成し、LCL の translations ユニット内の関数を用いて、プログラムの起動時に、正しいものをロードします。
Resourcestrings
例えば
resourcestring Caption1 = 'Some text'; HelloWorld1 = 'Hello World';
これらは通常の文字列定数と同様に、いかなる文字列にも代入することができます。例えば
Label1.Caption := HelloWorld1;
コンパイル時にfpc は unitname.rst を各ユニットに一つ生成します。その中にはリソース文字列のデータ(名前と中身)が含まれます。
.po ファイル
生成した .po ファイルを編集するためには、数多くのグラフィカルツールが無料で提供されています。実際 .po ファイルは .rst ファイル同様単なるテキストファイルですが、作者・文字コード・言語・日付といった要素を含んだヘッダのような付随的な要素を含んでいます。fpc をインストールすれば必ず rstconv というツールがついてきます (Windowsでは rstconv.exe)。このツールは .rst ファイルを .po ファイルに変換します。IDE を使うと、この変換操作を自動的に行うことができます。フリーなツールの例としては kbabel、poedit があります。
直接 rstconv を用いる例:
rstconv -i unit1.rst -o unit1.po
翻訳
それぞれの言語用に .po ファイルをコピーして翻訳する必要があります。LCL の translation ユニットは標準言語コード (en=英語, de=ドイツ語, it=イタリア語, ...) を用いて言語を検索します。例えば、unit1.po のドイツ語版は unit1.de.po となります。つまり、unit1.po ファイルを unit1.de.po、unit1.it.po などなどサポートしようと思っている言語用の名前でコピーして、そのファイルを各国語の翻訳者が編集すればいいわけです。
ブラジル人/ポルトガル人への註:: Lazarus IDE と LCL はブラジルのポルトガル語(拡張子 'pb.po')だけを用意しており、 'pt.po' はありません。
自動的に .po ファイルをアップデートする IDE のオプション
- The unit containing the resource strings must be added to the package or project.
- You must provide a .po path, this means a separate directory. For example: create a sub directory language in the package / project directory. For projects go to the Project > Project Options. For packages go to Options > IDE integration.
Translating Forms, Datamodules and Frames
When the i18n option is enabled for the project / package then the IDE automatically creates .lrt files for every form. It creates the .lrt file on saving a unit. So, if you enable the option for the first time, you must open every form once, move it a little bit, so that it is modified, and save the form. For example if you save a form unit1.pas the IDE creates a unit1.lrt. And on compile the IDE gathers all strings of all .lrt files and all .rst file into a single .po file (projectname.po or packagename.po) in the i18n directory.
Translating at start of program
For every .po file, you must call TranslateUnitResourceStrings of the LCL translations unit. For example:
<pascal>
{First of all: add "gettext" and "translations" units in uses clause} procedure TForm1.FormCreate(Sender: TObject); var PODirectory, Lang, FallbackLang: String; begin PODirectory := '/path/to/lazarus/lcl/languages/'; GetLanguageIDs(Lang, FallbackLang); // in unit gettext TranslateUnitResourceStrings('LCLStrConsts', PODirectory + 'lclstrconsts.%s.po', Lang, FallbackLang); MessageDlg('Title', 'Text', mtInformation, [mbOk, mbCancel, mbYes], 0); end;
</pascal>
Future work / ToDos
IDE Development: Translations, i18n, lrt, po files