gpt4 book ai didi

internationalization - Flutter 国际化 - 动态字符串

转载 作者:IT老高 更新时间:2023-10-28 12:37:47 68 4
gpt4 key购买 nike

我正在使用 intl 包将我的应用程序翻译成西类牙语。

locales.dart

class AppLocale {
...
String get folder => Intl.message("Folder", name: 'folder');
...
}

messages_es.dart

class MessageLookup extends MessageLookupByLibrary {
get localeName => 'es';

final messages = _notInlinedMessages(_notInlinedMessages);
static _notInlinedMessages(_) => <String, Function> {
"folder": MessageLookupByLibrary.simpleMessage("Carpeta"),
};
}

我使用以下代码调用它:

AppLocale.of(context).folder

它工作正常。

但是,我需要创建“动态”字符串。例如:

“嗨,{$name}”

然后我会调用这个字符串,将这个“名称”作为参数传递,或者类似的东西。用西类牙语翻译为“Hola,{$name}”。

可以使用这个国际包吗?

最佳答案

如果您关注 official internationalization docs并在 .arb 文件中指定所有短语,您可以执行以下参数:

{
"greeting": "Hi, {name}!",
"@greeting": {
"description": "Greet the user by their name.",
"placeholders": {
"name": {
"type": "String",
"example": "Jane"
}
}
}
}

当你编译你的代码时,会为你生成一个类似下面的函数,并带有一个很好的 docbloc 来支持你的 IDE 工具提示:

  /// Greet the user by their name.
///
/// In en, this message translates to:
/// **'Hi, {name}!'**
String greeting(String name);

所以你可以像这样使用它:

Text(AppLocalizations.of(context)!.greeting("Koos"))

关于internationalization - Flutter 国际化 - 动态字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52278035/

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