Skip to content

Commit

Permalink
Rename forcePluralCaseFallback to ignorePluralRules and reverse logic
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
mauriziopinotti committed May 7, 2024
1 parent 44216a8 commit 27353aa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 19 additions & 14 deletions lib/src/easy_localization_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -214,7 +219,7 @@ class _EasyLocalizationState extends State<EasyLocalization> {
supportedLocales: widget.supportedLocales,
useFallbackTranslationsForEmptyResources:
widget.useFallbackTranslationsForEmptyResources,
forcePluralCaseFallback: widget.forcePluralCaseFallback,
ignorePluralRules: widget.ignorePluralRules,
),
);
}
Expand Down Expand Up @@ -296,14 +301,14 @@ class _EasyLocalizationDelegate extends LocalizationsDelegate<Localization> {
final List<Locale>? 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,
}) {
Expand All @@ -326,7 +331,7 @@ class _EasyLocalizationDelegate extends LocalizationsDelegate<Localization> {
fallbackTranslations: localizationController!.fallbackTranslations,
useFallbackTranslationsForEmptyResources:
useFallbackTranslationsForEmptyResources,
forcePluralCaseFallback: forcePluralCaseFallback,
ignorePluralRules: ignorePluralRules,
);
return Future.value(Localization.instance);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/src/localization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Localization {
};

bool _useFallbackTranslationsForEmptyResources = false;
bool _forcePluralCaseFallback = false;
bool _ignorePluralRules = false;

Localization();

Expand All @@ -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;
}

Expand Down Expand Up @@ -119,7 +119,7 @@ class Localization {
}

static PluralRule? _pluralRule(String? locale, num howMany) {
if (instance._forcePluralCaseFallback) {
if (!instance._ignorePluralRules) {
return () => _pluralCaseFallback(howMany);
}
startRuleEvaluation(howMany);
Expand Down

0 comments on commit 27353aa

Please sign in to comment.