Skip to content

Commit

Permalink
Add support for plural rules from ICU4X (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
mosuem authored Feb 13, 2024
1 parent d400bbc commit 709bdcd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions pkgs/intl4x/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Add ICU4X support for number formatting.
- Add resource identifier annotations.
- Add ICU4X support for plural rules.

## 0.8.1

Expand Down
28 changes: 24 additions & 4 deletions pkgs/intl4x/lib/src/plural_rules/plural_rules_4x.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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,
};
}
Original file line number Diff line number Diff line change
@@ -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', () {
Expand Down

0 comments on commit 709bdcd

Please sign in to comment.