Browse Source

Merge pull request #3819 from nocodb/feat/language-alias

pull/3827/head
Braks 2 years ago committed by GitHub
parent
commit
4c1a8b251a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      packages/nc-gui/lib/enums.ts
  2. 8
      packages/nc-gui/plugins/a.i18n.ts
  3. 6
      packages/nc-gui/plugins/state.ts

5
packages/nc-gui/lib/enums.ts

@ -55,6 +55,11 @@ export enum Language {
'zh-Hant' = '繁體中文',
}
export enum LanguageAlias {
zn_CN = 'zh-Hans',
zh_TW = 'zh-Hant',
}
export enum NavigateDir {
NEXT,
PREV,

8
packages/nc-gui/plugins/a.i18n.ts

@ -2,6 +2,7 @@ import { defineNuxtPlugin } from 'nuxt/app'
import { createI18n } from 'vue-i18n'
import { nextTick } from 'vue'
import type { Language, NocoI18n } from '~/lib'
import { LanguageAlias } from '~/lib'
let globalI18n: NocoI18n
@ -26,7 +27,12 @@ export async function setI18nLanguage(locale: keyof typeof Language, i18n = glob
i18n.global.locale.value = locale
}
export async function loadLocaleMessages(locale: keyof typeof Language, i18n: NocoI18n = globalI18n) {
export async function loadLocaleMessages(
locale: keyof typeof Language | keyof typeof LanguageAlias,
i18n: NocoI18n = globalI18n,
) {
if (Object.keys(LanguageAlias).includes(locale)) locale = LanguageAlias[locale as keyof typeof LanguageAlias]
// load locale messages with dynamic import
const messages = await import(`../lang/${locale}.json`)

6
packages/nc-gui/plugins/state.ts

@ -1,5 +1,6 @@
import { defineNuxtPlugin, useApi, useGlobal } from '#imports'
import { loadLocaleMessages, setI18nLanguage } from '~/plugins/a.i18n'
import { Language, LanguageAlias } from '~/lib'
/**
* Initialize global state and watches for changes
@ -18,7 +19,10 @@ export default defineNuxtPlugin(async () => {
const { api } = useApi()
const currentLang = state.lang.value
let currentLang = state.lang.value
/** fall back to EN language if the current language cannot be found in Language or LanguagesAlias */
if (![...Object.keys(Language), ...Object.keys(LanguageAlias)].includes(currentLang)) state.lang.value = currentLang = 'en'
/** force load initial locale messages */
await loadLocaleMessages(currentLang)

Loading…
Cancel
Save