Dart Intl如何省略名称和参数

时间:2019-03-21 15:53:50

标签: dart flutter dart-intl

我试图省略将名称和参数传递到Intl.message中。 Intl文档建议,提供了一个转换器,它将自动为您插入这些参数。

所以我在pubspec.yaml中添加了以下内容:

dev_dependencies:
  intl_translation: ^0.17.3
transformers:
- intl_translation:
$include: lib/localization.dart

在我的localization.dart中,我有以下方法:

class AppLocalizations {
  ...
  String greetingMessage(String name) => Intl.message(
    "Hello $name!",
    desc: "Greet the user as they first open the application",
    examples: const {'name': "Emily"});
}

运行flutter pub pub run intl_translation:extract_to_arb --output-dir=strings lib/localization.dart时出现以下错误:

<Intl.message("Hello $name!", desc: "Greet the user as they first open the application", examples: const {'name' : "Emily"})>
    reason: The 'args' argument for Intl.message must be specified for messages with parameters. Consider using rewrite_intl_messages.dart

如何启用该转换器?

1 个答案:

答案 0 :(得分:0)

您的错误:

原因:必须为Intl.message指定'args'参数 带参数的消息。考虑使用rewrite_intl_messages.dart

因为您的Intl.message()中包含一个自变量name,所以您还必须添加:args: [name]

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

请参阅:https://api.flutter.dev/flutter/intl/Intl/message.html

args是一个包含封闭函数参数的列表。 如果没有参数,则可以省略args。

但是您确实有一个参数,因此不能忽略它。