Skip to content

Commit c336ee1

Browse files
committed
Notify depending widgets when the translations are loaded
1 parent 804d603 commit c336ee1

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

lib/src/easy_localization_app.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:easy_logger/easy_logger.dart';
66
import 'package:flutter/material.dart';
77
import 'package:flutter_localizations/flutter_localizations.dart';
88

9-
import 'asset_loader.dart';
109
import 'localization.dart';
1110

1211
part 'utils.dart';
@@ -230,6 +229,7 @@ class _EasyLocalizationProvider extends InheritedWidget {
230229
final EasyLocalizationController _localeState;
231230
final Locale? currentLocale;
232231
final _EasyLocalizationDelegate delegate;
232+
final bool _translationsLoaded;
233233

234234
/// {@macro flutter.widgets.widgetsApp.localizationsDelegates}
235235
///
@@ -256,6 +256,7 @@ class _EasyLocalizationProvider extends InheritedWidget {
256256
_EasyLocalizationProvider(this.parent, this._localeState,
257257
{Key? key, required this.delegate})
258258
: currentLocale = _localeState.locale,
259+
_translationsLoaded = _localeState.translations != null,
259260
super(key: key, child: parent.child) {
260261
EasyLocalization.logger.debug('Init provider');
261262
}
@@ -291,7 +292,8 @@ class _EasyLocalizationProvider extends InheritedWidget {
291292

292293
@override
293294
bool updateShouldNotify(_EasyLocalizationProvider oldWidget) {
294-
return oldWidget.currentLocale != locale;
295+
return oldWidget.currentLocale != locale
296+
|| oldWidget._translationsLoaded != _translationsLoaded;
295297
}
296298

297299
static _EasyLocalizationProvider? of(BuildContext context) =>

lib/src/easy_localization_controller.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class EasyLocalizationController extends ChangeNotifier {
124124
}
125125
_fallbackTranslations = Translations(data);
126126
}
127+
notifyListeners();
127128
} on FlutterError catch (e) {
128129
onLoadError(e);
129130
} catch (e) {

test/easy_localization_init_test.dart

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import 'package:easy_localization/easy_localization.dart';
2+
import 'package:easy_logger/easy_logger.dart';
3+
import 'package:flutter/material.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
import 'package:shared_preferences/shared_preferences.dart';
6+
7+
import 'easy_localization_context_test.dart';
8+
9+
Future<void> main() async {
10+
EasyLocalization.logger.enableLevels = <LevelMessages>[
11+
LevelMessages.error,
12+
LevelMessages.warning,
13+
];
14+
15+
SharedPreferences.setMockInitialValues({});
16+
EasyLocalization.logger.enableLevels = <LevelMessages>[
17+
LevelMessages.error,
18+
LevelMessages.warning,
19+
];
20+
21+
await EasyLocalization.ensureInitialized();
22+
23+
testWidgets(
24+
'Ensure that loading the translations will update its depending widgets',
25+
(WidgetTester tester) async {
26+
await tester.runAsync(() async {
27+
await tester.pumpWidget(EasyLocalization(
28+
supportedLocales: const [Locale('en'), Locale('de')],
29+
path: '../../i18n',
30+
fallbackLocale: const Locale('en'),
31+
child: const I18nObserver(child: MyApp()),
32+
));
33+
await tester.pump();
34+
});
35+
},
36+
);
37+
}
38+
39+
class I18nObserver extends StatefulWidget {
40+
final Widget child;
41+
42+
const I18nObserver({Key? key, required this.child}) : super(key: key);
43+
44+
@override
45+
State<I18nObserver> createState() => _I18nObserverState();
46+
}
47+
48+
class _I18nObserverState extends State<I18nObserver> {
49+
var _firstUpdate = true;
50+
51+
@override
52+
Widget build(BuildContext context) => widget.child;
53+
54+
@override
55+
void didChangeDependencies() {
56+
// use the dependOnInheritedWidgetOfExactType pattern
57+
EasyLocalization.of(context);
58+
59+
super.didChangeDependencies();
60+
61+
if (_firstUpdate) {
62+
_firstUpdate = false;
63+
expect(
64+
'test'.tr(),
65+
'test',
66+
reason: 'The translation cannot be found yet',
67+
);
68+
} else {
69+
expect(
70+
'test'.tr(),
71+
'test_en',
72+
reason: 'The translation should be loaded on the second call '
73+
'of didChangeDependencies()',
74+
);
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)