diff --git a/src/lang/index.js b/src/lang/index.js index 7c9c8af67b1..c443f41bf95 100644 --- a/src/lang/index.js +++ b/src/lang/index.js @@ -24,11 +24,24 @@ const messages = { ...elementEsLocale } } +export function getLanguage() { + const chooseLanguage = Cookies.get('language') + if (chooseLanguage) return chooseLanguage + // if has not choose language + const language = (navigator.language || navigator.browserLanguage).toLowerCase() + const locales = Object.keys(messages) + for (const locale of locales) { + if (language.indexOf(locale) > -1) { + return locale + } + } + return 'en' +} const i18n = new VueI18n({ // set locale // options: en | zh | es - locale: Cookies.get('language') || 'en', + locale: getLanguage(), // set locale messages messages }) diff --git a/src/store/index.js b/src/store/index.js index 4842ea7e43a..70736d3ad35 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -1,24 +1,24 @@ import Vue from 'vue' import Vuex from 'vuex' -import app from './modules/app' -import errorLog from './modules/errorLog' -import permission from './modules/permission' -import tagsView from './modules/tagsView' -import settings from './modules/settings' -import user from './modules/user' import getters from './getters' Vue.use(Vuex) +// https://webpack.js.org/guides/dependency-management/#requirecontext +const modulesFiles = require.context('./modules', false, /\.js$/) + +// you do not need `import app from './modules/app'` +// it will auto require all vuex module from modules file +const modules = modulesFiles.keys().reduce((modules, modulePath) => { + // set './app.js' => 'app' + const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') + const value = modulesFiles(modulePath) + modules[moduleName] = value.default + return modules +}, {}) + const store = new Vuex.Store({ - modules: { - app, - errorLog, - permission, - tagsView, - settings, - user - }, + modules, getters }) diff --git a/src/store/modules/app.js b/src/store/modules/app.js index 84875dd6481..73616469bd4 100644 --- a/src/store/modules/app.js +++ b/src/store/modules/app.js @@ -1,4 +1,5 @@ import Cookies from 'js-cookie' +import { getLanguage } from '@/lang/index' const state = { sidebar: { @@ -6,7 +7,7 @@ const state = { withoutAnimation: false }, device: 'desktop', - language: Cookies.get('language') || 'en', + language: getLanguage(), size: Cookies.get('size') || 'medium' } diff --git a/src/store/modules/errorLog.js b/src/store/modules/errorLog.js index c97d452a040..8e3db060783 100644 --- a/src/store/modules/errorLog.js +++ b/src/store/modules/errorLog.js @@ -1,4 +1,3 @@ - const state = { logs: [] } diff --git a/src/store/modules/tagsView.js b/src/store/modules/tagsView.js index 5cbe32f32c3..3e2c17036ca 100644 --- a/src/store/modules/tagsView.js +++ b/src/store/modules/tagsView.js @@ -1,4 +1,3 @@ - const state = { visitedViews: [], cachedViews: []