-
-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy patheasy_localization_controller.dart
More file actions
155 lines (136 loc) · 4.98 KB
/
Copy patheasy_localization_controller.dart
File metadata and controls
155 lines (136 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl_standalone.dart'
if (dart.library.html) 'package:intl/intl_browser.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'translations.dart';
class EasyLocalizationController extends ChangeNotifier {
static Locale? _savedLocale;
static late Locale _deviceLocale;
late Locale _locale;
Locale? _fallbackLocale;
final Function(FlutterError e) onLoadError;
final assetLoader;
final String path;
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;
EasyLocalizationController({
required List<Locale> supportedLocales,
required this.useFallbackTranslations,
required this.saveLocale,
required this.assetLoader,
required this.path,
required this.useOnlyLangCode,
required this.onLoadError,
this.fallbackLocaleData,
Locale? startLocale,
Locale? fallbackLocale,
Locale? forceLocale, // used for testing
}) {
_fallbackLocale = fallbackLocale;
if (forceLocale != null) {
_locale = forceLocale;
} else if (_savedLocale == null && startLocale != null) {
_locale = _getFallbackLocale(supportedLocales, startLocale);
EasyLocalization.logger('Start locale loaded ${_locale.toString()}');
}
// If saved locale then get
else if (saveLocale && _savedLocale != null) {
EasyLocalization.logger('Saved locale loaded ${_savedLocale.toString()}');
_locale = _savedLocale!;
} else {
// From Device Locale
_locale = supportedLocales.firstWhere(
(locale) => _checkInitLocale(locale, _deviceLocale),
orElse: () => _getFallbackLocale(supportedLocales, fallbackLocale));
}
if (fallbackLocaleData != null) {
_translations = Translations(fallbackLocaleData);
}
}
//Get fallback Locale
Locale _getFallbackLocale(
List<Locale> supportedLocales, Locale? fallbackLocale) {
//If fallbackLocale not set then return first from supportedLocales
if (fallbackLocale != null) {
return fallbackLocale;
} else {
return supportedLocales.first;
}
}
bool _checkInitLocale(Locale locale, Locale? _deviceLocale) {
// If supported locale not contain countryCode then check only languageCode
if (locale.countryCode == null) {
return (locale.languageCode == _deviceLocale!.languageCode);
} else {
return (locale == _deviceLocale);
}
}
Future loadTranslations() async {
Map<String, dynamic> data;
try {
data = await loadTranslationData(_locale);
_translations = Translations(data);
if (useFallbackTranslations && _fallbackLocale != null) {
Map<String, dynamic>? baseLangData;
if (_locale.countryCode != null && _locale.countryCode!.isNotEmpty) {
baseLangData = await loadTranslationData(Locale(locale.languageCode));
}
data = await loadTranslationData(_fallbackLocale!);
if (baseLangData != null) {
data.addAll(baseLangData);
}
_fallbackTranslations = Translations(data);
}
} on FlutterError catch (e) {
onLoadError(e);
} catch (e) {
onLoadError(FlutterError(e.toString()));
}
}
Future loadTranslationData(Locale locale) async {
if (useOnlyLangCode) {
return assetLoader.load(path, Locale(locale.languageCode));
} else {
return assetLoader.load(path, locale);
}
}
Locale get locale => _locale;
Future<void> setLocale(Locale l) async {
_locale = l;
await loadTranslations();
notifyListeners();
EasyLocalization.logger('Locale $locale changed');
await _saveLocale(_locale);
}
Future<void> _saveLocale(Locale? locale) async {
if (!saveLocale) return;
final _preferences = await SharedPreferences.getInstance();
await _preferences.setString('locale', locale.toString());
EasyLocalization.logger('Locale $locale saved');
}
static Future<void> initEasyLocation() async {
final _preferences = await SharedPreferences.getInstance();
final _strLocale = _preferences.getString('locale');
_savedLocale = _strLocale != null ? _strLocale.toLocale() : null;
final _foundPlatformLocale = await findSystemLocale();
_deviceLocale = _foundPlatformLocale.toLocale();
EasyLocalization.logger.debug('Localization initialized');
}
Future<void> deleteSaveLocale() async {
_savedLocale = null;
final _preferences = await SharedPreferences.getInstance();
await _preferences.remove('locale');
EasyLocalization.logger('Saved locale deleted');
}
Locale get deviceLocale => _deviceLocale;
Future<void> resetLocale() async {
EasyLocalization.logger('Reset locale to platform locale $_deviceLocale');
await setLocale(_deviceLocale);
}
}