✨ Add i18n support
This commit is contained in:
55
frontend/src/i18n/index.ts
Normal file
55
frontend/src/i18n/index.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import {createI18n} from 'vue-i18n';
|
||||
import {useStorage} from '@vueuse/core';
|
||||
import messages from './locales';
|
||||
|
||||
// 定义支持的语言类型
|
||||
export type SupportedLocaleType = 'zh-CN' | 'en-US';
|
||||
|
||||
// 支持的语言列表
|
||||
export const SUPPORTED_LOCALES = [
|
||||
{
|
||||
code: 'zh-CN' as SupportedLocaleType,
|
||||
name: '简体中文'
|
||||
},
|
||||
{
|
||||
code: 'en-US' as SupportedLocaleType,
|
||||
name: 'English'
|
||||
}
|
||||
];
|
||||
|
||||
// 获取浏览器的默认语言
|
||||
const getBrowserLanguage = (): SupportedLocaleType => {
|
||||
const browserLang = navigator.language;
|
||||
const langCode = browserLang.split('-')[0];
|
||||
|
||||
// 检查是否支持此语言
|
||||
const supportedLang = SUPPORTED_LOCALES.find(locale =>
|
||||
locale.code.startsWith(langCode) || locale.code.split('-')[0] === langCode
|
||||
);
|
||||
|
||||
return supportedLang?.code || 'zh-CN'; // 默认为中文
|
||||
};
|
||||
|
||||
const storedLocale = useStorage<SupportedLocaleType>('voidraft-language', getBrowserLanguage());
|
||||
|
||||
// 创建i18n实例
|
||||
const i18n = createI18n({
|
||||
lobalInjection: true,
|
||||
locale: storedLocale.value,
|
||||
fallbackLocale: 'zh-CN' as SupportedLocaleType,
|
||||
messages
|
||||
});
|
||||
|
||||
// 切换语言的方法
|
||||
export const setLocale = (locale: SupportedLocaleType) => {
|
||||
if (SUPPORTED_LOCALES.some(l => l.code === locale)) {
|
||||
storedLocale.value = locale;
|
||||
i18n.global.locale = locale;
|
||||
document.documentElement.setAttribute('lang', locale);
|
||||
}
|
||||
};
|
||||
|
||||
// 初始设置html lang属性
|
||||
document.documentElement.setAttribute('lang', storedLocale.value);
|
||||
|
||||
export default i18n;
|
33
frontend/src/i18n/locales/en-US.ts
Normal file
33
frontend/src/i18n/locales/en-US.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
export default {
|
||||
toolbar: {
|
||||
editor: {
|
||||
lines: 'Ln',
|
||||
characters: 'Ch',
|
||||
selected: 'Sel'
|
||||
},
|
||||
fontSize: 'Font Size',
|
||||
fontSizeTooltip: 'Font Size (Ctrl+wheel to adjust)',
|
||||
tabLabel: 'Tab',
|
||||
tabType: {
|
||||
spaces: 'Spaces',
|
||||
tab: 'Tab'
|
||||
},
|
||||
encoding: 'UTF-8',
|
||||
settings: 'Settings'
|
||||
},
|
||||
config: {
|
||||
loadSuccess: 'Configuration loaded successfully',
|
||||
loadFailed: 'Failed to load configuration',
|
||||
saveSuccess: 'Configuration saved',
|
||||
saveFailed: 'Failed to save configuration',
|
||||
resetSuccess: 'Configuration reset to defaults',
|
||||
resetFailed: 'Failed to reset configuration',
|
||||
fontSizeFixed: 'Font size ({value}) has been corrected to {fixed}',
|
||||
tabSizeFixed: 'Tab size ({value}) has been corrected to {fixed}',
|
||||
tabTypeFixed: 'Tab type ({value}) is invalid, corrected to spaces'
|
||||
},
|
||||
languages: {
|
||||
'zh-CN': '简体中文',
|
||||
'en-US': 'English'
|
||||
}
|
||||
};
|
7
frontend/src/i18n/locales/index.ts
Normal file
7
frontend/src/i18n/locales/index.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import zhCN from './zh-CN';
|
||||
import enUS from './en-US';
|
||||
|
||||
export default {
|
||||
'zh-CN': zhCN,
|
||||
'en-US': enUS
|
||||
};
|
33
frontend/src/i18n/locales/zh-CN.ts
Normal file
33
frontend/src/i18n/locales/zh-CN.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
export default {
|
||||
toolbar: {
|
||||
editor: {
|
||||
lines: 'Ln',
|
||||
characters: 'Ch',
|
||||
selected: 'Sel'
|
||||
},
|
||||
fontSize: '字体大小',
|
||||
fontSizeTooltip: '字体大小 (Ctrl+滚轮调整)',
|
||||
tabLabel: 'Tab',
|
||||
tabType: {
|
||||
spaces: '空格',
|
||||
tab: '制表符'
|
||||
},
|
||||
encoding: 'UTF-8',
|
||||
settings: '设置'
|
||||
},
|
||||
config: {
|
||||
loadSuccess: '配置加载成功',
|
||||
loadFailed: '配置加载失败',
|
||||
saveSuccess: '配置已保存',
|
||||
saveFailed: '配置保存失败',
|
||||
resetSuccess: '配置已重置为默认值',
|
||||
resetFailed: '重置配置失败',
|
||||
fontSizeFixed: '字体大小值({value})已被修正为{fixed}',
|
||||
tabSizeFixed: 'Tab大小值({value})已被修正为{fixed}',
|
||||
tabTypeFixed: 'Tab类型({value})不合法,已修正为空格'
|
||||
},
|
||||
languages: {
|
||||
'zh-CN': '简体中文',
|
||||
'en-US': 'English'
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user