You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

languageDetector.native.js 782B

123456789101112131415161718192021222324252627282930313233343536
  1. // @flow
  2. import { NativeModules } from 'react-native';
  3. import LANGUAGES_RESOURCES from '../../../../lang/languages.json';
  4. const LANGUAGES = Object.keys(LANGUAGES_RESOURCES);
  5. /**
  6. * The singleton language detector for React Native which uses the system-wide
  7. * locale.
  8. */
  9. export default {
  10. /**
  11. * Does not support caching.
  12. *
  13. * @returns {void}
  14. */
  15. cacheUserLanguage: Function.prototype,
  16. detect() {
  17. const { LocaleDetector } = NativeModules;
  18. const [ lang, region ] = LocaleDetector.locale.replace(/_/, '-').split('-');
  19. const locale = `${lang}${region}`;
  20. if (LANGUAGES.includes(locale)) {
  21. return locale;
  22. }
  23. return lang;
  24. },
  25. init: Function.prototype,
  26. type: 'languageDetector'
  27. };