2018-11-30 14:35:04 +00:00
|
|
|
import { NativeModules } from 'react-native';
|
2017-03-01 02:55:12 +00:00
|
|
|
|
2020-05-04 14:12:28 +00:00
|
|
|
import LANGUAGES_RESOURCES from '../../../../lang/languages.json';
|
|
|
|
|
|
|
|
const LANGUAGES = Object.keys(LANGUAGES_RESOURCES);
|
|
|
|
|
2017-03-01 02:55:12 +00:00
|
|
|
/**
|
|
|
|
* The singleton language detector for React Native which uses the system-wide
|
|
|
|
* locale.
|
|
|
|
*/
|
|
|
|
export default {
|
|
|
|
/**
|
|
|
|
* Does not support caching.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
cacheUserLanguage: Function.prototype,
|
|
|
|
|
|
|
|
detect() {
|
2018-11-30 14:35:04 +00:00
|
|
|
const { LocaleDetector } = NativeModules;
|
2021-06-06 07:22:13 +00:00
|
|
|
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;
|
|
|
|
}
|
2020-05-04 14:12:28 +00:00
|
|
|
|
|
|
|
if (LANGUAGES.includes(locale)) {
|
|
|
|
return locale;
|
|
|
|
}
|
2018-11-30 14:35:04 +00:00
|
|
|
|
2020-05-04 14:12:28 +00:00
|
|
|
return lang;
|
2017-03-01 02:55:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
init: Function.prototype,
|
|
|
|
|
|
|
|
type: 'languageDetector'
|
|
|
|
};
|