Skip to content

Commit

Permalink
refactor: Simplify the locale mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Jan 29, 2023
1 parent 9cdd6f4 commit e30367b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
11 changes: 10 additions & 1 deletion xmcl-electron-app/main/controllers/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ import Controller from '@/Controller'
import { BaseService } from '@xmcl/runtime'
import { ControllerPlugin } from './plugin'

export const localeMappings: Record<string, string> = {
'zh-CN': '简体中文',
'zh-TW': '繁體中文',
en: 'English',
ru: 'Русский язык',
'es-ES': 'Español',
fr: 'French',
}

export const i18n: ControllerPlugin = function (this: Controller) {
this.app.once('engine-ready', () => {
const baseService = this.app.serviceManager.get(BaseService)
baseService.state.localesSet(['en', 'zh-CN', 'zh-TW', 'ru', 'es-ES'])
baseService.state.localesSet(Object.entries(localeMappings).map(([locale, name]) => ({ locale, name })))
this.app.log(`Set locale for the app ${baseService.state.locales}`)
this.i18n.use(baseService.state.locale)
this.app.serviceStateManager.subscribe('config', (c) => {
Expand Down
6 changes: 2 additions & 4 deletions xmcl-keystone-ui/src/composables/setting.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { useService, useServiceBusy } from '@/composables'
import { BaseServiceKey } from '@xmcl/runtime-api'
import { useServiceBusy, useService } from '@/composables'
import { injection } from '@/util/inject'
import { kUILayout } from './uiLayout'

export function useUpdateSettings() {
const { state, checkUpdate } = useService(BaseServiceKey)
Expand Down Expand Up @@ -42,7 +40,7 @@ export function useSettings() {
const root = computed(() => state.root)
const locales = computed(() => state.locales || [])
const selectedLocale = computed({
get: () => locales.value.find(l => l === state.locale) || 'en',
get: () => locales.value.find(l => l.locale === state.locale)?.locale || 'en',
set: v => state.localeSet(v),
})
const allowPrerelease = computed({
Expand Down
11 changes: 0 additions & 11 deletions xmcl-keystone-ui/src/util/localeMappings.ts

This file was deleted.

3 changes: 1 addition & 2 deletions xmcl-keystone-ui/src/views/SettingGeneral.vue
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@
<script lang="ts" setup>
import { BaseServiceKey } from '@xmcl/runtime-api'
import { useDialog } from '../composables/dialog'
import { localeMappings } from '@/util/localeMappings'
import { useSettings } from '../composables/setting'
import { useService } from '@/composables'
Expand Down Expand Up @@ -189,7 +188,7 @@ const apiSetItems = computed(() =>
value: v.name,
}
})))
const locales = rawLocales.value.map(l => ({ text: localeMappings[l] ?? l, value: l }))
const locales = computed(() => rawLocales.value.map(({ locale, name }) => ({ text: name, value: locale })))
const { show } = useDialog('migration')
Expand Down
23 changes: 21 additions & 2 deletions xmcl-runtime-api/src/services/BaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ export class BaseState implements SettingSchema {
/**
* All supported languages of the launcher
*/
locales: string[] = []
locales: {
/**
* The i18n locale key
*/
locale: string
/**
* The human readable name of the locale
*/
name: string
}[] = []

updateInfo: ReleaseInfo | null = null
updateStatus: 'ready' | 'none' | 'pending' = 'none'
allowPrerelease = false
Expand Down Expand Up @@ -99,7 +109,16 @@ export class BaseState implements SettingSchema {
this.locale = language
}

localesSet(languages: string[]) {
localesSet(languages: {
/**
* The i18n locale key
*/
locale: string
/**
* The human readable name of the locale
*/
name: string
}[]) {
this.locales = languages
}

Expand Down

0 comments on commit e30367b

Please sign in to comment.