Difference between revisions of "Translations / i18n / localizations for programs/ja"

From Lazarus wiki
Jump to navigationJump to search
Line 344: Line 344:
 
IDE の .po ファイルは以下の Lazarus ソースディレクトリにあります。
 
IDE の .po ファイルは以下の Lazarus ソースディレクトリにあります。
 
*lazarus/languages/ - IDE で使われている文字列
 
*lazarus/languages/ - IDE で使われている文字列
*lcl/languages/ - LCL で使われている文字列
+
*lazarus/lcl/languages/ - LCL で使われている文字列
*ideintf/languages/ - IDE インターフェースで使われている文字列
+
*lazarus/components/ideintf/languages/ - IDE インターフェースで使われている文字列
  
 
===翻訳者===
 
===翻訳者===

Revision as of 10:54, 15 December 2013

Deutsch (de) English (en) español (es) français (fr) 日本語 (ja) 한국어 (ko) polski (pl) português (pt) русский (ru) 中文(中国大陆)‎ (zh_CN)

日本語版メニュー
メインページ - Lazarus Documentation日本語版 - 翻訳ノート - 日本語障害情報

概要

このページでは、プログラムで用いる文字列を英語、中国語、ドイツ語、フィンランド語、イタリア語…といった各国語にあわせて変更することができるようにする方法を紹介します。基本的には、次のようになります : 各々のキャプションに resourcestring (リソース文字列定義)を加え、コンパイルして .rst ファイルないしは .po ファイルを得ます(IDE が自動的に行います)。各国語用にそれぞれ一つの翻訳済み .po ファイルを生成し、LCL の translations ユニット内の関数を用いて、プログラムの起動時に、正しいものをロードします。


Date, time and number format

Under Linux, BSD, Mac OS X there are several locales defining things like time and date format or the thousand separator. In order to initialize the RTL you need to iclude the clocale unit in the uses section of your program (lpr file).

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、po-auto-translator、poedit、virtaal があります。

Virtaal has a translation memory containing source-target language pairs for items that you already translated once, and a translation suggestion function that shows already translated terms in various open source software packages. These function may save you a lot of work and improve consistency.

直接 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 などなどサポートしようと思っている言語用の名前でコピーして、そのファイルを各国語の翻訳者が編集すればいいわけです。

Light bulb  Note: ブラジル人/ポルトガル人へ: Lazarus IDE と LCL はブラジルのポルトガル語(拡張子 'pt_BR.po')だけしか用意しておりません。

自動的に .po ファイルをアップデートする IDE のオプション

  • リソース文字列を含むユニットをパッケージないしプロジェクトに加えます。
  • .po ファイルを出力するパスを指定します。これは隔離された単独のディレクトリにしてください。例えば、パッケージ/プロジェクトのディレクトリに language という名前のサブディレクトリを作ります。プロジェクトに追加する場合はプロジェクト→プロジェクトオプション、パッケージの場合はオプション→IDE 統合から行います。

このオプションが有効になっている場合、IDE は .rst および .lrt ファイルに含まれている情報を使用して原本となる .po ファイルを生成・更新します(この後に rstconv ツールを使う必要はありません)。これは自動更新処理として行われます。原本の .po ファイル、.rst と .lrt ファイルにすべての項目を集計し、その後に翻訳ファイル .xx.po に対して該当する以下の機能による更新が行われます。

使われなくなった項目の除去

.rst および .lrt で見つからなかった項目は、原本の .po ファイルから削除されます。その次に、原本の .po ファイルで見つからなかったすべての項目も、翻訳ファイル .xx.po から削除されます。こうすることで、使われなくなった項目で .po ファイルが乱されることはなくなり、これらの項目を無駄に翻訳しなくてすむようになります。

重複項目

いくつかの理由によって同じ文字列が異なったリソース文字列で使われている場合に、重複項目が生じます。例えば、ファイル lazarus/ide/lazarusidestrconst.pas に 'Gutter' という文字列があるとします。

  dlfMouseSimpleGutterSect = 'Gutter';
  dlgMouseOptNodeGutter = 'Gutter';
  dlgGutter = 'Gutter';
  dlgAddHiAttrGroupGutter = 'Gutter';

