diff --git a/pkgs/intl4x/CHANGELOG.md b/pkgs/intl4x/CHANGELOG.md index 144b4199..9ae7f725 100644 --- a/pkgs/intl4x/CHANGELOG.md +++ b/pkgs/intl4x/CHANGELOG.md @@ -2,6 +2,7 @@ - Add ICU4X support for number formatting. - Add resource identifier annotations. +- Add ICU4X support for plural rules. ## 0.8.1 diff --git a/pkgs/intl4x/lib/src/plural_rules/plural_rules_4x.dart b/pkgs/intl4x/lib/src/plural_rules/plural_rules_4x.dart index b1852795..c78663b5 100644 --- a/pkgs/intl4x/lib/src/plural_rules/plural_rules_4x.dart +++ b/pkgs/intl4x/lib/src/plural_rules/plural_rules_4x.dart @@ -2,9 +2,11 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import '../../intl4x.dart'; +import '../bindings/lib.g.dart' as icu; import '../data.dart'; -import '../locale/locale.dart'; -import 'plural_rules.dart'; +import '../data_4x.dart'; +import '../locale/locale_4x.dart'; import 'plural_rules_impl.dart'; import 'plural_rules_options.dart'; @@ -16,10 +18,28 @@ PluralRulesImpl getPluralSelect4X( PluralRules4X(locale, data, options); class PluralRules4X extends PluralRulesImpl { - PluralRules4X(super.locale, Data data, super.options); + final icu.PluralRules _pluralRules; + + PluralRules4X(super.locale, Data data, super.options) + : _pluralRules = switch (options.type) { + Type.cardinal => icu.PluralRules.cardinal(data.to4X(), locale.to4X()), + Type.ordinal => icu.PluralRules.ordinal(data.to4X(), locale.to4X()), + }; @override PluralCategory selectImpl(num number) { - throw UnimplementedError('Insert diplomat bindings here'); + final operand = icu.PluralOperands.fromString(number.toString()); + return _pluralRules.categoryFor(operand).toDart(); } } + +extension on icu.PluralCategory { + PluralCategory toDart() => switch (this) { + icu.PluralCategory.zero => PluralCategory.zero, + icu.PluralCategory.one => PluralCategory.one, + icu.PluralCategory.two => PluralCategory.two, + icu.PluralCategory.few => PluralCategory.few, + icu.PluralCategory.many => PluralCategory.many, + icu.PluralCategory.other => PluralCategory.other, + }; +} diff --git a/pkgs/intl4x/test/ecma/plural_rules_test.dart b/pkgs/intl4x/test/plural_rules_test.dart similarity index 97% rename from pkgs/intl4x/test/ecma/plural_rules_test.dart rename to pkgs/intl4x/test/plural_rules_test.dart index 8e9e46a1..05e1efe7 100644 --- a/pkgs/intl4x/test/ecma/plural_rules_test.dart +++ b/pkgs/intl4x/test/plural_rules_test.dart @@ -1,15 +1,11 @@ // Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. - -@TestOn('browser') -library; - import 'package:intl4x/intl4x.dart'; import 'package:intl4x/plural_rules.dart'; import 'package:test/test.dart'; -import '../utils.dart'; +import 'utils.dart'; void main() { testWithFormatting('en-US simple', () {