添加了i18n
This commit is contained in:
parent
f18aa0c43d
commit
448a305abe
32
src/utils/i18n.ts
Normal file
32
src/utils/i18n.ts
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user