rn,lang: fix language detection to take region into account

If we have a regional variant, prefer that.
This commit is contained in:
Saúl Ibarra Corretgé 2020-05-04 16:12:28 +02:00 committed by Saúl Ibarra Corretgé
parent a45cbf41ef
commit 9be78c60eb
1 changed files with 11 additions and 1 deletions

View File

@ -2,6 +2,10 @@
import { NativeModules } from 'react-native';
import LANGUAGES_RESOURCES from '../../../../lang/languages.json';
const LANGUAGES = Object.keys(LANGUAGES_RESOURCES);
/**
* The singleton language detector for React Native which uses the system-wide
* locale.
@ -16,8 +20,14 @@ export default {
detect() {
const { LocaleDetector } = NativeModules;
const [ lang, region ] = LocaleDetector.locale.replace(/_/, '-').split('-');
const locale = `${lang}${region}`;
return LocaleDetector.locale.replace(/[_-]/, '');
if (LANGUAGES.includes(locale)) {
return locale;
}
return lang;
},
init: Function.prototype,