.rst ファイルを経て変換された .po ファイル上では、これらの文字列は同じように見えてしまいます。

#: lazarusidestrconsts.dlfmousesimpleguttersect
msgid "Gutter"
msgstr ""

#: lazarusidestrconsts.dlgaddhiattrgroupgutter
msgid "Gutter"
msgstr ""

・・・

"#: " で始まる行は重複を考慮したコメントになっていますが、翻訳ツールでこの項目を翻訳しようとすると、繰り返し現れる msgid "Gutter" の行が重複項目と見られ、読み込みや保存時にエラーもしくは警告を発します。関連する複数の状況で使い分けの必要があるため、.po ファイル上における重複項目は少ないながらも存在するのです。msgctxt キーワードは重複項目へ状況(コンテキスト)を追加するのに使われるもので、自動更新処理はコンテキスト("#: " で始まる文の次行)も項目 ID として使用します。上にあった例では、以下のように生成されます。

#: lazarusidestrconsts.dlfmousesimpleguttersect
msgctxt "lazarusidestrconsts.dlfmousesimpleguttersect"
msgid "Gutter"
msgstr ""

#: lazarusidestrconsts.dlgaddhiattrgroupgutter
msgctxt "lazarusidestrconsts.dlgaddhiattrgroupgutter"
msgid "Gutter"
msgstr ""

・・・

On translated .xx.po files the automatic tool does one additional check: if the duplicated entry was already translated, the new entry gets the old translation, so it appears like being translated automatically.

The automatic detection of duplicates is not yet perfect, duplicate detection is made as items are added to the list and it may happen that some untranslated entries are read first. So it may take several passes to get all duplicates automatically translated by the tool.

未確定項目

リソース文字列が変更されると、翻訳されてものにも影響します。例えば、以下に示すようなリソース文字列が最初に定義されていたとします。 (訳注:以下の例は実際の Lazarus の変更履歴を元にしていますが、元の例は日本語ではあまり適切ではないので、別の例を取り上げています。)

lisKMToggleViewCallStack = 'Toggle view Call Stack';

これに基に作られる .po ファイルの該当項目は以下のようになります。

#: lazarusidestrconsts.liskmtoggleviewcallstack
msgid "View Call Stack"
msgstr ""

日本語の翻訳をすると以下のようになります。

#: lazarusidestrconsts.liskmtoggleviewcallstack
msgid "Toggle view Call Stack"
msgstr "コールスタックの表示の切り替え"

後日、このリソース文字列が以下のように変更されたとします。

lisKMToggleViewCallStack = 'View Call Stack';

この修正で .po ファイルの項目は以下のようになるでしょう。

#: lazarusidestrconsts.liskmtoggleviewcallstack
msgid "View Call Stack"
msgstr ""

lazarusidestrconsts.liskmtoggleviewcallstack を頼りに項目を識別して、原文の文字列は 'Toggle view Call Stack' から 'View Call Stack' へ変更されます。原文に対して既に翻訳が行われていると、元の訳は新しい意味に合わなくなります。先の例では、新たに 'コールスタックを表示' と訳すほうがより相応しいでしょう。自動更新処理ではこうした状況を、以下のように項目を生成して知らせるようにしています。

#: lazarusidestrconsts.liskmtoggleviewcallstack
#, fuzzy
#| msgid "Toggle view Call Stack"
msgid "View Call Stack"
msgstr "コールスタックの表示の切り替え"

In terms of .po file format, the "#," prefix means the entry has a flag (fuzzy) and translator programs may present a special GUI to the translator user for this item. In this case, the flag would mean that the translation in its current state is doubtful and needs to be reviewed more carefully by translator. The "#|" prefix indicates what was the previous untranslated string of this entry and gives the translator a hint why the entry was marked as fuzzy.

フォーム、データモジュール、フレームの翻訳

