Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ordinals in NumberFormat #484

Open
putnokiabel opened this issue Apr 2, 2023 · 5 comments
Open

Add support for ordinals in NumberFormat #484

putnokiabel opened this issue Apr 2, 2023 · 5 comments
Labels
P3 A lower priority bug or feature request package:intl type-enhancement A request for a change that isn't a bug

Comments

@putnokiabel
Copy link

Is your feature request related to a problem? Please describe.
I want to format a number with an ordinal suffix (e.g. "1st", "2nd", "3rd", "205th") in a localized way, as different languages have different ordinal suffixes.

Describe the solution you'd like
I would like a NumberFormat that formats a number with an ordinal suffix, e.g.

factory NumberFormat.ordinal({String? locale});

, similar to NumberFormat.compact.

Describe alternatives you've considered
I'm currently using a custom solution, however this obviously only works in English and is not localized.

extension Ordinals on int {
  String get ordinal {
    switch (this % 10) {
      case 1:
        return '${this}st';
      case 2:
        return '${this}nd';
      case 3:
        return '${this}rd';
      default:
        return '${this}th';
    }
  }
}

I've also considered using native libraries (as all platforms supported by Flutter have native support for this), but this would mean that:

  1. I have to maintain a native library for this functionality
  2. It would likely mean that formatting the numbers needs to happen asynchronously (since we need to wait on a PlatformChannel) which is obviously not ideal if you just want to display a string.

Additional context
This issue is not a duplicate of #163, as this functionality does not inherently involve formatting dates, it's purely related to formatting numbers, and should therefore likely be an addition to NumberFormat.

@mosuem mosuem added type-enhancement A request for a change that isn't a bug P3 A lower priority bug or feature request labels Apr 3, 2023
@mosuem
Copy link
Member

mosuem commented Apr 3, 2023

Currently, there is no support for ordinal plural rules in this package, which differ from the cardinal plural rules as described here. This would certainly be a nice addition, but would also increase code size.

Building a formatter on top of that would be then be easy, see the JS documentation. If we would actually want a separate formatter for this, again having to add the cases for different locales, increasing code size, would have to be discussed.

@mosuem mosuem transferred this issue from dart-archive/intl Apr 18, 2023
@timsneath
Copy link

Prior art exists in other languages, c.f. Swift: https://developer.apple.com/documentation/foundation/numberformatter/style/ordinal

@mateusfccp
Copy link

mateusfccp commented Mar 31, 2024

For now, we have the option of using https://pub.dev/packages/ordinal_formatter, but it only supports a few locales, and I think this actually belongs to this package.

@putnokiabel
Copy link
Author

For now, we have the option of using https://pub.dev/packages/ordinal_formatter, but it only supports a few locales, and I think this actually belongs to this package.

Agreed! Thanks for mentioning an alternative.
IMO, since this is basic functionality that is included in the standard libraries of most languages (that are used for UI), and is 100% synchronous logic (no hardware or system calls needed), having to call native code asynchronously using PlatformChannel and waiting for the answer is "obviously" not the right way and should indeed belong to this package.

@mosuem
Copy link
Member

mosuem commented Apr 2, 2024

For the new package:intl4x, the implementation of this feature would require tc39/ecma402#494 and unicode-org/icu4x#595 to be resolved.

I agree that this seems like a good feature to have in an i18n library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 A lower priority bug or feature request package:intl type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants