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

Feature: Fallback data #463

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 15 additions & 3 deletions lib/src/easy_localization_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:easy_localization/easy_localization.dart';
import 'package:easy_localization/src/easy_localization_controller.dart';
import 'package:easy_logger/easy_logger.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

Expand Down Expand Up @@ -75,6 +76,10 @@ class EasyLocalization extends StatefulWidget {
/// @Default value `errorWidget = ErrorWidget()`
final Widget Function(FlutterError? message)? errorWidget;

/// Data for fallback localte that is also startup locale
/// Use this to fix startup black screen
final Map<String, dynamic>? fallbackLocaleData;

EasyLocalization({
Key? key,
required this.child,
Expand All @@ -87,6 +92,7 @@ class EasyLocalization extends StatefulWidget {
this.assetLoader = const RootBundleAssetLoader(),
this.saveLocale = true,
this.errorWidget,
this.fallbackLocaleData,
}) : assert(supportedLocales.isNotEmpty),
assert(path.isNotEmpty),
super(key: key) {
Expand Down Expand Up @@ -126,6 +132,7 @@ class _EasyLocalizationState extends State<EasyLocalization> {
useOnlyLangCode: widget.useOnlyLangCode,
useFallbackTranslations: widget.useFallbackTranslations,
path: widget.path,
fallbackLocaleData: widget.fallbackLocaleData,
onLoadError: (FlutterError e) {
setState(() {
translationsLoadError = e;
Expand Down Expand Up @@ -251,16 +258,21 @@ class _EasyLocalizationDelegate extends LocalizationsDelegate<Localization> {
bool isSupported(Locale locale) => supportedLocales!.contains(locale);

@override
Future<Localization> load(Locale value) async {
Future<Localization> load(Locale value) {
EasyLocalization.logger.debug('Load Localization Delegate');
if (localizationController!.translations == null) {
await localizationController!.loadTranslations();
return localizationController!.loadTranslations().then((value) {
Localization.load(value,
translations: localizationController!.translations,
fallbackTranslations: localizationController!.fallbackTranslations);
return Future.value(Localization.instance);
});
}

Localization.load(value,
translations: localizationController!.translations,
fallbackTranslations: localizationController!.fallbackTranslations);
return Future.value(Localization.instance);
return SynchronousFuture<Localization>(Localization.instance);
}

@override
Expand Down
7 changes: 7 additions & 0 deletions lib/src/easy_localization_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class EasyLocalizationController extends ChangeNotifier {
final bool useFallbackTranslations;
final bool saveLocale;
final bool useOnlyLangCode;
final Map<String, dynamic>? fallbackLocaleData;

Translations? _translations, _fallbackTranslations;
Translations? get translations => _translations;
Translations? get fallbackTranslations => _fallbackTranslations;
Expand All @@ -31,6 +33,7 @@ class EasyLocalizationController extends ChangeNotifier {
required this.path,
required this.useOnlyLangCode,
required this.onLoadError,
this.fallbackLocaleData,
Locale? startLocale,
Locale? fallbackLocale,
Locale? forceLocale, // used for testing
Expand All @@ -52,6 +55,10 @@ class EasyLocalizationController extends ChangeNotifier {
(locale) => _checkInitLocale(locale, _deviceLocale),
orElse: () => _getFallbackLocale(supportedLocales, fallbackLocale));
}

if (fallbackLocaleData != null) {
_translations = Translations(fallbackLocaleData);
}
}

//Get fallback Locale
Expand Down