mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-08-08 23:14:07 +08:00
✨ Feature: finish i18n system
This commit is contained in:
72
src/renderer/i18n/index.ts
Normal file
72
src/renderer/i18n/index.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { ipcRenderer } from 'electron'
|
||||
import { ObjectAdapter, I18n } from '@picgo/i18n'
|
||||
import { GET_CURRENT_LANGUAGE, SET_CURRENT_LANGUAGE, FORCE_UPDATE, GET_LANGUAGE_LIST } from '#/events/constants'
|
||||
import bus from '@/utils/bus'
|
||||
|
||||
export class I18nManager {
|
||||
private i18n: I18n | null = null
|
||||
private i18nFileList: II18nItem[] = [{
|
||||
label: '简体中文',
|
||||
value: 'zh-CN'
|
||||
}, {
|
||||
label: 'English',
|
||||
value: 'en'
|
||||
}]
|
||||
|
||||
private getLanguageList () {
|
||||
ipcRenderer.send(GET_LANGUAGE_LIST)
|
||||
ipcRenderer.once(GET_LANGUAGE_LIST, (event, list: II18nItem[]) => {
|
||||
this.i18nFileList = list
|
||||
})
|
||||
}
|
||||
|
||||
private getCurrentLanguage () {
|
||||
ipcRenderer.send(GET_CURRENT_LANGUAGE)
|
||||
ipcRenderer.once(GET_CURRENT_LANGUAGE, (event, lang: string, locales: ILocales) => {
|
||||
this.setLocales(lang, locales)
|
||||
bus.$emit(FORCE_UPDATE)
|
||||
})
|
||||
}
|
||||
|
||||
private setLocales (lang: string, locales: ILocales) {
|
||||
const objectAdapter = new ObjectAdapter({
|
||||
[lang]: locales
|
||||
})
|
||||
this.i18n = new I18n({
|
||||
adapter: objectAdapter,
|
||||
defaultLanguage: lang
|
||||
})
|
||||
}
|
||||
|
||||
constructor () {
|
||||
this.getCurrentLanguage()
|
||||
this.getLanguageList()
|
||||
ipcRenderer.on(SET_CURRENT_LANGUAGE, (event, lang: string, locales: ILocales) => {
|
||||
this.setLocales(lang, locales)
|
||||
bus.$emit(FORCE_UPDATE)
|
||||
})
|
||||
}
|
||||
|
||||
T (key: ILocalesKey, args: IStringKeyMap = {}): string {
|
||||
return this.i18n?.translate(key, args) || key
|
||||
}
|
||||
|
||||
setCurrentLanguage (lang: string) {
|
||||
ipcRenderer.send(SET_CURRENT_LANGUAGE, lang)
|
||||
}
|
||||
|
||||
get languageList () {
|
||||
return this.i18nFileList
|
||||
}
|
||||
}
|
||||
|
||||
const i18nManager = new I18nManager()
|
||||
|
||||
const T = (key: ILocalesKey, args: IStringKeyMap = {}): string => {
|
||||
return i18nManager.T(key, args)
|
||||
}
|
||||
|
||||
export {
|
||||
i18nManager,
|
||||
T
|
||||
}
|
||||
@@ -386,12 +386,12 @@
|
||||
import keyDetect from '@/utils/key-binding'
|
||||
import pkg from 'root/package.json'
|
||||
import { IConfig } from 'picgo'
|
||||
import { PICGO_OPEN_FILE, OPEN_URL, CHANGE_LANGUAGE } from '#/events/constants'
|
||||
import { PICGO_OPEN_FILE, OPEN_URL } from '#/events/constants'
|
||||
import {
|
||||
ipcRenderer
|
||||
} from 'electron'
|
||||
import { Component, Vue } from 'vue-property-decorator'
|
||||
import { T, languageList } from '~/universal/i18n'
|
||||
import { T, i18nManager } from '@/i18n/index'
|
||||
import { enforceNumber } from '~/universal/utils/common'
|
||||
import { getLatestVersion } from '#/utils/getLatestVersion'
|
||||
import { compare } from 'compare-versions'
|
||||
@@ -424,9 +424,9 @@ export default class extends Vue {
|
||||
logFileSizeLimit: 10
|
||||
}
|
||||
|
||||
languageList = languageList.map(item => ({
|
||||
label: item,
|
||||
value: item
|
||||
languageList = i18nManager.languageList.map(item => ({
|
||||
label: item.label,
|
||||
value: item.value
|
||||
}))
|
||||
|
||||
currentLanguage = 'zh-CN'
|
||||
@@ -759,9 +759,7 @@ export default class extends Vue {
|
||||
}
|
||||
|
||||
handleLanguageChange (val: string) {
|
||||
this.$i18n.setLanguage(val)
|
||||
this.forceUpdate()
|
||||
ipcRenderer.send(CHANGE_LANGUAGE, val)
|
||||
this.$i18n.setCurrentLanguage(val)
|
||||
this.saveConfig({
|
||||
'settings.language': val
|
||||
})
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
import { i18n } from '~/universal/i18n'
|
||||
|
||||
export const handleURLParams = () => {
|
||||
const url = new URL(location.href)
|
||||
const search = new URLSearchParams(url.search)
|
||||
if (search.has('lang')) {
|
||||
const lang = search.get('lang') || 'zh-CN'
|
||||
i18n.setLanguage(lang)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user