localizely/flutter-intl-vscode

Can't add an int as a key for Custom selects

db-dblank opened this issue · 4 comments

I'm trying to add this to my ARB file:

"weekOfMonthType": "{weekOfMonth, select, 1{First} 2{Second} 3{Third} 4{Fourth} other{Last}}",

But getting this error:

INFO: Generating localization files for the 'flutter_app' project...
ERROR: The 'weekOfMonthType' key will be ignored due to parsing errors.
exit code 0

Expected outcome would be:

static String m0(weekOfMonth) => "${Intl.select(weekOfMonth, {
            '1': 'First',
            '2': 'Second',
            '3': 'Third',
            '4': 'Fourth',
            'other': 'Last',
          })}";

And to use like this:

S.of(context).weekOfMonthType(weekOfMonth.toString());

Hi @db-dblank,

Thanks for reporting this!

I must agree that it would be great if ICU Select messages would allow integers (e.g., handling weeks of the month, error codes from the API, etc.). However, I'm not sure if such change would be in compliance with the ICU specification (icu-project website, intl_translation package impl.).

Maybe, such messages can be used together with Dart enums?

The en.arb file:

"weekOfMonthType": "{weekOfMonth, select, first{First} second{Second} third{Third} fourth{Fourth} other{Last}}"

The l10n.dart file:

/// `{weekOfMonth, select, first{First} second{Second} third{Third} fourth{Fourth} other{Last}}`
String weekOfMonthType(Object weekOfMonth) {
  return Intl.select(
    weekOfMonth,
    {
      'first': 'First',
      'second': 'Second',
      'third': 'Third',
      'fourth': 'Fourth',
      'other': 'Last',
    },
    name: 'weekOfMonthType',
    desc: '',
    args: [weekOfMonth],
  );
}

The usage:

enum WeekOfMonth { first, second, third, fourth, last }

WeekOfMonth getWeekOfMonthEnum(index) =>
    (index > 0 && index <= WeekOfMonth.values.length) ? WeekOfMonth.values[index - 1] : WeekOfMonth.last;

...

S.of(context).weekOfMonthType(getWeekOfMonthEnum(index))

If you agree, I suggest leaving this ticket open until we find the solution (to better see the demand/upvotes for such functionality).

Hey @lzoran,

Nice approach!

My current approach is to prepend the integer with the letter n.

The en.arb file:

"weekOfMonthType": "{weekOfMonth, select, n1{First} n2{Second} n3{Third} n4{Fourth} other{Last}}",

The l10n.dart file:

/// `{weekOfMonth, select, n1{First} n2{Second} n3{Third} n4{Fourth} other{Last}}`
String weekOfMonthType(Object weekOfMonth) {
  return Intl.select(
    weekOfMonth,
    {
      'n1': 'First',
      'n2': 'Second',
      'n3': 'Third',
      'n4': 'Fourth',
      'other': 'Last',
    },
    name: 'weekOfMonthType',
    desc: '',
    args: [weekOfMonth],
  );
}

The usage:

S.of(context).weekOfMonthType('n$weekOfMonth');

I don't have access to the source code but the solution seems pretty easy. Would be just to handle integers as string instead of ignoring it.

The 'weekOfMonthType' key will be ignored due to parsing errors.

Sorry for the delayed response.

My current approach is to prepend the integer with the letter n.

It looks like a simpler solution. 👍

I don't have access to the source code but the solution seems pretty easy. Would be just to handle integers as string instead of ignoring it.

Yes, but unfortunately, that would diverge from the recommended way of usage (link). In case you really need such functionality, you can always fork the generator of the localization files (intl_utils package) and adjust it.

I suggest leaving this issue open, so we can easily monitor interest for such functionality. In case this attracts a significant number of upvotes, then consider extending the current implementation to support integers as well.

Closing this issue because it has been open for some time and without activity.