Skip to content

Commit 446d76d

Browse files
Find locale in meta content language or html tag if it is not defined
1 parent e313211 commit 446d76d

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

js/i18n-plugin.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,35 @@ export default class BaseI18nPlugin extends BasePlugin
2626
* @returns {object} The language configuration
2727
*/
2828
locale(locale) {
29-
if (!locale) {
30-
locale = this.options.locale;
29+
return this.constructor.locales[this.getLocale(locale)];
30+
}
3131

32-
if (!locale) {
33-
if (undefined === globalLocale) {
34-
let lang = document.querySelector('html').lang;
35-
globalLocale = lang ? lang : null;
36-
}
32+
/**
33+
* Get the valid available locale.
34+
*
35+
* @param {string} [locale] The ISO code of language
36+
*
37+
* @returns {object} The language configuration
38+
*/
39+
getLocale(locale) {
40+
locale = locale ? locale : this.options.locale;
41+
42+
if (this.constructor.locales[locale]) {
43+
return locale;
44+
}
45+
46+
if (!locale) {
47+
if (undefined === globalLocale) {
48+
let metaLang = document.querySelector('head > meta[http-equiv="Content-Language"]');
49+
globalLocale = metaLang && metaLang.content ? metaLang.content : null;
50+
}
3751

38-
locale = globalLocale;
52+
if (undefined === globalLocale) {
53+
let lang = document.querySelector('html').lang;
54+
globalLocale = lang ? lang : null;
3955
}
56+
57+
locale = globalLocale;
4058
}
4159

4260
if (typeof locale === 'string') {
@@ -52,7 +70,9 @@ export default class BaseI18nPlugin extends BasePlugin
5270
locale = localeKeys.length > 0 ? localeKeys[0] : 'en';
5371
}
5472

55-
return this.constructor.locales[locale];
73+
this.options.locale = locale;
74+
75+
return locale;
5676
}
5777

5878
/**

0 commit comments

Comments
 (0)