dart-lang / i18n

A general mono-repo for Dart i18n and l10n packages.
BSD 3-Clause "New" or "Revised" License
61 stars 36 forks source link

Add support for ordinals in `MessageFormat` #784

Open Reprevise opened 7 months ago

Reprevise commented 7 months ago

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 commented 7 months ago

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
}
mosuem commented 7 months ago

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 commented 7 months ago

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.