-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n-plugin.js
147 lines (122 loc) · 3.73 KB
/
i18n-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* This file is part of the Fxp package.
*
* (c) François Pluchino <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import BasePlugin from './plugin';
const LOCALES = {};
let globalLocale;
/**
* Base class for i18n plugin.
*/
export default class BaseI18nPlugin extends BasePlugin
{
/**
* Get the language configuration.
*
* @param {string} [locale] The ISO code of language
*
* @returns {object} The language configuration
*/
locale(locale) {
return this.constructor.locales[this.getLocale(locale)];
}
/**
* Get the valid available locale.
*
* @param {string} [locale] The ISO code of language
*
* @returns {object} The language configuration
*/
getLocale(locale) {
locale = locale ? locale : this.options.locale;
if (this.constructor.locales[locale]) {
return locale;
}
if (!locale) {
if (undefined === globalLocale) {
let metaLang = document.querySelector('head > meta[http-equiv="Content-Language"]');
globalLocale = metaLang && metaLang.content ? metaLang.content : null;
}
if (undefined === globalLocale) {
let lang = document.querySelector('html').lang;
globalLocale = lang ? lang : null;
}
locale = globalLocale;
}
if (typeof locale === 'string') {
locale = locale.toLowerCase().replace('-', '_');
if (locale.indexOf('_') >= 0 && undefined === this.constructor.locales[locale]) {
locale = locale.substr(0, locale.indexOf('_'));
}
}
if (undefined === this.constructor.locales[locale]) {
let localeKeys = Object.keys(this.constructor.locales);
locale = localeKeys.length > 0 ? localeKeys[0] : 'en';
}
this.options.locale = locale;
return locale;
}
/**
* Get the map of locales.
* The map consists of the key containing the ISO code of the language
* and an object containing the translations for each ISO code.
*
* Example:
* {
* 'en': {
* 'foo.bar': 'My message'
* }
* }
*
* @param {object} translations The translations map defined in a language ISO code key
*/
static set locales(translations) {
let keys, i, val;
// Force the initialisation of i18n options
this.defaultOptions = {};
if (typeof translations === 'object') {
keys = Object.keys(translations);
for (i = 0; i < keys.length; ++i) {
val = translations[keys[i]];
if (typeof val === 'object') {
if (undefined === LOCALES[this.name]) {
LOCALES[this.name] = {};
}
LOCALES[this.name][keys[i]] = val;
}
}
}
}
/**
* Get the map of locales.
* The map consists of the key containing the ISO code of the language
* and an object containing the translations for each ISO code.
*
* @returns {object}
*/
static get locales() {
if (undefined === LOCALES[this.name]) {
LOCALES[this.name] = {};
}
return LOCALES[this.name];
}
/**
* @inheritDoc
*/
static get defaultOptions() {
return super.defaultOptions;
}
/**
* @inheritDoc
*/
static set defaultOptions(options) {
if (undefined === options.locale) {
options.locale = null;
}
super.defaultOptions = options;
}
}