在 GetX 中使用 JSON 文件进行本地化
我正在尝试阅读翻译文件,例如。 en.json
用于 GetX 本地化。我尝试了以下代码但没有成功。有更好的方法吗?有人建议将 auto_localize 与 GetX 一起使用,但如果我可以在没有额外软件包的情况下继续进行,我将非常
Map<String, Map<String, String>> get keys => {
"ar": readJson("ar"),
"en": readJson("en"),
}
感激尝试使用以下函数 DebugPrint() 加载本地化信息
// Fetch content from the json file
Map<String, String> readJson(String languageCode) {
Map data = {};
rootBundle
.loadString('assets/translations/$languageCode.json')
.then((response) {
data = json.decode(response);
});
return data.map((key, value) {
return MapEntry(key, value.toString());
});
}
,显示文件已成功加载。但是,尝试显示加载的地图不起作用
I am trying to read translation files eg. en.json
to use with GetX localization. I tried the following code but no success. Is there any better way of doing it? Somebody suggested using auto_localize with GetX, but I'll really appreciate if I can proceed without an extra package
Map<String, Map<String, String>> get keys => {
"ar": readJson("ar"),
"en": readJson("en"),
}
I tried loading the localization information using the following function
// Fetch content from the json file
Map<String, String> readJson(String languageCode) {
Map data = {};
rootBundle
.loadString('assets/translations/$languageCode.json')
.then((response) {
data = json.decode(response);
});
return data.map((key, value) {
return MapEntry(key, value.toString());
});
}
DebugPrint() gets shows that the files were successfully loaded. However, trying to display the loaded Maps doesn't work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
readJson 应该是 Future,因此最后返回空 Map。
方式
The readJson should be Future and because of that the empty Map returned at the end.
The way i did