getx icon indicating copy to clipboard operation
getx copied to clipboard

Add zh_Hans and zh_Hant when using multiple languages

Open pinping opened this issue 1 year ago • 1 comments

Describe the bug When zh_Hans_CN and zh_Hant_TW are added to multi-language, only one zh voice will be displayed.

Reproduction code NOTE: THIS IS MANDATORY, IF YOUR ISSUE DOES NOT CONTAIN IT, IT WILL BE CLOSED PRELIMINARY)

example:

Translation.dart


static const supportedLocales = [
    Locale('en', 'US'), // 英语
    Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: 'CN'), // 简体中文
    // Locale('zh_Hans', 'CN'),
    // Locale('zh_Hant', 'TW'),
    Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hant', countryCode: 'TW'), // 繁体中文
    Locale('ja', 'JP'), // 日语
    Locale('ko', 'KR'), // 韩语
    Locale('es', 'ES'), // 西班牙语
    Locale('pt', 'PT'), // 葡萄牙语
    Locale('de', 'DE'), // 德语
    Locale('it', 'IT'), // 意大利语
  ];

@override
  Map<String, Map<String, String>> get keys => {
        'en': LocaleMaps.en,
        'zh_Hans': LocaleMaps.zhHans,
        'zh_Hant': LocaleMaps.zhHant,
        'ja': LocaleMaps.ja,
        'ko': LocaleMaps.ko,
        'es': LocaleMaps.es,
        'pt': LocaleMaps.pt,
        'de': LocaleMaps.de,
        'it': LocaleMaps.it,
      };


Expected behavior When switching languages, you can choose Simplified Chinese or Traditional Chinese

Getx Version: 4.6.6

Minimal reproduce code internacionalization.dart


  Map<String, String>? get _getSimilarLanguageTranslation {
    final translationsWithNoCountry = Get.translations.map((key, value) {
      var keyArr = key.split("_");
      if (keyArr.length > 1) {
        return MapEntry(keyArr[0] + "_" + keyArr[1], value);
      }
      return MapEntry(keyArr.first, value);
    });

    var localeSubtag = Get.locale!.languageCode.split("_").first;
    if (Get.locale!.scriptCode != null && Get.locale!.scriptCode != "") {
      localeSubtag = localeSubtag + "_" + Get.locale!.scriptCode!;
    }
    final containsKey = translationsWithNoCountry.containsKey(localeSubtag);
    if (!containsKey) {
      return null;
    }
    return translationsWithNoCountry[localeSubtag];
  }

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) {
      if (Get.locale!.scriptCode != null && Get.locale!.scriptCode != "") {
        return Get.translations[
                "${Get.locale!.languageCode}_${Get.locale!.scriptCode}_${Get.locale!.countryCode}"]![
            this]!;
      }
      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;
    }
  }


pinping avatar Jan 17 '24 08:01 pinping

@override
  Map<String, Map<String, String>> get keys => {
        'en': LocaleMaps.en,
        'zh_CN': LocaleMaps.zhHans,
        'zh_TW': LocaleMaps.zhHant,
        'ja': LocaleMaps.ja,
        'ko': LocaleMaps.ko,
        'es': LocaleMaps.es,
        'pt': LocaleMaps.pt,
        'de': LocaleMaps.de,
        'it': LocaleMaps.it,
      };

tankai2525 avatar Apr 22 '25 02:04 tankai2525