diff --git a/src/components/MainToolbar.vue b/src/components/MainToolbar.vue index 1867850..f87d41b 100644 --- a/src/components/MainToolbar.vue +++ b/src/components/MainToolbar.vue @@ -6,9 +6,17 @@ class="app-bar pl-2" > - - - qBittorrent Web UI + + + + qBittorrent Web UI + { + let locales: {}[] = Object.entries(translations).map(([lang, translation]) => { return { - text: value, - value: key, + text: translation.lang, + value: lang, }; }); + locales = [ + { + text: tr('auto'), + value: null, + }, + ...locales + ] + const locale = i18n.locale(); return { - locales: locales_, + locales, oldLocale: locale, currentLocale: locale, }; @@ -58,15 +74,16 @@ export default Vue.extend({ toggle() { this.$emit('input', !this.value); }, - async switchLocale(locale: string) { + async switchLocale(locale: keyof typeof translations | null) { if (locale === this.oldLocale) { return; } const confirm = await new Promise((resolve) => { + const localeKey = !locale ? defaultLocale : locale this.showDialog({ content: { - text: tr('dialog.switch_locale.msg', { lang: locales[locale] }), + text: tr('dialog.switch_locale.msg', { lang: translations[localeKey].lang }), type: DialogType.OkCancel, callback: resolve, }, diff --git a/src/locale/en.ts b/src/locale/en.ts index 1d13619..17f4dc0 100644 --- a/src/locale/en.ts +++ b/src/locale/en.ts @@ -1,5 +1,7 @@ +/* eslint-disable @typescript-eslint/camelcase */ export default { lang: 'English', + auto: 'Auto', close: 'Close', no: 'No', diff --git a/src/locale/index.ts b/src/locale/index.ts index 12e4107..4e8180f 100644 --- a/src/locale/index.ts +++ b/src/locale/index.ts @@ -1,38 +1,45 @@ import Polyglot from 'node-polyglot'; import en from './en'; +import zhCn from './zh-CN'; import { loadConfig } from '@/store/config'; +export const translations = { + en: en, + 'zh-CN': zhCn, +} + const polyglot = new Polyglot({ - phrases: en, + phrases: translations.en }); -export const locales: {[key: string]: string} = { - en: 'English', - 'zh-CN': '中文', -}; +function matchLocale() { + const { languages } = navigator; -function updateLocale() { - let locale: string | undefined = loadConfig()['locale']; - if (!locale) { - const { languages } = navigator; - - for (const code of languages) { - if (code in locales) { - locale = code; - break; - } + for (const code of languages) { + if (code in translations) { + return code as keyof typeof translations; } } - if (!locale || locale === 'en') { + return 'en' +} + +export const defaultLocale = matchLocale() + +function updateLocale() { + let locale: keyof typeof translations | null = loadConfig()['locale']; + + if (!locale) { + locale = defaultLocale; + } + + if (locale === polyglot.locale()) { return; } polyglot.locale(locale); - - const { default: translation } = require('./' + locale); - polyglot.extend(translation); + polyglot.extend(translations[locale]); } updateLocale(); diff --git a/src/locale/zh-CN.ts b/src/locale/zh-CN.ts index 05d176a..1393367 100644 --- a/src/locale/zh-CN.ts +++ b/src/locale/zh-CN.ts @@ -1,5 +1,7 @@ +/* eslint-disable @typescript-eslint/camelcase */ export default { lang: '中文', + auto: '自动', close: '关闭', no: '否',