From 448a305abe68fae4d78c7325baa0b6f9ada1b9b1 Mon Sep 17 00:00:00 2001 From: luyuan <1162963624@qq.com> Date: Mon, 19 Oct 2020 15:06:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86i18n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/i18n.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/utils/i18n.ts diff --git a/src/utils/i18n.ts b/src/utils/i18n.ts new file mode 100644 index 0000000..e9ae3b4 --- /dev/null +++ b/src/utils/i18n.ts @@ -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; +} \ No newline at end of file