プロジェクトやパッケージの国際化(i18n、internationalisationの略)オプションが選択されていれば、IDE はユニットを保存する際、自動的に各フォームにつき一つの .lrt ファイルを生成します。例えば、unit1.pas を保存すると unit1.lrt ができます。ですから、最初にこのオプションを選択した後で、全てのフォームを一度は開き、少しいじって、保存してください。コンパイル時には、IDE が全ての .lrt ファイルに存在する全ての文字列を集めて国際化ディレクトリの中の単一の .po ファイルにまとめます(プロジェクト名.po ないしは パッケージ名.po)。

For the forms to be actually translated at runtime, you have to assign a translator to LRSTranslator (defined in LResources) in the initialization section to one of your units

...
uses
  ...
  LResources;
...
...
initialization
  LRSTranslator := TPoTranslator.Create('/path/to/the/po/file');

However there's no TPoTranslator class (i.e a class that translates using .po files) available in the LCL. This is a possible implementation (partly lifted from DefaultTranslator.pas in the LCL): The following code isn't needed anymore if you use recent Lazarus 0.9.29 snapshots. Simply include DefaultTranslator in Uses clause.

unit PoTranslator;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, typinfo, Translations;

type

 { TPoTranslator }

 TPoTranslator=class(TAbstractTranslator)
 private
  FPOFile:TPOFile;
 public
  constructor Create(POFileName:string);
  destructor Destroy;override;
  procedure TranslateStringProperty(Sender:TObject; 
    const Instance: TPersistent; PropInfo: PPropInfo; var Content:string);override;
 end;

implementation

{ TPoTranslator }

constructor TPoTranslator.Create(POFileName: string);
begin
  inherited Create;
  FPOFile:=TPOFile.Create(POFileName);
end;

destructor TPoTranslator.Destroy;
begin
  FPOFile.Free;
  inherited Destroy;
end;

procedure TPoTranslator.TranslateStringProperty(Sender: TObject;
  const Instance: TPersistent; PropInfo: PPropInfo; var Content: string);
var
  s: String;
begin
  if not Assigned(FPOFile) then exit;
  if not Assigned(PropInfo) then exit;
{DO we really need this?}
  if Instance is TComponent then
   if csDesigning in (Instance as TComponent).ComponentState then exit;
{End DO :)}
  if (AnsiUpperCase(PropInfo^.PropType^.Name)<>'TTRANSLATESTRING') then exit;
  s:=FPOFile.Translate(Content, Content);
  if s<>'' then Content:=s;
end;

end.

