From 27353aab4bf39f82419c3b3ff42da7747f31c495 Mon Sep 17 00:00:00 2001 From: Maurizio Pinotti Date: Tue, 7 May 2024 15:02:09 +0200 Subject: [PATCH] Rename forcePluralCaseFallback to ignorePluralRules and reverse logic Parameter forcePluralCaseFallback has been renamed to ignorePluralRules for clarity as suggested by bw-flagship. Also, the logic is now reversed (e.g. false will implement the old behavior and true will implement the new behavior). Finally, the default value is now false (old behavior). --- example/lib/main.dart | 2 +- lib/src/easy_localization_app.dart | 33 +++++++++++++++++------------- lib/src/localization.dart | 8 ++++---- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 73725046..5ea46500 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -23,7 +23,7 @@ void main() async { // startLocale: Locale('de', 'DE'), // saveLocale: false, // useOnlyLangCode: true, - forcePluralCaseFallback: true, + // ignorePluralRules: true, // optional assetLoader default used is RootBundleAssetLoader which uses flutter's assetloader // install easy_localization_loader for enable custom loaders diff --git a/lib/src/easy_localization_app.dart b/lib/src/easy_localization_app.dart index f6e5baab..c7fac7d5 100644 --- a/lib/src/easy_localization_app.dart +++ b/lib/src/easy_localization_app.dart @@ -63,20 +63,25 @@ class EasyLocalization extends StatefulWidget { /// ``` final bool useFallbackTranslationsForEmptyResources; - /// Force use of plural strings for all languages. This will still fallback - /// to "other" if the specific rule isn't defined. + /// Ignore usage of plural strings for languages that do not use plural rules. /// @Default value false /// Example: /// ``` - /// // Default behavior, will use "zero" rule for 0 only if the language - /// // is set to do so (e.g. for "lt" but not for "en"). - /// forcePluralCaseFallback: false - /// // Force using "zero" rule for 0 even if the language doesn't use it - /// // by default (e.g. "en"). If "zero" localization for that string + /// // Default behavior, use "zero" rule for 0 even if the language doesn't + /// // use it by default (e.g. "en"). If "zero" localization for that string /// // doesn't exist, "other" is still used as fallback. - /// forcePluralCaseFallback: true + /// // "nTimes": "{count, plural, =0{never} =1{once} other{{count} times}}" + /// // Text(AppLocalizations.of(context)!.nTimes(_counter)), + /// // will print "never, once, 2 times" for ALL languages. + /// ignorePluralRules: false + /// // Use "zero" rule for 0 only if the language is set to do so (e.g. for + /// "lt" but not for "en"). + /// // "nTimes": "{count, plural, =0{never} =1{once} other{{count} times}}" + /// // Text(AppLocalizations.of(context)!.nTimes(_counter)), + /// // will print "never, once, 2 times" ONLY for languages with plural rules. + /// ignorePluralRules: true /// ``` - final bool forcePluralCaseFallback; + final bool ignorePluralRules; /// Path to your folder with localization files. /// Example: @@ -132,7 +137,7 @@ class EasyLocalization extends StatefulWidget { this.useOnlyLangCode = false, this.useFallbackTranslations = false, this.useFallbackTranslationsForEmptyResources = false, - this.forcePluralCaseFallback = false, + this.ignorePluralRules = false, this.assetLoader = const RootBundleAssetLoader(), this.extraAssetLoaders, this.saveLocale = true, @@ -214,7 +219,7 @@ class _EasyLocalizationState extends State { supportedLocales: widget.supportedLocales, useFallbackTranslationsForEmptyResources: widget.useFallbackTranslationsForEmptyResources, - forcePluralCaseFallback: widget.forcePluralCaseFallback, + ignorePluralRules: widget.ignorePluralRules, ), ); } @@ -296,14 +301,14 @@ class _EasyLocalizationDelegate extends LocalizationsDelegate { final List? supportedLocales; final EasyLocalizationController? localizationController; final bool useFallbackTranslationsForEmptyResources; - final bool forcePluralCaseFallback; + final bool ignorePluralRules; /// * use only the lang code to generate i18n file path like en.json or ar.json // final bool useOnlyLangCode; _EasyLocalizationDelegate({ required this.useFallbackTranslationsForEmptyResources, - this.forcePluralCaseFallback = false, + this.ignorePluralRules = false, this.localizationController, this.supportedLocales, }) { @@ -326,7 +331,7 @@ class _EasyLocalizationDelegate extends LocalizationsDelegate { fallbackTranslations: localizationController!.fallbackTranslations, useFallbackTranslationsForEmptyResources: useFallbackTranslationsForEmptyResources, - forcePluralCaseFallback: forcePluralCaseFallback, + ignorePluralRules: ignorePluralRules, ); return Future.value(Localization.instance); } diff --git a/lib/src/localization.dart b/lib/src/localization.dart index 463bf816..ef8e0609 100644 --- a/lib/src/localization.dart +++ b/lib/src/localization.dart @@ -20,7 +20,7 @@ class Localization { }; bool _useFallbackTranslationsForEmptyResources = false; - bool _forcePluralCaseFallback = false; + bool _ignorePluralRules = false; Localization(); @@ -36,14 +36,14 @@ class Localization { Translations? translations, Translations? fallbackTranslations, bool useFallbackTranslationsForEmptyResources = false, - bool forcePluralCaseFallback = false, + bool ignorePluralRules = false, }) { instance._locale = locale; instance._translations = translations; instance._fallbackTranslations = fallbackTranslations; instance._useFallbackTranslationsForEmptyResources = useFallbackTranslationsForEmptyResources; - instance._forcePluralCaseFallback = forcePluralCaseFallback; + instance._ignorePluralRules = ignorePluralRules; return translations == null ? false : true; } @@ -119,7 +119,7 @@ class Localization { } static PluralRule? _pluralRule(String? locale, num howMany) { - if (instance._forcePluralCaseFallback) { + if (!instance._ignorePluralRules) { return () => _pluralCaseFallback(howMany); } startRuleEvaluation(howMany);