Committed by
GitHub
Merge pull request #394 from jasonlaw/master
Translation return null value when no entry found
Showing
2 changed files
with
22 additions
and
5 deletions
@@ -655,6 +655,16 @@ class GetImpl implements GetService { | @@ -655,6 +655,16 @@ class GetImpl implements GetService { | ||
655 | translations.addAll(tr); | 655 | translations.addAll(tr); |
656 | } | 656 | } |
657 | 657 | ||
658 | + void appendTranslations(Map<String, Map<String, String>> tr) { | ||
659 | + tr.forEach((key, map) { | ||
660 | + if (Get.translations.containsKey(key)) { | ||
661 | + Get.translations[key].addAll(map); | ||
662 | + } else { | ||
663 | + Get.translations[key] = map; | ||
664 | + } | ||
665 | + }); | ||
666 | + } | ||
667 | + | ||
658 | void changeTheme(ThemeData theme) { | 668 | void changeTheme(ThemeData theme) { |
659 | getxController.setTheme(theme); | 669 | getxController.setTheme(theme); |
660 | } | 670 | } |
@@ -257,15 +257,22 @@ abstract class Translations { | @@ -257,15 +257,22 @@ abstract class Translations { | ||
257 | 257 | ||
258 | extension Trans on String { | 258 | extension Trans on String { |
259 | String get tr { | 259 | String get tr { |
260 | + // Returns the key if locale is null. | ||
260 | if (Get.locale?.languageCode == null) return this; | 261 | if (Get.locale?.languageCode == null) return this; |
261 | - if (Get.translations | ||
262 | - .containsKey("${Get.locale.languageCode}_${Get.locale.countryCode}")) { | 262 | + |
263 | + // Checks whether the language code and country code are present, and whether the key is also present. | ||
264 | + if (Get.translations.containsKey( | ||
265 | + "${Get.locale.languageCode}_${Get.locale.countryCode}") && | ||
266 | + Get.translations["${Get.locale.languageCode}_${Get.locale.countryCode}"] | ||
267 | + .containsKey(this)) { | ||
263 | return Get.translations[ | 268 | return Get.translations[ |
264 | "${Get.locale.languageCode}_${Get.locale.countryCode}"][this]; | 269 | "${Get.locale.languageCode}_${Get.locale.countryCode}"][this]; |
265 | - } else if (Get.translations.containsKey(Get.locale.languageCode)) { | 270 | + |
271 | + // Checks if there is a callback language in the absence of the specific country, and if it contains that key. | ||
272 | + } else if (Get.translations.containsKey(Get.locale.languageCode) && | ||
273 | + Get.translations[Get.locale.languageCode].containsKey(this)) { | ||
266 | return Get.translations[Get.locale.languageCode][this]; | 274 | return Get.translations[Get.locale.languageCode][this]; |
267 | - } else if (Get.translations.isNotEmpty) { | ||
268 | - return Get.translations.values.first[this]; | 275 | + // If there is no corresponding language or corresponding key, return the key. |
269 | } else { | 276 | } else { |
270 | return this; | 277 | return this; |
271 | } | 278 | } |
-
Please register or login to post a comment