gpt4 book ai didi

带有变量的 Flutter 本地化

转载 作者:行者123 更新时间:2023-12-04 08:08:10 30 4
gpt4 key购买 nike

我正在尝试通过关注 documentation 来本地化我的 Flutter 应用程序.
我想要实现的是,在动态构建小部件的同时,我想转换来自我的模型的数据。这是我迄今为止尝试过的

List.generate(services.length, (index) {
final Service service = services[index];
return Material(
borderRadius: BorderRadius.circular(10.0),
child: Text(
AppLocalizations.of(context).{{service.title}} // Here I wanted to translate the service title
),
);
}
我如何在 Flutter 中实现这一点?或任何其他可能的方法来翻译动态内容。

最佳答案

Flutter 本地化包不支持动态翻译。您可以尝试整合google translation API在你的应用程序中。但我坚信它应该是一个服务器端特性,并且 Flutter 客户端应该在你的模型中获取已经翻译的消息。例如,为了实现这一点,您可以使用带有设备区域设置的 http-headers 来告诉服务器客户端的语言。
您还需要根据 this guide 为国际消息使用参数。 .这是 AppLocalizations 中带有 String 参数的消息示例:

  String someLocalizedString(String argument) => Intl.message(
'Localized part - $argument',
name: 'someLocalizedString',
locale: localeName,
args: [argument],
);
里面的这个字符串 .arb文件:
  "someLocalizedString": "Localized part - {argument}",
"@someLocalizedString": {
"type": "text",
"placeholders": {
"argument": {}
}
}

关于带有变量的 Flutter 本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66114113/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com