Total Complexity | 6 |
Complexity/F | 2 |
Lines of Code | 30 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | // mini translator |
||
3 | import en from './translations/en' |
||
4 | |||
5 | |||
6 | const Translator = function (translations, separator = '.') { |
||
7 | let lang = window.navigator.languages[1] || window.navigator.userLanguage[1] |
||
8 | this.lang = typeof translations[lang] !== 'undefined' ? lang : 'en' |
||
9 | this.separator = separator |
||
10 | this.translations = translations |
||
11 | } |
||
12 | |||
13 | Translator.prototype.get = function (route) { |
||
14 | let parts = route.split(this.separator) |
||
15 | let translation = this.translations[this.lang] |
||
16 | |||
17 | for (let i = 0; i < parts.length; i++) { |
||
18 | translation = translation[parts[i]] |
||
19 | if (translation === undefined) { |
||
20 | translation = 'No Translation' |
||
21 | break |
||
22 | } |
||
23 | } |
||
24 | |||
25 | return translation |
||
26 | } |
||
27 | |||
28 | let T = new Translator({en}) |
||
29 | |||
30 | export default function (n) { |
||
31 | return T.get(n) |
||
32 | } |