Skip to content

Commit

Permalink
Fix #648: allow using "old" (e.g. pre-3.0.4) plural rules evaluation
Browse files Browse the repository at this point in the history
Add forcePluralCaseFallback option to force evaluation of fallback
plural rules, i.e.

* forcePluralCaseFallback: false
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: true
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
doesn't exist, "other" is still used as fallback.
  • Loading branch information
mauriziopinotti committed Apr 28, 2024
1 parent 58b738a commit 44216a8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void main() async {
// startLocale: Locale('de', 'DE'),
// saveLocale: false,
// useOnlyLangCode: true,
forcePluralCaseFallback: true,

// optional assetLoader default used is RootBundleAssetLoader which uses flutter's assetloader
// install easy_localization_loader for enable custom loaders
Expand Down
10 changes: 4 additions & 6 deletions example/resources/langs/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
}
},
"clicked": {
"zero": "You clicked {} times!",
"one": "You clicked {} time!",
"two": "You clicked {} times!",
"few": "You clicked {} times!",
"many": "You clicked {} times!",
"other": "You clicked {} times!"
"zero": "You didn't click yet!",
"few": "You clicked a few times ({})!",
"many": "You clicked many times ({})!",
"other": "You clicked {} time(s)!"
},
"amount": {
"zero": "Your amount : {} ",
Expand Down
21 changes: 21 additions & 0 deletions lib/src/easy_localization_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ 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.
/// @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
/// // doesn't exist, "other" is still used as fallback.
/// forcePluralCaseFallback: true
/// ```
final bool forcePluralCaseFallback;

/// Path to your folder with localization files.
/// Example:
/// ```dart
Expand Down Expand Up @@ -117,6 +132,7 @@ class EasyLocalization extends StatefulWidget {
this.useOnlyLangCode = false,
this.useFallbackTranslations = false,
this.useFallbackTranslationsForEmptyResources = false,
this.forcePluralCaseFallback = false,
this.assetLoader = const RootBundleAssetLoader(),
this.extraAssetLoaders,
this.saveLocale = true,
Expand Down Expand Up @@ -198,6 +214,7 @@ class _EasyLocalizationState extends State<EasyLocalization> {
supportedLocales: widget.supportedLocales,
useFallbackTranslationsForEmptyResources:
widget.useFallbackTranslationsForEmptyResources,
forcePluralCaseFallback: widget.forcePluralCaseFallback,
),
);
}
Expand Down Expand Up @@ -243,6 +260,7 @@ class _EasyLocalizationProvider extends InheritedWidget {
/// Get fallback locale
Locale? get fallbackLocale => parent.fallbackLocale;
// Locale get startLocale => parent.startLocale;
/// Change app locale
Expand Down Expand Up @@ -278,12 +296,14 @@ class _EasyLocalizationDelegate extends LocalizationsDelegate<Localization> {
final List<Locale>? supportedLocales;
final EasyLocalizationController? localizationController;
final bool useFallbackTranslationsForEmptyResources;
final bool forcePluralCaseFallback;
/// * 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.localizationController,
this.supportedLocales,
}) {
Expand All @@ -306,6 +326,7 @@ class _EasyLocalizationDelegate extends LocalizationsDelegate<Localization> {
fallbackTranslations: localizationController!.fallbackTranslations,
useFallbackTranslationsForEmptyResources:
useFallbackTranslationsForEmptyResources,
forcePluralCaseFallback: forcePluralCaseFallback,
);
return Future.value(Localization.instance);
}
Expand Down
15 changes: 12 additions & 3 deletions lib/src/localization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ class Localization {
};

bool _useFallbackTranslationsForEmptyResources = false;
bool _forcePluralCaseFallback = false;

Localization();

static Localization? _instance;

static Localization get instance => _instance ?? (_instance = Localization());

static Localization? of(BuildContext context) =>
Localizations.of<Localization>(context, Localization);

Expand All @@ -33,12 +36,14 @@ class Localization {
Translations? translations,
Translations? fallbackTranslations,
bool useFallbackTranslationsForEmptyResources = false,
bool forcePluralCaseFallback = false,
}) {
instance._locale = locale;
instance._translations = translations;
instance._fallbackTranslations = fallbackTranslations;
instance._useFallbackTranslationsForEmptyResources =
useFallbackTranslationsForEmptyResources;
instance._forcePluralCaseFallback = forcePluralCaseFallback;
return translations == null ? false : true;
}

Expand Down Expand Up @@ -114,6 +119,9 @@ class Localization {
}

static PluralRule? _pluralRule(String? locale, num howMany) {
if (instance._forcePluralCaseFallback) {
return () => _pluralCaseFallback(howMany);
}
startRuleEvaluation(howMany);
return pluralRules[locale];
}
Expand All @@ -139,11 +147,11 @@ class Localization {
String? name,
NumberFormat? format,
}) {

late String res;

final pluralRule = _pluralRule(_locale.languageCode, value);
final pluralCase = pluralRule != null ? pluralRule() : _pluralCaseFallback(value);
final pluralCase =
pluralRule != null ? pluralRule() : _pluralCaseFallback(value);

switch (pluralCase) {
case PluralCase.ZERO:
Expand Down Expand Up @@ -186,7 +194,8 @@ class Localization {
if (subKey == 'other') return _resolve('$key.other');

final tag = '$key.$subKey';
var resource = _resolve(tag, logging: false, fallback: _fallbackTranslations != null);
var resource =
_resolve(tag, logging: false, fallback: _fallbackTranslations != null);
if (resource == tag) {
resource = _resolve('$key.other');
}
Expand Down

0 comments on commit 44216a8

Please sign in to comment.