feat: add runtime i18n API with manifest translations
This commit is contained in:
@@ -71,6 +71,69 @@ class LuaDardoScriptEngine implements ScriptEngine {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void setTranslations({
|
||||
required String locale,
|
||||
required Map<String, Map<String, String>> translations,
|
||||
}) {
|
||||
_lua.getGlobal('runtime');
|
||||
if (_lua.isNil(-1)) {
|
||||
_lua.pop(1);
|
||||
return;
|
||||
}
|
||||
|
||||
_lua.newTable();
|
||||
|
||||
_lua.pushString(locale);
|
||||
_lua.setField(-2, 'locale');
|
||||
|
||||
_lua.pushDartFunction((LuaState lua) {
|
||||
lua.pushString(locale);
|
||||
return 1;
|
||||
});
|
||||
_lua.setField(-2, 'get_locale');
|
||||
|
||||
_lua.pushDartFunction((LuaState lua) {
|
||||
final key = lua.toStr(1);
|
||||
if (key == null || key.isEmpty) {
|
||||
if (lua.getTop() >= 2 && !lua.isNil(2)) {
|
||||
lua.pushValue(2);
|
||||
} else {
|
||||
lua.pushString(key ?? '');
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
final localeDict = translations[locale];
|
||||
final value = localeDict?[key];
|
||||
if (value != null) {
|
||||
lua.pushString(value);
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (final entry in translations.entries) {
|
||||
if (entry.key != locale) {
|
||||
final fallbackValue = entry.value[key];
|
||||
if (fallbackValue != null) {
|
||||
lua.pushString(fallbackValue);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (lua.getTop() >= 2 && !lua.isNil(2)) {
|
||||
lua.pushValue(2);
|
||||
} else {
|
||||
lua.pushString(key);
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
_lua.setField(-2, 't');
|
||||
|
||||
_lua.setField(-2, 'i18n');
|
||||
_lua.pop(1);
|
||||
}
|
||||
|
||||
@override
|
||||
bool smokeTest(Map<String, Object?> context) {
|
||||
_lua.getGlobal('smoke_test');
|
||||
|
||||
Reference in New Issue
Block a user