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

customNavigatorDetector.js 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* @flow */
  2. declare var navigator: Object;
  3. /**
  4. * Custom language detection, just returns the config property if any.
  5. */
  6. export default {
  7. /**
  8. * Does not support caching.
  9. *
  10. * @returns {void}
  11. */
  12. cacheUserLanguage: Function.prototype,
  13. /**
  14. * Looks the language up in the config.
  15. *
  16. * @returns {string} The default language if any.
  17. */
  18. lookup() {
  19. let found = [];
  20. if (typeof navigator !== 'undefined') {
  21. if (navigator.languages) {
  22. // chrome only; not an array, so can't use .push.apply instead of iterating
  23. for (let i = 0; i < navigator.languages.length; i++) {
  24. found.push(navigator.languages[i]);
  25. }
  26. }
  27. if (navigator.userLanguage) {
  28. found.push(navigator.userLanguage);
  29. }
  30. if (navigator.language) {
  31. found.push(navigator.language);
  32. }
  33. }
  34. // Fix language format (en-US => enUS)
  35. found = found.map<string>(f => f.replace(/[-_]+/g, ''));
  36. return found.length > 0 ? found : undefined;
  37. },
  38. /**
  39. * Name of the language detector.
  40. */
  41. name: 'customNavigatorDetector'
  42. };