Skip to content

Commit

Permalink
add $i18n instance --build
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronlamz committed Feb 22, 2022
1 parent a79a2c8 commit 2e60ad8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/vite-project/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

<template>
<h2>Current Lang : {{ currentLocale }}</h2>
<h2>Current Lang from $i18n.currentLocale: {{ currentLocale }}</h2>
<h2>Test Cases:</h2>
<p>Key[button.add: {{ $t('button.add') }}</p>
<p>Key[params]: {{ $t('params', 'function value') }}</p>
Expand All @@ -13,6 +14,21 @@
<button @click="swtchLang('zhCHS')">Change Lang to zhCHS</button>
</p>
<button @click="swtchLang('zhCHT')">Change Lang to zhCHT</button>
<p>
<button @click="changLang('zhCHT')">
Change Lang to zhCHT by $i18n.changeLocale
</button>
</p>
<p>
<button @click="changLang('zhCHS')">
Change Lang to zhCHS by $i18n.changeLocale
</button>
</p>
<p>
<button @click="changLang('en')">
Change Lang to en by $i18n.changeLocale
</button>
</p>
</template>
<script lang="ts">
import { useI18n } from '../../../src/index'
Expand All @@ -39,6 +55,11 @@ export default {
params: (val: string) => `繁体 i18n选项 参数: ${val}`
}
},
methods: {
changLang(locale: string) {
;(this as any).$i18n.changeLocale(locale)
}
},
setup() {
const i18n = useI18n()
const { changeLocale, currentLocale } = i18n
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function createI18n(options?: I18nOptions): I18nInstance {
},
install(app: App) {
const ctx = this
app.config.globalProperties.$i18n = ctx
app.config.globalProperties.$t = ctx.t
$t = ctx.t
app.mixin({
Expand Down

0 comments on commit 2e60ad8

Please sign in to comment.