Skip to content

Commit 73a8830

Browse files
committed
added support for localisation json, updated flutter version
1 parent ba7d70a commit 73a8830

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

lib/model/localization_settings.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import 'package:flutter_localization/model/local_file.dart';
44
55
class LocalizationSettings {
66
/// Path to the localisation file in app bundle
7-
final String localisationFilePath;
7+
/// Use either [localisationFilePath] or [localisationJson]
8+
final String? localisationFilePath;
9+
10+
/// Localisation json
11+
/// Use either [localisationFilePath] or [localisationJson]
12+
final String? localisationJson;
813

914
/// The [supportedLanguages] of the app.
1015
///
@@ -17,8 +22,11 @@ class LocalizationSettings {
1722
final List<LocalFile> localFiles;
1823

1924
LocalizationSettings({
20-
required this.localisationFilePath,
25+
this.localisationFilePath,
26+
this.localisationJson,
2127
this.supportedLanguages = const ['en'],
2228
this.localFiles = const [],
23-
}) : assert(supportedLanguages.length > 0, 'supportedLanguages cannot be empty');
29+
}) : assert(supportedLanguages.length > 0, 'supportedLanguages cannot be empty'),
30+
assert(localisationFilePath != null || localisationJson != null,
31+
'localisationFilePath or localisationJson must not be null');
2432
}

lib/service/localization_service.dart

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'dart:convert';
2-
import 'dart:ui';
32

43
import 'package:flutter_localization/model/localization_settings.dart';
54
import 'package:flutter_localization/model/localized_string.dart';
@@ -44,8 +43,8 @@ class LocalizationService {
4443
}
4544

4645
Future<void> _readLocalization() async {
47-
final Map<String, dynamic> localizationMap =
48-
jsonDecode(await rootBundle.loadString(_settings.localisationFilePath));
46+
final localizationJson = _settings.localisationJson ?? await rootBundle.loadString(_settings.localisationFilePath!);
47+
final Map<String, dynamic> localizationMap = jsonDecode(localizationJson);
4948
_initLocalizationStringFromJsonMap(localizationMap);
5049
}
5150

@@ -122,13 +121,10 @@ class LocalizationService {
122121
}
123122

124123
Future<String?> getLocalizedLocalFile(String id) async {
125-
var localFile =
126-
_settings.localFiles.firstWhereOrNull((file) => file.id == id && file.langCode == currentLanguageCode);
124+
var localFile = _settings.localFiles.firstWhereOrNull((file) => file.id == id && file.langCode == currentLanguageCode);
127125
if (localFile == null) {
128-
print(
129-
'flutter_localization: Local file with id: $id not found for current language. Fallback to default language');
130-
localFile =
131-
_settings.localFiles.firstWhereOrNull((file) => file.id == id && file.langCode == defaultLanguageCode);
126+
print('flutter_localization: Local file with id: $id not found for current language. Fallback to default language');
127+
localFile = _settings.localFiles.firstWhereOrNull((file) => file.id == id && file.langCode == defaultLanguageCode);
132128
}
133129
if (localFile == null) {
134130
print('flutter_localization: Local file with id: $id not found for default language');

pubspec.lock

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
name: async
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "2.6.1"
10+
version: "2.8.2"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
@@ -21,14 +21,14 @@ packages:
2121
name: characters
2222
url: "https://pub.dartlang.org"
2323
source: hosted
24-
version: "1.1.0"
24+
version: "1.2.0"
2525
charcode:
2626
dependency: transitive
2727
description:
2828
name: charcode
2929
url: "https://pub.dartlang.org"
3030
source: hosted
31-
version: "1.2.0"
31+
version: "1.3.1"
3232
clock:
3333
dependency: transitive
3434
description:
@@ -85,14 +85,21 @@ packages:
8585
name: matcher
8686
url: "https://pub.dartlang.org"
8787
source: hosted
88-
version: "0.12.10"
88+
version: "0.12.11"
89+
material_color_utilities:
90+
dependency: transitive
91+
description:
92+
name: material_color_utilities
93+
url: "https://pub.dartlang.org"
94+
source: hosted
95+
version: "0.1.3"
8996
meta:
9097
dependency: transitive
9198
description:
9299
name: meta
93100
url: "https://pub.dartlang.org"
94101
source: hosted
95-
version: "1.3.0"
102+
version: "1.7.0"
96103
path:
97104
dependency: transitive
98105
description:
@@ -146,7 +153,7 @@ packages:
146153
name: test_api
147154
url: "https://pub.dartlang.org"
148155
source: hosted
149-
version: "0.3.0"
156+
version: "0.4.8"
150157
typed_data:
151158
dependency: transitive
152159
description:
@@ -160,7 +167,7 @@ packages:
160167
name: vector_math
161168
url: "https://pub.dartlang.org"
162169
source: hosted
163-
version: "2.1.0"
170+
version: "2.1.1"
164171
sdks:
165-
dart: ">=2.12.0 <3.0.0"
172+
dart: ">=2.14.0 <3.0.0"
166173
flutter: ">=1.20.0"

pubspec.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
name: flutter_localization
22
description: Flutter Localization
3-
version: 2.0.0
4-
author: Filip Rajicic
5-
homepage: www.credeo.de
3+
version: 2.1.0
4+
homepage: www.credeo.rs
65

76
environment:
87
sdk: ">=2.12.0 <3.0.0"

0 commit comments

Comments
 (0)