internacionalization.dart
4.2 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
import 'dart:ui';
import '../../../get_core/get_core.dart';
class _IntlHost {
Locale? locale;
Locale? fallbackLocale;
Map<String, Map<String, String>> translations = {};
}
extension FirstWhereExt<T> on List<T> {
/// The first element satisfying [test], or `null` if there are none.
T? firstWhereOrNull(bool Function(T element) test) {
for (var element in this) {
if (test(element)) return element;
}
return null;
}
}
extension LocalesIntl on GetInterface {
static final _intlHost = _IntlHost();
Locale? get locale => _intlHost.locale;
Locale? get fallbackLocale => _intlHost.fallbackLocale;
set locale(Locale? newLocale) => _intlHost.locale = newLocale;
set fallbackLocale(Locale? newLocale) => _intlHost.fallbackLocale = newLocale;
Map<String, Map<String, String>> get translations => _intlHost.translations;
void addTranslations(Map<String, Map<String, String>> tr) {
translations.addAll(tr);
}
void clearTranslations() {
translations.clear();
}
void appendTranslations(Map<String, Map<String, String>> tr) {
tr.forEach((key, map) {
if (translations.containsKey(key)) {
translations[key]!.addAll(map);
} else {
translations[key] = map;
}
});
}
}
extension Trans on String {
// Checks whether the language code and country code are present, and
// whether the key is also present.
bool get _fullLocaleAndKey {
return Get.translations.containsKey(
"${Get.locale!.languageCode}_${Get.locale!.countryCode}") &&
Get.translations[
"${Get.locale!.languageCode}_${Get.locale!.countryCode}"]!
.containsKey(this);
}
// Checks if there is a callback language in the absence of the specific
// country, and if it contains that key.
Map<String, String>? get _getSimilarLanguageTranslation {
final translationsWithNoCountry = Get.translations
.map((key, value) => MapEntry(key.split("_").first, value));
final containsKey =
translationsWithNoCountry.containsKey(Get.locale!.languageCode);
if (!containsKey) {
return null;
}
return translationsWithNoCountry[Get.locale!.languageCode];
}
String get tr {
// print('language');
// print(Get.locale!.languageCode);
// print('contains');
// print(Get.translations.containsKey(Get.locale!.languageCode));
// print(Get.translations.keys);
// Returns the key if locale is null.
if (Get.locale?.languageCode == null) return this;
if (_fullLocaleAndKey) {
return Get.translations[
"${Get.locale!.languageCode}_${Get.locale!.countryCode}"]![this]!;
}
final similarTranslation = _getSimilarLanguageTranslation;
if (similarTranslation != null && similarTranslation.containsKey(this)) {
return similarTranslation[this]!;
// If there is no corresponding language or corresponding key, return
// the key.
} else if (Get.fallbackLocale != null) {
final fallback = Get.fallbackLocale!;
final key = "${fallback.languageCode}_${fallback.countryCode}";
if (Get.translations.containsKey(key) &&
Get.translations[key]!.containsKey(this)) {
return Get.translations[key]![this]!;
}
if (Get.translations.containsKey(fallback.languageCode) &&
Get.translations[fallback.languageCode]!.containsKey(this)) {
return Get.translations[fallback.languageCode]![this]!;
}
return this;
} else {
return this;
}
}
String trArgs([List<String> args = const []]) {
var key = tr;
if (args.isNotEmpty) {
for (final arg in args) {
key = key.replaceFirst(RegExp(r'%s'), arg.toString());
}
}
return key;
}
String trPlural([String? pluralKey, int? i, List<String> args = const []]) {
return i == 1 ? trArgs(args) : pluralKey!.trArgs(args);
}
String trParams([Map<String, String> params = const {}]) {
var trans = tr;
if (params.isNotEmpty) {
params.forEach((key, value) {
trans = trans.replaceAll('@$key', value);
});
}
return trans;
}
String trPluralParams(
[String? pluralKey, int? i, Map<String, String> params = const {}]) {
return i == 1 ? trParams(params) : pluralKey!.trParams(params);
}
}