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