Alternatively you can transform the .po file into .mo using msgfmt (isn't needed anymore if you use recent 0.9.29 snapshot) and simply use the DefaultTranslator unit

...
uses
   ...
   DefaultTranslator;

which will automatically look in several standard places for a .po file (higher precedence) or .mo file (the disadvantage is that you'll have to keep around both the .mo files for the DefaultTranslator unit and the .po files for TranslateUnitResourceStrings). If you use DefaultTranslator, it will try to automatically detect the language based on the LANG environment variable (overridable using the --lang command line switch), then look in these places for the translation (LANG stands for the desired language, ext can be either po or mo):

  • <Application Directory>/<LANG>/<Application Filename>.<ext>
  • <Application Directory>/languages/<LANG>/<Application Filename>.<ext>
  • <Application Directory>/locale/<LANG>/<Application Filename>.<ext>
  • <Application Directory>/locale/LC_MESSAGES/<LANG/><Application Filename>.<ext>

under unix-like systems it will also look in

  • /usr/share/locale/<LANG>/LC_MESSAGES/<Application Filename>.<ext>

as well as using the short part of the language (e.g. if it is "es_ES" or "es_ES.UTF-8" and it doesn't exist it will also try "es")

プログラム起動時の処理

すべての .po ファイルについて、それぞれに TranslateUnitResourceStrings を呼ばなければなりません。LCL の po ファイルは lclstrconsts です。以下のようにメインフォームの FormCreate などから呼び出します。

uses
 ..., gettext, translations;

procedure TForm1.FormCreate(Sender: TObject);
var
  PODirectory, Lang, FallbackLang: String;
begin
  PODirectory := '/path/to/lazarus/lcl/languages/';
  GetLanguageIDs(Lang, FallbackLang);
  Translations.TranslateUnitResourceStrings('LCLStrConsts', PODirectory + 'lclstrconsts.%s.po', Lang, FallbackLang);

  // the following dialog now shows translated buttons:
  MessageDlg('Title', 'Text', mtInformation, [mbOk, mbCancel, mbYes], 0);
end;

Compiling po files into the executable

If you don't want to install the .po files, but put all files of the application into the executable, use the following:

  • Create a new unit (not a form!).
  • Convert the .po file(s) to .lrs using tools/lazres:
./lazres unit1.lrs unit1.de.po

This will create an include file unit1.lrs beginning with

LazarusResources.Add('unit1.de','PO',[
  ...
  • Add the code:
uses LResources, Translations;

resourcestring
  MyCaption = 'Caption';

function TranslateUnitResourceStrings: boolean;
var
  r: TLResource;
  POFile: TPOFile;
begin
  r:=LazarusResources.Find('unit1.de','PO');
  POFile:=TPOFile.Create;
  try
    POFile.ReadPOText(r.Value);
    Result:=Translations.TranslateUnitResourceStrings('unit1',POFile);
  finally
    POFile.Free;
  end;
end;

initialization
  {$I unit1.lrs}
  • Call TranslateUnitResourceStrings at the beginning of the program. You can do that in the initialization section if you like.

Cross-platform method to determine system language

The following function delivers a string that represents the language of the user interface. It supports Linux, Mac OS X and Windows.

uses
  Classes, SysUtils {add additional units that may be needed by your code here}
  {$IFDEF win32}
  , Windows
  {$ELSE}
  , Unix
    {$IFDEF LCLCarbon}
  , MacOSAll
    {$ENDIF}
  {$ENDIF}
  ;
function GetOSLanguage: string;
{platform-independent method to read the language of the user interface}
var
  l, fbl: string;
  {$IFDEF LCLCarbon}
  theLocaleRef: CFLocaleRef;
  locale: CFStringRef;
  buffer: StringPtr;
  bufferSize: CFIndex;
  encoding: CFStringEncoding;
  success: boolean;
  {$ENDIF}
begin
  {$IFDEF LCLCarbon}
  theLocaleRef := CFLocaleCopyCurrent;
  locale := CFLocaleGetIdentifier(theLocaleRef);
  encoding := 0;
  bufferSize := 256;
  buffer := new(StringPtr);
  success := CFStringGetPascalString(locale, buffer, bufferSize, encoding);
  if success then
    l := string(buffer^)
  else
    l := '';
  fbl := Copy(l, 1, 2);
  dispose(buffer);
  {$ELSE}
  {$IFDEF LINUX}
  fbl := Copy(GetEnvironmentVariable('LC_CTYPE'), 1, 2);
    {$ELSE}
  GetLanguageIDs(l, fbl);
    {$ENDIF}
  {$ENDIF}
  Result := fbl;
end;

IDE の翻訳

ファイル

IDE の .po ファイルは以下の Lazarus ソースディレクトリにあります。

  • lazarus/languages/ - IDE で使われている文字列
  • lazarus/lcl/languages/ - LCL で使われている文字列
  • lazarus/components/ideintf/languages/ - IDE インターフェースで使われている文字列

翻訳者

  • ドイツ語の翻訳は Joerg Braun 氏によってメンテナンスされています。
  • フィンランド語の翻訳は Seppo Suurtarla 氏によってメンテナンスされています。
  • ロシア語の翻訳は Maxim Ganetsky 氏によってメンテナンスされています。

新たに翻訳を始める場合は、既に誰かが取り掛かっていないかメーリングリストで尋ねてください。

また、Translations をよくお読みになってください。

(以下訳注追記)

関連項目