翻译、国际化
翻译
4.0起新增 .gd、.tscn、csv、json转POT文件的插件 - POT Generation" in "Localization" tab in "Project Settings" menu.
简单翻译:
x.csv 逗号分隔、双引号转义,竖向指定语言:
keys,en,zh,zh_TW
GREET,"Hi, friend!","欢迎",歡迎
QUOTE,"""Hello"" said the man.","""欢迎"" 是个好词。",「歡迎」是个好词。
IDE会自动识别x.csv并生成x.zh_TW.translation等,使用前应将这些*.translation添加至“本地化”->“翻译”。
编程方式翻译 - ((Godot.GodotObject)this).Tr("TR_X_BUTTON_TEXT");
应用名 下方 name_localized 可根据操作系统区域语言进行逐个翻译;v3.x则可以使用 application/config/name_zh_TW 方式定义; 等同 project.godot :
[application]
config/name="GameName"
config/name_en="Game Name"
config/name_zh="游戏名"
config/name_zh_TW="遊戲名"
config/name_en_US="Game Name"
config/name_zh_HK="遊戲名"
Godot翻译可采用 GNU gettext 生成*.po方式 和 传统的*.csv列对应国别的方式;找不到匹配翻译会降级至en语言环境。
注意 - Poedit保存会生成x.mo文件;或 msgfmt x.po --no-hash -o x.mo
godot 3.5+起支持读取mo翻译二进制文件
国别 - https://docs.godotengine.org/en/stable/tutorials/i18n/locales.html
软件生成*.pot模板文件(zh_CN) - https://poedit.net/download
命令行生成*.po - https://www.gnu.org/software/gettext/#downloading
自*.mo还原回*.po命令:msgunfmt fr.mo > fr.po
抽取.gd, .tres, .tscn翻译 - https://github.com/remram44/pybabel-godot
生成翻译文件:手动创建模板文件 x.pot
# 井号注释和每行两边空格及换行均为可选,但前两行属性值必须为空。
msgid ""
msgstr ""
msgid "MAIN_UI_HI"
msgstr ""
# 通过Poedit生成po文件(添加 Language: fr 头信息供Godot识别)
# 或 命令行生成po(指定了--output-file才能覆盖) - C:\Progra~1\gettext-iconv\bin\msginit --no-translator --input=x.pot --locale=fr
# 或 合并翻译过的和未翻译过的 - msgmerge --update --backup=none fr.po messages.pot
# Project Settings > Localization tab > Translations > Add… .po file
# IDE text=msgid 或 n.SetText("msgid") 或 GD.Print(Tr("MAIN_UI_HI"));
操作系统判断 - OS.HasFeature("mobile"); // pc web Android
法语国家化测试- godot --language fr