beelink/src/utils/i18n.ts
2020-10-19 15:24:55 +08:00

35 lines
713 B
TypeScript

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];
},
$s(){
return this.locale.value
}
});
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;
}