From 8c46f8d04289930d8c017dbdb607820b5e9c7a6f Mon Sep 17 00:00:00 2001 From: Mostafa Alazhariy <87443208+MAlazhariy@users.noreply.github.com> Date: Mon, 25 Nov 2024 12:13:34 +0200 Subject: [PATCH 1/3] [update] Configuring Plural Rules to README --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index c74f5ac4..d8822c3c 100644 --- a/README.md +++ b/README.md @@ -317,6 +317,29 @@ var money = plural('money_named_args', 10.23, namedArgs: {'name': 'Jane', 'money var money = plural('money_named_args', 10.23, namedArgs: {'name': 'Jane'}, name: 'money') // output: Jane has 10.23 dollars ``` +### ⚙️ Configuring Plural Rules with `ignorePluralRules` + +In some languages, pluralization is simple and only involves using zero, one, two, and other forms, without needing to handle the `few` or `many` categories. +By default, `easy_localization` ignores the `few` and `many` plural forms and uses just the zero, one, two, and other forms. + +If you want to enable the handling of the `few` and `many` plural categories for specific languages, you can configure the `ignorePluralRules` flag to `false` in the `EasyLocalization` initialization. + +Here’s how to configure it: + +```dart +EasyLocalization( + ignorePluralRules: false, // Set this line to false to enable 'few' and 'many' plural categories + supportedLocales: [Locale('en', 'US'), Locale('de', 'DE')], + path: 'assets/translations', + fallbackLocale: Locale('en', 'US'), + child: MyApp() +) +``` + +Setting `ignorePluralRules: false` will enable the `few` and `many` plural categories, allowing your translations to handle all plural forms, including `few` and `many`, for supported languages. + + + ### 🔥 Linked translations: If there's a translation key that will always have the same concrete text as another one you can just link to it. To link to another translation key, all you have to do is to prefix its contents with an `@:` sign followed by the full name of the translation key including the namespace you want to link to. From 4b69f4bd00cba73e8c2b79dd28387dfeddeaa413 Mon Sep 17 00:00:00 2001 From: Mostafa Alazhariy <87443208+MAlazhariy@users.noreply.github.com> Date: Mon, 25 Nov 2024 12:15:41 +0200 Subject: [PATCH 2/3] [test] plural rules with 3 languages with `ignorePluralRules` in different cases - English - Russian - Arabic --- ...y_localization_plural_rules_unit_test.dart | 212 ++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 test/easy_localization_plural_rules_unit_test.dart diff --git a/test/easy_localization_plural_rules_unit_test.dart b/test/easy_localization_plural_rules_unit_test.dart new file mode 100644 index 00000000..6b0cf556 --- /dev/null +++ b/test/easy_localization_plural_rules_unit_test.dart @@ -0,0 +1,212 @@ +import 'dart:developer'; + +import 'package:easy_localization/src/easy_localization_controller.dart'; +import 'package:easy_localization/src/localization.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import 'utils/test_asset_loaders.dart'; + +void main() { + // Setup + var r = EasyLocalizationController( + forceLocale: const Locale('fb'), + supportedLocales: [const Locale('en'), const Locale('ar'), const Locale('ru'), const Locale('fb')], + fallbackLocale: const Locale('fb'), + path: 'path', + useOnlyLangCode: true, + useFallbackTranslations: true, + onLoadError: (FlutterError e) { + log(e.toString()); + }, + saveLocale: false, + assetLoader: const JsonAssetLoader()); + + setUpAll(() async { + await r.loadTranslations(); + }); + + group('English plural', () { + test('English one (no ignorePluralRules)', () async { + Localization.load( + const Locale('en'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ignorePluralRules: false, + ); + + expect(Localization.instance.plural('hat', 1), 'one hat'); + }); + test('English all cases (no ignorePluralRules)', () async { + Localization.load( + const Locale('en'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ignorePluralRules: false, + ); + + expect(Localization.instance.plural('hat', 1), 'one hat'); + expect(Localization.instance.plural('hat', 0), 'other hats'); + expect(Localization.instance.plural('hat', 2), 'other hats'); + expect(Localization.instance.plural('hat', 3), 'other hats'); + expect(Localization.instance.plural('hat', 4), 'other hats'); + expect(Localization.instance.plural('hat', 11), 'other hats'); + expect(Localization.instance.plural('hat', 101), 'other hats'); + expect(Localization.instance.plural('hat', 111), 'other hats'); + }); + test('English all cases (with ignorePluralRules) | using `_pluralCaseFallback`', () async { + Localization.load( + const Locale('en'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ignorePluralRules: true, + ); + + expect(Localization.instance.plural('hat', 0), 'no hats'); + expect(Localization.instance.plural('hat', 1), 'one hat'); + expect(Localization.instance.plural('hat', 2), 'two hats'); + expect(Localization.instance.plural('hat', 3), 'other hats'); + expect(Localization.instance.plural('hat', 4), 'other hats'); + expect(Localization.instance.plural('hat', 11), 'other hats'); + expect(Localization.instance.plural('hat', 101), 'other hats'); + expect(Localization.instance.plural('hat', 111), 'other hats'); + }); + }); + + group('Russian plural', () { + test('Russian one', () async { + Localization.load( + const Locale('ru'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ); + expect(Localization.instance.plural('hat', 1), 'one hat'); + }); + test('Russian few (no ignorePluralRules)', () async { + Localization.load( + const Locale('ru'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ignorePluralRules: false, + ); + expect(Localization.instance.plural('hat', 2), 'few hats'); + expect(Localization.instance.plural('hat', 3), 'few hats'); + }); + test('Russian few (with ignorePluralRules)', () async { + Localization.load( + const Locale('ru'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ignorePluralRules: true, + ); + expect(Localization.instance.plural('hat', 2), 'two hats'); + expect(Localization.instance.plural('hat', 3), 'other hats'); + }); + test('Russian many (no ignorePluralRules)', () async { + Localization.load( + const Locale('ru'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ignorePluralRules: false, + ); + expect(Localization.instance.plural('hat', 0), 'many hats'); + expect(Localization.instance.plural('hat', 5), 'many hats'); + expect(Localization.instance.plural('hat', 101), 'one hat'); + }); + test('Russian many (with ignorePluralRules)', () async { + Localization.load( + const Locale('ru'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ignorePluralRules: true, + ); + expect(Localization.instance.plural('hat', 0), 'no hats'); + expect(Localization.instance.plural('hat', 5), 'other hats'); + expect(Localization.instance.plural('hat', 101), 'other hats'); + }); + }); + + group("Arabic plural", () { + test('Arabic few (no ignorePluralRules)', () async { + Localization.load( + const Locale('ar'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ignorePluralRules: false, + ); + expect(Localization.instance.plural('hat', 2), 'two hats'); + expect(Localization.instance.plural('hat', 10), 'few hats'); + expect(Localization.instance.plural('hat', 110), 'few hats'); + }); + test('Arabic few (with ignorePluralRules)', () async { + Localization.load( + const Locale('ar'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ignorePluralRules: true, + ); + expect(Localization.instance.plural('hat', 2), 'two hats'); + expect(Localization.instance.plural('hat', 3), 'other hats'); + expect(Localization.instance.plural('hat', 10), 'other hats'); + expect(Localization.instance.plural('hat', 110), 'other hats'); + }); + test('Arabic many (no ignorePluralRules)', () async { + Localization.load( + const Locale('ar'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ignorePluralRules: false, + ); + + expect(Localization.instance.plural('hat', 11), 'many hats'); + expect(Localization.instance.plural('hat', 111), 'many hats'); + }); + test('Arabic many (with ignorePluralRules)', () async { + Localization.load( + const Locale('ar'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ignorePluralRules: true, + ); + expect(Localization.instance.plural('hat', 10), 'other hats'); + expect(Localization.instance.plural('hat', 11), 'other hats'); + expect(Localization.instance.plural('hat', 111), 'other hats'); + }); + test('Arabic all cases (no ignorePluralRules)', () async { + Localization.load( + const Locale('ar'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ignorePluralRules: false, + ); + + expect(Localization.instance.plural('hat', 0), 'no hats'); + expect(Localization.instance.plural('hat', 1), 'one hat'); + expect(Localization.instance.plural('hat', 2), 'two hats'); + expect(Localization.instance.plural('hat', 3), 'few hats'); + expect(Localization.instance.plural('hat', 4), 'few hats'); + expect(Localization.instance.plural('hat', 11), 'many hats'); + expect(Localization.instance.plural('hat', 101), 'other hats'); + expect(Localization.instance.plural('hat', 111), 'many hats'); + expect(Localization.instance.plural('hat', 103), 'few hats'); + }); + test('Arabic all cases (with ignorePluralRules) | using `_pluralCaseFallback`', () async { + Localization.load( + const Locale('en'), + translations: r.translations, + fallbackTranslations: r.fallbackTranslations, + ignorePluralRules: true, + ); + + expect(Localization.instance.plural('hat', 0), 'no hats'); + expect(Localization.instance.plural('hat', 1), 'one hat'); + expect(Localization.instance.plural('hat', 2), 'two hats'); + expect(Localization.instance.plural('hat', 3), 'other hats'); + expect(Localization.instance.plural('hat', 4), 'other hats'); + expect(Localization.instance.plural('hat', 11), 'other hats'); + expect(Localization.instance.plural('hat', 101), 'other hats'); + expect(Localization.instance.plural('hat', 111), 'other hats'); + expect(Localization.instance.plural('hat', 103), 'other hats'); + }); + }); +} From c713a1bbbe055e76438e0ee1e32683f95bf6f469 Mon Sep 17 00:00:00 2001 From: Mostafa Alazhariy <87443208+MAlazhariy@users.noreply.github.com> Date: Mon, 25 Nov 2024 17:45:04 +0200 Subject: [PATCH 3/3] [fix] test | change en to ar in `Arabic all cases (with ignorePluralRules) | using `_pluralCaseFallback`` --- test/easy_localization_plural_rules_unit_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/easy_localization_plural_rules_unit_test.dart b/test/easy_localization_plural_rules_unit_test.dart index 6b0cf556..9c9d2a0a 100644 --- a/test/easy_localization_plural_rules_unit_test.dart +++ b/test/easy_localization_plural_rules_unit_test.dart @@ -192,7 +192,7 @@ void main() { }); test('Arabic all cases (with ignorePluralRules) | using `_pluralCaseFallback`', () async { Localization.load( - const Locale('en'), + const Locale('ar'), translations: r.translations, fallbackTranslations: r.fallbackTranslations, ignorePluralRules: true,