您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

languageDetector.native.js 1023B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 parts = LocaleDetector.locale.replace(/_/, '-').split('-');
  19. const [ lang, regionOrScript, region ] = parts;
  20. let locale;
  21. if (parts.length >= 3) {
  22. locale = `${lang}${region}`;
  23. } else if (parts.length === 2) {
  24. locale = `${lang}${regionOrScript}`;
  25. } else {
  26. locale = lang;
  27. }
  28. if (LANGUAGES.includes(locale)) {
  29. return locale;
  30. }
  31. return lang;
  32. },
  33. init: Function.prototype,
  34. type: 'languageDetector'
  35. };