long1eu / flutter_i18n

This plugin create a binding between your translations from .arb files and your Flutter app.
Apache License 2.0
252 stars 55 forks source link

Doesn't support zh_TW #57

Open jidesoft opened 5 years ago

jidesoft commented 5 years ago

The locale comes into GeneratedLocalizationsDelegate_resolve method is 'zh_Hant_TW' on an Android phone that was set to Chinese (Taiwan). The arb file is strings_zh_TW.arb which is Traditional Chinese. We also have strings_zh.arb which is Simplified Chinese.

    final Locale languageLocale = Locale(locale.languageCode, "");
    if (supported.contains(locale)) { // this line return false because the supported locales doesn't include zh_Hant_TW
      return locale;
    } else if (supported.contains(languageLocale)) { // this line returns true because zh is there so we ended up with zh (Simplified Chinese). 
      return languageLocale;
    } else {
      final Locale fallbackLocale = fallback ?? supported.first;
      return fallbackLocale;
    }

It looks like we should have one more if statement to check for language and country locale.

For Chinese, there are also zh_Hant_HW and zh_Hant_MO in addition to zh_Hant_TW. So we need a way to support the scriptCode (Hans or Hant for Simplified Chinese and Traditional Chinese respectively) so that we don't need an arb for each of them. Two arbs for Hant and Hans would be good enough.

noordawod commented 5 years ago

Thanks @jidesoft for this!

Adding support for a script code will be a lengthy task and unsure which languages will this aid beyond Chinese (maybe Japanese, Korean, and other Asian languages?), what do you think @long1eu?

I suspect this may take up to an hour, including testing, so maybe we can put it on the to-do list for an upcoming minor release?

@jidesoft by any chance, do you want to submit a pr to add this functionality?

jidesoft commented 5 years ago

@noordawod @long1eu Since I need it anyway, I can do it if you can tell me where to modify the code as I am not familiar with the way IDEA plugin works. Here are the list of tasks I can think of.

  1. When scanning the existing arb files, it should support scriptCode. The scriptCode should have a high priority than countryCode. The get supportedLocales method should be generated to include scriptCode. The only problem is when the arb file name is zh_Hant, how do we know Hant is the scriptCode or the countryCode? Maybe if it is two-letter, we assume it is a countryCode. Otherwrise a scriptCode.
  2. The _resolve and _isSupported methods need to be changed to consider the scriptCode. It should have multiple levels of fallbacks, starting from more specific match or less specific match. First lang-script-country, then lang-script, then land-country, then lang.
  3. The getLang method needs to changed to consider the scriptCode.
crush3r commented 5 years ago

I also need a way to support the scriptCode (Hans and Hant). Do you have plans to add this ability?

noordawod commented 5 years ago

The code is written in Kotlin and you'd need to open the project in some Kotlin- and Gradle-capable IDE. I use, as part of normal work, IntelliJ IDEA and can certainly recommend it.

crush3r commented 5 years ago

Thank you for your time for this answer and such a helpful library. You wrote above that you need about one hour to add this functionality. It's required because of without it it's impossible to support all Chinese languages, Traditional and Simplified. It works with Android, but not with iOS. Do you still have plans to add this feature?

long1eu commented 5 years ago

One reason this doesn't work just on iOS is that you didn't added the supported languages to your Info.plist

https://flutter.dev/docs/development/accessibility-and-localization/internationalization#appendix-updating-the-ios-app-bundle

crush3r commented 5 years ago

@long1eu @noordawod Thank you for the answers. I added the supported languages to Info.plist, and all of them work fine except Chinese. But if I understand right, we can't fix it. Google wrote: "As of April 2019, the global localization classes support about 52 languages." And as we could see here https://github.com/flutter/flutter/tree/master/packages/flutter_localizations/lib/src/l10n language resource names can contain only lang code and country code, like zh_HK. In the settings of the iOS, you can select a few variants of Chinese language: Simplified, Traditional, Traditional (Hong Kong), Traditional (Macau), Traditional (Taiwan). So, I should use zh, zh_HK, zh_MO, zh_TW. And only if the user selected Traditional (without specifying country), it could be anything like zh_Hant_RU, the app will choose zh (simplified). It’s wrong, but we can't do anything in that case. What do you think about it?