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

Enhance Documentation and Add Comprehensive Pluralization Tests #730

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
212 changes: 212 additions & 0 deletions test/easy_localization_plural_rules_unit_test.dart
Original file line number Diff line number Diff line change
@@ -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'),
bw-flagship marked this conversation as resolved.
Show resolved Hide resolved
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');
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incorrect locale in Arabic test case

There appears to be a bug in the test setup. The test is titled "Arabic all cases" but uses the English locale ('en') instead of Arabic ('ar').

       Localization.load(
-        const Locale('en'),
+        const Locale('ar'),
         translations: r.translations,
         fallbackTranslations: r.fallbackTranslations,
         ignorePluralRules: true,
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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');
});
test('Arabic all cases (with ignorePluralRules) | using `_pluralCaseFallback`', () async {
Localization.load(
const Locale('ar'),
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');
});

});
}