Merge pull request #33 from shaohaojiecoder/i18n

fix local storage for defalut lang
This commit is contained in:
pycook 2020-02-16 14:51:59 +08:00 committed by GitHub
commit a188218818
3 changed files with 13 additions and 13 deletions

View File

@ -48,7 +48,7 @@
import NoticeIcon from '@/components/NoticeIcon'
import { mapActions, mapGetters } from 'vuex'
import LangSelect from '@/components/tools/LangSelect'
import { showLocale } from '@/locales'
import config from '@/config/defaultSettings'
export default {
name: 'UserMenu',
@ -57,7 +57,7 @@ export default {
},
data () {
return {
showLocale: showLocale
showLocale: config.showLocale
}
},
methods: {

View File

@ -14,6 +14,8 @@
*/
export default {
showLocale: true,
defaultLang: 'zh-CN',
useSSO: false,
primaryColor: '#1890ff', // primary color of ant design
navTheme: 'dark', // theme for nav menu

View File

@ -6,12 +6,10 @@ import enUS from './lang/en-US'
import zhCN from './lang/zh-CN'
// change default accept-language
import { axios } from '@/utils/request'
import config from '@/config/defaultSettings'
Vue.use(VueI18n)
export const defaultLang = 'zh-CN'
export const showLocale = true
const messages = {
'en-US': {
...enUS
@ -22,14 +20,14 @@ const messages = {
}
const i18n = new VueI18n({
locale: defaultLang,
fallbackLocale: defaultLang,
locale: config.defaultLang,
fallbackLocale: config.defaultLang,
messages
})
export default i18n
const loadedLanguages = [defaultLang]
const loadedLanguages = [config.defaultLang]
function setI18nLanguage (lang) {
i18n.locale = lang
@ -42,9 +40,9 @@ export function i18nRender (key) {
return i18n.t(key)
}
export function loadLanguageAsync (lang = defaultLang) {
export function loadLanguageAsync (lang = config.defaultLang) {
return new Promise(resolve => {
Vue.ls.set('lang', lang)
localStorage.setItem('lang', lang)
if (i18n.locale !== lang) {
if (!loadedLanguages.includes(lang)) {
return import(/* webpackChunkName: "lang-[request]" */ `./lang/${lang}`).then(msg => {
@ -59,6 +57,6 @@ export function loadLanguageAsync (lang = defaultLang) {
})
}
// if (Vue.ls.get('lang') !== null && defaultLang !== Vue.ls.get('lang')) {
// loadLanguageAsync(localStorage.lang)
// }
if (localStorage.getItem('lang') !== null && config.defaultLang !== localStorage.getItem('lang')) {
loadLanguageAsync(localStorage.lang)
}