From cc0d7403e426498292aaa0cf900728b61d0e2da0 Mon Sep 17 00:00:00 2001 From: Kagol Date: Mon, 4 Nov 2024 19:53:12 +0800 Subject: [PATCH] docs: optimize i18n docs --- examples/sites/demos/pc/webdoc/i18n.md | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/examples/sites/demos/pc/webdoc/i18n.md b/examples/sites/demos/pc/webdoc/i18n.md index 2191a01f67..425379b7cb 100644 --- a/examples/sites/demos/pc/webdoc/i18n.md +++ b/examples/sites/demos/pc/webdoc/i18n.md @@ -18,7 +18,7 @@ npm install vue-i18n import { createI18n } from 'vue-i18n' import locale from '@opentiny/vue-locale' -export default (i18n) => +const initI18n = (i18n) => locale.initI18n({ i18n, createI18n, @@ -31,6 +31,8 @@ export default (i18n) => } } }) + +export const i18n = initI18n({ locale: 'zhCN' }) ``` 步骤三、修改 `src/main.js`,添加以下代码: @@ -38,12 +40,12 @@ export default (i18n) => ```js import { createApp } from 'vue' import App from './App.vue' -import initI18n from './i18n' +import { i18n } from './i18n' import router from './router' const app = createApp(App) -app.use(initI18n({ locale: 'zhCN' })) +app.use(i18n) app.use(router).mount('#app') ``` @@ -67,7 +69,7 @@ import locale from '@opentiny/vue-locale' Vue.use(VueI18n) -export default (i18n) => +const initI18n = (i18n) => locale.initI18n({ i18n, VueI18n, @@ -80,6 +82,8 @@ export default (i18n) => } } }) + +export const i18n = initI18n({ locale: 'zhCN' }) ``` 步骤三、修改 `src/main.js`,添加以下代码: @@ -87,11 +91,11 @@ export default (i18n) => ```js import Vue from 'vue' import App from './App.vue' -import initI18n from './i18n' +import { i18n } from './i18n' import router from './router' new Vue({ - i18n: initI18n({ locale: 'zhCN' }), + i18n, router, render: (h) => h(App) }).$mount('#app') @@ -165,3 +169,13 @@ Vue2 中切换语言的写法如下: } ``` + +### 在 JavaScript 中使用 + +有时我们需要在纯 JavaScript 文件中使用国际化词条,而不是在 Vue 组件中,这时我们可以直接导入 `i18n` 变量,调用 `i18n.global.t` 方法即可。 + +```js +import { i18n } from './i18n' + +const test = i18n.global.t('test') +```