Flutter 中的国际化:如何从一个选择中获取所有密钥?

问题描述 投票:0回答:2

好吧,也许这个问题不是很精确。但是,我发现在 Flutter 中使用类似于 Android 的

<string-array>
进行翻译的唯一方法是使用
select
。例如,

"pronoun": "{gender, select, male{he} female{she} other{they}}",
"@pronoun": {
  "description": "A gendered message",
  "placeholders": {
    "gender": {
      "type": "String"
    }
  }
}

现在,如何获取用于创建此定义的键数组?在大写字母中,答案应该是

[male, female, other]

flutter dart internationalization flutter-intl
2个回答
0
投票

如果您查看本地化语言特定文件中生成的代码(即

localization_en.dart
),您将看到以下内容:

@override
String pronoun(String gender) {
  String _temp0 = intl.Intl.selectLogic(
    gender,
    {
      'male': 'he',
      'female': 'she',
      'other': 'they',
    },
  );
  return '$_temp0';
}

由于地图是直接在方法中生成并存储的。无需我进行任何进一步的研究:您没有外部方法可以获取它。

除非有本地化特定的构建器选项来生成它(查找文档),否则上述内容成立。


0
投票

您必须自己定义

enum

文档

It is possible to use a Dart enum as the choice and as the key in cases, 
but note that we will process this by truncating toString() of the enum and
using just the name part. We will do this for any class or strings that are 
passed, since we can't actually identify if something is an enum or not.

您的 dart 代码可能如下所示:

enum Gender{
  male,
  female,
  nonbinary;
}

var birthdaycake = Translations.of(context).birthdaycake(Gender.female.toString())

arb 文件:

 "birthdaycake": "{gender, select, male{his} female{her} nonbinary{their} other{please use the enum 'Gender'. this should not happen.}} Birthdaycake",
© www.soinside.com 2019 - 2024. All rights reserved.