國際化指南
介紹
LeviLamina提供了一個簡單的i18n類,用於為自身和模組提供多語言支持。
使用
加載語言文件
- 引用頭文件
ll/api/i18n/I18n.h。 - 在模組的
load方法中調用ll::i18n::getInstance().load方法,傳入語言文件所在路徑,一般為文件夾,也可以使用單個json文件。LeviLamina提供了Mod::getLangDir方法供開發者獲取規範的語言文件路徑。
C++
#include "ll/api/i18n/I18n.h"
bool MyMod::load() {
if (auto res = ll::i18n::getInstance().load(getSelf().getLangDir()); !res) {
getSelf().getLogger().error("i18n load failed");
res.error().log(getSelf().getLogger());
}
return true;
}語言文件路徑示例:
- lang/
- de.json
- en.json
- fr.json
- id.json
- it.json
- ja.json
- ko.json
- pt_BR.json
- ru.json
- th.json
- tr.json
- vi.json
- zh_CN.json
- zh_TW.json
語言文件內容示例:
JSON
{
"The mod cannot be loaded": "无法加载该模组",
"The mod cannot be unloaded": "无法卸载该模组",
"The mod cannot be enabled": "无法启用该模组",
"The mod cannot be disabled": "无法禁用该模组",
"Mod {0} already loaded": "{0} 已加载",
"Unrecognized mod type: {0}": "无法识别的模组类型:{0}",
"Mod {0} not found": "找不到模组 {0}",
"crash the server": "崩溃服务器",
"Query memory stats": "查询内存统计"
}使用I18n來翻譯文本
- 引用頭文件
ll/api/i18n/I18n.h。 - 在源文件中使用
using namespace ll::i18n_literals指令。 - 在字符串後使用
_tr()字面量。
C++
#include "ll/api/i18n/I18n.h"
using namespace ll::i18n_literals;
bool MyMod::enable() {
getSelf().getLogger().info("Hello, world!"_tr());
getSelf().getLogger().info("This is a text with parameter: {0}"_tr("parameter"));
return true;
}