fix(i18n): LocaleDetector should consider the language tag

This commit is contained in:
lala 2021-06-06 15:22:13 +08:00 committed by Дамян Минков
parent f82d46337b
commit 35c7f156db
2 changed files with 13 additions and 4 deletions

View File

@ -47,7 +47,7 @@ class LocaleDetector extends ReactContextBaseJavaModule {
public Map<String, Object> getConstants() {
Context context = getReactApplicationContext();
HashMap<String,Object> constants = new HashMap<>();
constants.put("locale", context.getResources().getConfiguration().locale.toString());
constants.put("locale", context.getResources().getConfiguration().locale.toLanguageTag());
return constants;
}
@ -55,4 +55,4 @@ class LocaleDetector extends ReactContextBaseJavaModule {
public String getName() {
return "LocaleDetector";
}
}
}

View File

@ -20,8 +20,17 @@ export default {
detect() {
const { LocaleDetector } = NativeModules;
const [ lang, region ] = LocaleDetector.locale.replace(/_/, '-').split('-');
const locale = `${lang}${region}`;
const parts = LocaleDetector.locale.replace(/_/, '-').split('-');
const [ lang, regionOrScript, region ] = parts;
let locale;
if (parts.length >= 3) {
locale = `${lang}${region}`;
} else if (parts.length === 2) {
locale = `${lang}${regionOrScript}`;
} else {
locale = lang;
}
if (LANGUAGES.includes(locale)) {
return locale;