Skip to content

Commit 74e6432

Browse files
committed
Merge branch 'feat/add-linked-files' into develop
2 parents 186b05c + a7e5a95 commit 74e6432

File tree

3 files changed

+81
-138
lines changed

3 files changed

+81
-138
lines changed

lib/src/asset_loader.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,29 @@ class RootBundleAssetLoader extends AssetLoader {
3535
}
3636

3737
Future<Map<String, dynamic>> _getLinkedTranslationFileDataFromBaseJson(
38-
String basePath, Locale locale, Map<String, dynamic> baseJson) async {
39-
Map<String, dynamic> fullJson = {};
38+
String basePath, Locale locale, Map<String, dynamic> baseJson,
39+
{List<String> fileLoaded = const []}) async {
40+
Map<String, dynamic> fullJson = Map<String, dynamic>.from(baseJson);
4041

4142
for (var entry in baseJson.entries) {
4243
var key = entry.key;
4344
var value = entry.value;
4445

4546
if (value is String && value.startsWith(':/')) {
4647
String filePath = value.substring(2);
48+
49+
if (fileLoaded.contains(filePath)) {
50+
throw Exception('Circular reference detected: $filePath is loaded multiple times');
51+
}
52+
53+
fileLoaded.add(filePath);
4754
value = json.decode(await rootBundle.loadString(_getLinkedLocalePath(basePath, filePath, locale)));
4855
}
4956

5057
if (value is Map<String, dynamic>) {
51-
fullJson[key] = await _getLinkedTranslationFileDataFromBaseJson(basePath, locale, value);
52-
continue;
58+
fullJson[key] =
59+
await _getLinkedTranslationFileDataFromBaseJson(basePath, locale, value, fileLoaded: fileLoaded);
5360
}
54-
55-
fullJson[key] = value;
5661
}
5762

5863
return fullJson;
@@ -64,6 +69,6 @@ class RootBundleAssetLoader extends AssetLoader {
6469
EasyLocalization.logger.debug('Load asset from $path');
6570

6671
Map<String, dynamic> baseJson = json.decode(await rootBundle.loadString(localePath));
67-
return _getLinkedTranslationFileDataFromBaseJson(path, locale, baseJson);
72+
return await _getLinkedTranslationFileDataFromBaseJson(path, locale, baseJson);
6873
}
6974
}

test/easy_localization_context_test.dart

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ void main() async {
8787
saveLocale: false,
8888
useOnlyLangCode: true,
8989
// fallbackLocale:Locale('en') ,
90-
supportedLocales: const [
91-
Locale('ar')
92-
], // Locale('en', 'US'), Locale('ar','DZ')
90+
supportedLocales: const [Locale('ar')], // Locale('en', 'US'), Locale('ar','DZ')
9391
child: const MyApp(),
9492
));
9593
// await tester.idle();
@@ -117,10 +115,7 @@ void main() async {
117115
await tester.pumpWidget(EasyLocalization(
118116
path: '../../i18n',
119117
// fallbackLocale:Locale('en') ,
120-
supportedLocales: const [
121-
Locale('en', 'US'),
122-
Locale('ar', 'DZ')
123-
], // Locale('en', 'US'), Locale('ar','DZ')
118+
supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], // Locale('en', 'US'), Locale('ar','DZ')
124119
child: const MyApp(),
125120
));
126121
// await tester.idle();
@@ -140,13 +135,10 @@ void main() async {
140135
await tester.pumpWidget(EasyLocalization(
141136
path: '../../i18n',
142137
// fallbackLocale:Locale('en') ,
143-
supportedLocales: const [
144-
Locale('en', 'US'),
145-
Locale('ar', 'DZ')
146-
], // Locale('en', 'US'), Locale('ar','DZ')
138+
supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], // Locale('en', 'US'), Locale('ar','DZ')
147139
child: const MyApp(),
148140
));
149-
// await tester.idle();
141+
await tester.idle();
150142
// The async delegator load will require build on the next frame. Thus, pump
151143
await tester.pump();
152144

@@ -161,13 +153,10 @@ void main() async {
161153
await tester.runAsync(() async {
162154
await tester.pumpWidget(EasyLocalization(
163155
path: '../../i18n',
164-
supportedLocales: const [
165-
Locale('en', 'US'),
166-
Locale('ar', 'DZ')
167-
], // Locale('en', 'US'), Locale('ar','DZ')
156+
supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], // Locale('en', 'US'), Locale('ar','DZ')
168157
child: const MyApp(),
169158
));
170-
// await tester.idle();
159+
await tester.idle();
171160
// The async delegator load will require build on the next frame. Thus, pump
172161
await tester.pump();
173162

@@ -182,10 +171,7 @@ void main() async {
182171
await tester.runAsync(() async {
183172
await tester.pumpWidget(EasyLocalization(
184173
path: '../../i18n',
185-
supportedLocales: const [
186-
Locale('en', 'US'),
187-
Locale('ar', 'DZ')
188-
], // Locale('en', 'US'), Locale('ar','DZ')
174+
supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], // Locale('en', 'US'), Locale('ar','DZ')
189175
startLocale: const Locale('ar', 'DZ'),
190176
child: const MyApp(),
191177
));
@@ -208,10 +194,7 @@ void main() async {
208194
await tester.runAsync(() async {
209195
await tester.pumpWidget(EasyLocalization(
210196
path: '../../i18n',
211-
supportedLocales: const [
212-
Locale('en', 'US'),
213-
Locale('ar', 'DZ')
214-
], // Locale('en', 'US'), Locale('ar','DZ')
197+
supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], // Locale('en', 'US'), Locale('ar','DZ')
215198
child: const MyApp(),
216199
));
217200
await tester.idle();
@@ -229,10 +212,7 @@ void main() async {
229212
await tester.runAsync(() async {
230213
await tester.pumpWidget(EasyLocalization(
231214
path: '../../i18n',
232-
supportedLocales: const [
233-
Locale('en', 'US'),
234-
Locale('ar', 'DZ')
235-
], // Locale('en', 'US'), Locale('ar','DZ')
215+
supportedLocales: const [Locale('en', 'US'), Locale('ar', 'DZ')], // Locale('en', 'US'), Locale('ar','DZ')
236216
startLocale: const Locale('ar', 'DZ'),
237217
child: const MyApp(),
238218
));

0 commit comments

Comments
 (0)