i18n icon indicating copy to clipboard operation
i18n copied to clipboard

Add support for ordinals in `MessageFormat`

Open Reprevise opened this issue 1 year ago • 3 comments

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like There appears to be parsing for selectordinal already, but it only works for "one" and I guess "other".

import 'package:intl/message_format.dart';

void main() {
  final msg = MessageFormat('{count, selectordinal, one {#st} two {#nd} few {#rd} other {#th}}');
  print(msg.format({"count": 1})); // 1st
  print(msg.format({"count": 2})); // 2th
  print(msg.format({"count": 3})); // 3th
  print(msg.format({"count": 4})); // 4th
}

Describe alternatives you've considered I could maintain my own ordinal method however it would be difficult to localize myself.

Reprevise avatar Jan 30 '24 21:01 Reprevise

The following works though. I'm confused as to why the text I posted originally doesn't. It also works without the = sign in front of it, where on sites like this, it requires the = sign prefix.

import 'package:intl/message_format.dart';

void main() {
  final msg = MessageFormat('{count, selectordinal, =1{#st} =2{#nd} =3{#rd} other {#th}}');
  print(msg.format({"count": 1})); // 1st
  print(msg.format({"count": 2})); // 2nd
  print(msg.format({"count": 3})); // 3rd
  print(msg.format({"count": 4})); // 4th
}

Reprevise avatar Jan 30 '24 22:01 Reprevise

Just a quick note without having taken a deeper look: The implementation is derived from the Closure one and should be based on the ICU MessageFormat, so that should be the reference to look at.

mosuem avatar Jan 31 '24 08:01 mosuem

But this class does not have ordinal support in general, see https://github.com/dart-lang/i18n/blob/316929f6ff4b72e22a52d5cb40bb620f429a8fa4/pkgs/intl/lib/message_format.dart#L770.

mosuem avatar Jan 31 '24 08:01 mosuem