From 88135630eeee214e6a6777b325970084305a3028 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Wed, 30 Oct 2019 19:45:53 +0100 Subject: [PATCH] Add locales methods to i18n --- src/i18n/index.js | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/i18n/index.js b/src/i18n/index.js index ff2049255..6ce9cc433 100644 --- a/src/i18n/index.js +++ b/src/i18n/index.js @@ -25,11 +25,9 @@ import messages from './messages'; export default () => { - let em; - let config; const { language } = window.navigator || {}; const localeDef = language ? language.split('-')[0] : 'en'; - const configDef = { + const config = { locale: localeDef, localeFallback: 'en', counter: 'n', @@ -39,12 +37,34 @@ export default () => { return { name: 'I18n', + config, + /** * Get module configurations * @returns {Object} Configuration object */ getConfig() { - return config; + return this.config; + }, + + /** + * Update current locale + * @param {String} locale Locale value + * @returns {this} + * @example + * i18n.setLocale('it'); + */ + setLocale(locale) { + this.config.locale = locale; + return this; + }, + + /** + * Get current locale + * @returns {String} Current locale value + */ + getLocale() { + return this.config.locale; }, /** @@ -53,9 +73,7 @@ export default () => { * @private */ init(opts = {}) { - config = { ...configDef, ...opts }; - em = opts.em; - this.em = em; + this.em = opts.em; return this; } };