gettext を使ったローカライズ¶
In addition to 翻訳のインポート in CSV format, Godot
also supports loading translation files written in the GNU gettext
format (text-based .po
and compiled .mo
since Godot 3.5).
注釈
gettextの概要については、A Quick Gettext Tutorial をご覧ください。 Cプロジェクトを念頭に置いて書かれていますが、アドバイスの多くはGodotにも当てはまります(``xgettext``を除く)。
利点¶
欠点¶
gettextはCSVよりも複雑な形式であり、ソフトウェアのローカライズを初めて行う人にとっては把握が難しい場合があります。
People who maintain localization files will have to install gettext tools on their system. However, as Godot supports using text-based message files (
.po
), translators can test their work without having to install gettext tools.
注意事項¶
Godotは独自のPOファイルパーサーをバックグラウンドで使用するため(GNU gettextのリファレンス実装よりも制限されています)、複数形化などの一部の機能はサポートされていません。
gettextツールのインストール¶
メッセージファイルの更新などのメンテナンス操作を実行するには、コマンドラインのgettextツールが必要です。したがって、それらをインストールすることを強くお勧めします。
Windows: このページ からインストーラーをダウンロードします。すべてのアーキテクチャおよびバイナリタイプ(共有または静的)が機能します。疑わしい場合は、64ビットの静的インストーラーを選択してください。
macOS: Install gettext either using Homebrew with the
brew install gettext
command, or using MacPorts with thesudo port install gettext
command.Linux: ほとんどのディストリビューションでは、ディストリビューションのパッケージマネージャーから
gettext
パッケージをインストールします。
POテンプレート (POT)の手動作成¶
Godotは現在、xgettext
を使用したソース文字列の抽出をサポートしていないため、.pot
ファイルを手動で作成する必要があります。このファイルはプロジェクトディレクトリのどこにでも配置できますが、各ロケールは個別のファイルで定義されるため、サブディレクトリに保存することをお勧めします。
プロジェクトディレクトリに locale という名前のディレクトリを作成します。このディレクトリに、次の内容の messages.pot
という名前のファイルを保存します:
# Don't remove the two lines below, they're required for gettext to work correctly.
msgid ""
msgstr ""
msgid "Hello world!"
msgstr ""
gettextのメッセージは、msgid
と msgstr
のペアで構成されています。msgid
はソース文字列(通常は英語)で、msgstr
は翻訳された文字列です。
POテンプレートファイル(.pot
)の msgstr
値は常に空である必要があります。代わりに、生成された .po
ファイルでローカライズが行われます。
pybabelを使ったPOテンプレート(POT)の作成¶
PythonツールpybabelはGodotをサポートしており、シーンファイルとスクリプトからPOTファイルを自動的に作成および更新するために使用できます。
babel
と babel-godot
をインストールした後、例えばpipを使用して:
pip3 install babel babel-godot
pybabelが処理する必要があるファイルを示すマッピングファイル(たとえば babelrc
)を記述します(GDScriptをPythonとして処理することに注意してください。通常はこれで事足ります):
[python: **.gd]
encoding = utf-8
[godot_scene: **.tscn]
encoding = utf-8
pybabelを次のように実行できます:
pybabel extract -F babelrc -k text -k LineEdit/placeholder_text -k tr -o godot-l10n.pot .
抽出する必要があるものを指定するには、-k
オプションを使用します。この場合、tr() への引数と、(コントロールノードで一般的に使用される)"text"、およびLineEditの "placeholder_text"という名前のプロパティが変換されます。
POテンプレートからメッセージファイルを作成する¶
msginit
コマンドは、POテンプレートをメッセージファイルに変換するために使用されます。たとえば、フランス語のローカライズファイルを作成するには、locale
ディレクトリで次のコマンドを使用します:
msginit --no-translator --input=messages.pot --locale=fr
上記のコマンドは、POテンプレートと同じディレクトリに fr.po
という名前のファイルを作成します。
または、Poeditを使用してグラフィカルに行うか、選択したWebプラットフォームにPOTファイルをアップロードします。
Godot でのメッセージ ファイルの読み込み¶
To register a messages file as a translation in a project, open the
Project Settings, then go to the Localization tab.
In Translations, click Add… then choose the .po
or .mo
file
in the file dialog. The locale will be inferred from the
"Language: <code>\n"
property in the messages file.
注釈
Godotでの翻訳のインポートとテストの詳細については、ゲームの国際化 を参照してください。
POテンプレートに従うようにメッセージファイルを更新する¶
After updating the PO template, you will have to update message files so
that they contain new strings, while removing strings that are no longer
present in the PO template. This can be done automatically using the
msgmerge
tool:
# The order matters: specify the message file *then* the PO template!
msgmerge --update --backup=none fr.po messages.pot
元のメッセージファイル(この例では fr.po~
として保存される)のバックアップを保持する場合は、--backup=none
引数を削除します。
注釈
After running msgmerge
, strings which were modified in the source language
will have a "fuzzy" comment added before them in the .po
file. This comment
denotes that the translation should be updated to match the new source string,
as the translation will most likely be inaccurate until it's updated.
Strings with "fuzzy" comments will not be read by Godot until the translation is updated and the "fuzzy" comment is removed.
POファイルまたはテンプレートの有効性の確認¶
以下のコマンドを実行すると、gettextファイルの構文が有効かどうかを確認できます:
msgfmt fr.po --check
構文エラーまたは警告がある場合は、コンソールに表示されます。そうでなければ、msgfmt
は何も出力しません。
Using binary MO files (useful for large projects only)¶
For large projects with several thousands of strings to translate or more, it can be worth it to use binary (compiled) MO message files instead of text-based PO files. Binary MO files are smaller and faster to read than the equivalent PO files.
You can generate a MO file with the command below:
msgfmt fr.po --no-hash -o fr.mo
If the PO file is valid, this command will create a fr.mo
file besides
the PO file. This MO file can then be loaded in Godot as described below.
The original PO file should be kept in version control so you can update your translation in the future. In case you lose the original PO file and wish to decompile a MO file into a text-based PO file, you can do so with:
msgunfmt fr.mo > fr.po
The decompiled file will not include comments or fuzzy strings, as these are never compiled in the MO file in the first place.