Flutter 国际化 - 动态字符串

Flutter internationalization - Dynamic strings

我正在使用 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

它工作正常。

但是,我需要创建 "dynamic" 个字符串。例如:

"Hi, {$name}"

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

可以使用这个国际包吗?

intl 包的 README 解释了这个例子 https://github.com/dart-lang/intl

The purpose of wrapping the message in a function is to allow it to have parameters which can be used in the result. The message string is allowed to use a restricted form of Dart string interpolation, where only the function's parameters can be used, and only in simple expressions. Local variables cannot be used, and neither can expressions with curly braces. Only the message string can have interpolation. The name, desc, args, and examples must be literals and not contain interpolations. Only the args parameter can refer to variables, and it should list exactly the function parameters. If you are passing numbers or dates and you want them formatted, you must do the formatting outside the function and pass the formatted string into the message.

greetingMessage(name) => Intl.message(
      "Hello $name!",
      name: "greetingMessage",
      args: [name],
      desc: "Greet the user as they first open the application",
      examples: const {'name': "Emily"});
  print(greetingMessage('Dan'));

在本节下方,解释了更复杂的示例,这些示例也涉及复数和性别。

为了在您的翻译中使用占位符,您需要:

  • 将该占位符添加为 getter 参数
  • 在翻译中提及带有 $ 前缀的占位符(即 $name
  • 调用时在args列表中添加占位符Intl.message

所以一个完整的例子是这样的:

greetingMessage(name) => Intl.message(
  "Hello $name!",
  name: 'greetingMessage',
  args: [name]
);

如果您遵循 official internationalization docs 并在 .arb 文件中指定所有短语,您可以这样设置参数:

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

当您编译代码时,将为您生成如下函数,并附有一个漂亮的文档块来为您的 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"))

关注此 link。完成所有步骤后,在 .arb 文件中进行以下更改:

{  
  "title": "App Title",
  "helloWorld": "{name1} and {name2} must be different",
  "@helloWorld": {
        "description": "The conventional newborn programmer greeting",
        "placeholders": {
                    "name1": {
                        "type": "String"
                    },
                    "name2": {
                        "type": "String"
                    }
                }
      },
  "appInfo":  "Information about your app",
 }