flutterdartflutter-intl

Flutter Intl - Use of double type in ARB plural translations


Been moving over to Flutter 2, and in process moving our translations directly into .arb format, as now suggested. However hitting an issue where the generated l10n translation files can't understand when a type should be double and instead uses int, which causes type errors when used in code. There doesn't seem to be any way to specify that a particular placeholder substitution would be a double rather than an int.

ARB Translations File

"hours": "{hours,plural, =1{Hour}other{Hours}}",
"@hours": {
  "placeholders": {
    "hours": {}
  }
},

Tried adding type information to the placeholder, but seems to have little impact on generation (using flutter gen-l10n script)

"hours": "{hours,plural, =1{Hour}other{Hours}}",
"@hours": {
  "placeholders": {
    "hours": {
      "type: "double" <========== THIS SEEMS TO HAVE NO IMPACT (and/or may not be valid)
    }
  }
},

Generated l10n translations.dart

/// In en, this message translates to:
/// **'{hours,plural, =1{Hour}other{Hours}}'**
String hours(int hours); <========== THIS NEEDS TO BE A DOUBLE

Code usage

AppLocalizations.of(context)!.hours(1.0)

In the previous method of using the Intl library (which is still an option, but the ARB generation tools are stale and/or multi-staged, and so attempting to move away), this was fairly easy as we controlled the types

String HOURS([double hours = 0]) => Intl.plural(
   hours,
   one: 'Hour',
   other: 'Hours',
   name: 'HOURS',
   args: <Object>[hours],
   locale: localeName,
);

We could of course have multiple strings in our translations, and manually handle the pluralisation, but given the power of plural in intl, it feels like this should be possible? Any advice?


Solution

  • When you need a placeholder type to be double, you must also add the format parameter. Some possible values: "compact", "currency", "decimalPattern" (the most general).

    You can find more of them here: https://localizely.com/flutter-arb/

    Sample localization:

    "validation_errors_min_value": "The value must be greater than {min}",
    "@validation_errors_min_value": {
        "placeholders": {
            "min": {
                "type": "double",
                "format": "decimalPattern"
            }
        }
    }