Skip to content

Commit

Permalink
Find locale in meta content language or html tag if it is not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispluchino committed Jun 17, 2018
1 parent e313211 commit 446d76d
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions js/i18n-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,35 @@ export default class BaseI18nPlugin extends BasePlugin
* @returns {object} The language configuration
*/
locale(locale) {
if (!locale) {
locale = this.options.locale;
return this.constructor.locales[this.getLocale(locale)];
}

if (!locale) {
if (undefined === globalLocale) {
let lang = document.querySelector('html').lang;
globalLocale = lang ? lang : null;
}
/**
* 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;
}

locale = globalLocale;
if (undefined === globalLocale) {
let lang = document.querySelector('html').lang;
globalLocale = lang ? lang : null;
}

locale = globalLocale;
}

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

return this.constructor.locales[locale];
this.options.locale = locale;

return locale;
}

/**
Expand Down

0 comments on commit 446d76d

Please sign in to comment.