-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add i18n support for the web ui
- Loading branch information
1 parent
37700ec
commit 699cc4d
Showing
11 changed files
with
285 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { nextTick, isRef } from 'vue' | ||
import { createI18n } from 'vue-i18n' | ||
|
||
import type { | ||
I18n, | ||
I18nOptions, | ||
Locale, | ||
VueI18n, | ||
Composer, | ||
I18nMode | ||
} from 'vue-i18n' | ||
|
||
export const SUPPORT_LOCALES = ['en', 'ja'] | ||
|
||
function isComposer( | ||
instance: VueI18n | Composer, | ||
mode: I18nMode | ||
): instance is Composer { | ||
return mode === 'composition' && isRef(instance.locale) | ||
} | ||
|
||
export function getLocale(i18n: I18n): string { | ||
if (isComposer(i18n.global, i18n.mode)) { | ||
return i18n.global.locale.value | ||
} else { | ||
return i18n.global.locale | ||
} | ||
} | ||
|
||
export function setLocale(i18n: I18n, locale: Locale): void { | ||
if (isComposer(i18n.global, i18n.mode)) { | ||
i18n.global.locale.value = locale | ||
} else { | ||
i18n.global.locale = locale | ||
} | ||
} | ||
|
||
export function setupI18n(options: I18nOptions = { locale: 'en' }): I18n { | ||
const i18n = createI18n(options) | ||
setI18nLanguage(i18n, options.locale!) | ||
return i18n | ||
} | ||
|
||
export function setI18nLanguage(i18n: I18n, locale: Locale): void { | ||
setLocale(i18n, locale) | ||
/** | ||
* NOTE: | ||
* If you need to specify the language setting for headers, such as the `fetch` API, set it here. | ||
* The following is an example for axios. | ||
* | ||
* axios.defaults.headers.common['Accept-Language'] = locale | ||
*/ | ||
document.querySelector('html')!.setAttribute('lang', locale) | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const getResourceMessages = (r: any) => r.default || r | ||
|
||
export async function loadLocaleMessages(i18n: I18n, locale: Locale) { | ||
// load locale messages | ||
const messages = await import( | ||
/* @vite-ignore */ `./locales/${locale}.json` | ||
).then(getResourceMessages) | ||
|
||
// set locale and locale message | ||
i18n.global.setLocaleMessage(locale, messages) | ||
|
||
return nextTick() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"button": { | ||
"import": "Import", | ||
"export": "Export", | ||
"new": "New", | ||
"submit": "Submit", | ||
"save": "Save", | ||
"delete": "Delete", | ||
"send": "Send", | ||
"toolbox": "Tool Box", | ||
"newtestcase": "New TestCase" | ||
}, | ||
"tip": { | ||
"filter": "Filter Keyword" | ||
}, | ||
"field": { | ||
"name": "Name" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"button": { | ||
"import": "导入", | ||
"export": "导出", | ||
"new": "新建", | ||
"submit": "提交", | ||
"save": "保存", | ||
"delete": "删除", | ||
"send": "发送", | ||
"toolbox": "工具箱", | ||
"newtestcase": "新建测试用例" | ||
}, | ||
"tip": { | ||
"filter": "过滤" | ||
}, | ||
"field": { | ||
"name": "名称" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.