antest1 / kcanotify

Viewer Application for KanColle Android
GNU General Public License v3.0
204 stars 29 forks source link

Get locale code from string.xml instead of Locale #8

Closed wafer-li closed 7 years ago

wafer-li commented 7 years ago

There is some problems in the current i18n implementation.

Let me explain it:

Background

There is lots of regions in the Chinese language zone, such as CN, HK, TW, MO and SG.

But there are only two main language variants: the Simplified Chinese(简体中文) and the Traditional Chinese(繁體中文).

The CN and SG use Simplified Chinese, and the rest of the regions use Traditional Chinese.

We should use Traditional Chinese as the fallback, because it covers more regions.

As above, we came across the project struct, as I mentioned at https://github.com/antest1/kcanotify/issues/6#issuecomment-286493663:

value-zh => Traditional Chinese
value-zh-rCN => Simplified Chinese
value-zh-rSG => Simplified Chinese

Problem

There comes the problem, we only have two language variants, and need to deal with a lot of regions.

Now the project using the following code to retrieve the localecode:

String language = Locale.getDefault().getLanguage();
String country = Locale.getDefault().getCountry();

And the languageOptionValue only provide two zh value: zh-CN and zh-TW

What if a user in HK, and the default locale is zh-HK, as the structure above, it will fallback to value-zh which provides the Traditional Chinese.

But, there is no zh-HK in languageOptionValue, then the entry of Language Setting will display the English.

And when the user restart the app, it will set the Configuration Locale to English even if we have the correct Localized Resource.

Solution

As This StackOverflow describe, we should set the correct localecode in each strings.xml.

And retrieve the localecode in strings.xml which is the real currently using language variant.

antest1 commented 7 years ago

Since the locale of application is set in KcaApplication.java with preference, so it won't set the locale to English after restart.

But your solution seems to be more clear, I'll merge your commit after checking any dependencies for getLocaleInArray.

wafer-li commented 7 years ago

But your solution seems to be more clear, I'll merge your commit after checking any dependencies for getLocaleInArray.

The getLocaleInArray() only used in MainActivity#setDefaultPreference().

That is why I delete that method.

======

Since the locale of application is set in KcaApplication.java with preference, so it won't set the locale to English after restart.

Actually, it will.

As the app first boot, the locale of configuration is the default, let's say, zh-HK.

And the Preference is set at MainActivity#setDefaultPreference().

As there is no zh-HK locale code.(You use zh-TW to represent Traditional Chinese at getLocaleCode())

It will fallback to en-US. (It should be set to zh-TW, but it doesn't).

But, there is the value-zh fallback resource, so the Android System will use it to display.

Now the Language Preference is en-US, while the Application use the Traditional Chinese.

That is the problem, you have the wrong language setting "English", but you display the correct language "Traditional Chinese".

But when the app restart, without changing any Preference, which means the Language Preference is still English.

Therefore, it will use the en-US locale, which will cause the Language to be English.

antest1 commented 7 years ago

Oh, I now understand. I haven't notice that problem before. It should be fixed. Thanks for kind explanation!