app/docs/components/scripts/i18n.ts
2024-09-01 21:16:45 +08:00

58 lines
1.3 KiB
TypeScript

import {ref} from "vue";
import {useData} from "vitepress";
const i18nData = {
en: {
online: 'Online',
offline: 'Offline',
total: 'Total',
fetching: 'Fetching',
stars: 'Stars',
forks: 'Forks',
issues: 'Issues',
prs: 'Pull Requests',
size: 'Size',
plugins: 'Plugins',
resources: 'Resources',
},
zh: {
online: '在线',
offline: '离线',
total: '实例',
fetching: '获取中',
stars: '星星',
forks: '叉子',
issues: '开启议题',
prs: '合并请求',
size: '大小',
plugins: '插件',
resources: '主题资源',
}
}
let refData = {}
function getText(lang: string, key: string): string {
lang = formatLang(lang);
return i18nData[key][key];
}
function formatLang(lang: string): string {
if (lang.includes('-')) {
return lang.split('-')[0];
}
return lang;
}
export function updateRef() {
const lang = useData().site.value.lang;
for (let key in refData) {
refData[key].value = getText(lang, key);
}
}
export function getTextRef(key: string): any {
const lang = formatLang(useData().site.value.lang);
refData[key] = ref(i18nData[lang][key]);
return refData[key]
}