fix(i18n): LocaleDetector should consider the language tag
This commit is contained in:
parent
f82d46337b
commit
35c7f156db
|
@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue