添加了i18n

This commit is contained in:
luyuan 2020-10-19 15:06:08 +08:00
parent f18aa0c43d
commit 448a305abe
Signed by: theluyuan
GPG Key ID: A7972FD973317FF3

32
src/utils/i18n.ts Normal file
View File

@ -0,0 +1,32 @@
import { inject, provide, ref } from 'vue';
interface Language {
[key :string]: string
}
interface Config {
locale: string;
messages: {[key: string]: Language}
}
const createI18n = (config: Config) => ({
locale: ref(config.locale),
messages: config.messages,
$t(key: string) {
return this.messages[this.locale.value][key];
}
});
const i18nSymbol = Symbol();
export function provideI18n(i18nConfig: Config) {
const i18n = createI18n(i18nConfig);
provide(i18nSymbol, i18n);
}
export function useI18n() {
const i18n = inject(i18nSymbol);
if (!i18n) throw new Error("No i18n provided!!!");
return i18n;
}