Skip to content

Commit

Permalink
fix: lang of twikoo.init is no longer case sensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
imaegoo committed Nov 17, 2023
1 parent a04dcde commit 4aaa908
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/client/utils/i18n/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@ const langs = {
'ja-jp': 5
}

let userLanguage = ''
const defaultLanguage = 'zh-cn'
let twikooLangOption = ''

const setLanguage = (options = {}) => {
userLanguage = options.lang in langs ? options.lang : navigator.language
if (options.lang && options.lang.toLowerCase() in langs) {
twikooLangOption = options.lang
}
}

const translate = (key, language) => {
const lang = (language || userLanguage || navigator.language).toLowerCase()
// 优先级: translate 入参 > twikoo.init 入参 > 浏览器语言设置 > 默认语言
const lang = (language || twikooLangOption || navigator.language).toLowerCase()
let value
if (lang && langs[lang]) {
value = i18n[key][langs[lang]]
} else {
value = i18n[key][langs['zh-cn']]
value = i18n[key][langs[defaultLanguage]]
}
return value || ''
}
Expand Down

0 comments on commit 4aaa908

Please sign in to comment.