Skip to content

Commit

Permalink
docs: optimize i18n docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kagol committed Nov 4, 2024
1 parent 35ad00f commit cc0d740
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions examples/sites/demos/pc/webdoc/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,19 +31,21 @@ export default (i18n) =>
}
}
})

export const i18n = initI18n({ locale: 'zhCN' })
```

步骤三、修改 `src/main.js`,添加以下代码:

```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')
```
Expand All @@ -67,7 +69,7 @@ import locale from '@opentiny/vue-locale'

Vue.use(VueI18n)

export default (i18n) =>
const initI18n = (i18n) =>
locale.initI18n({
i18n,
VueI18n,
Expand All @@ -80,18 +82,20 @@ export default (i18n) =>
}
}
})

export const i18n = initI18n({ locale: 'zhCN' })
```

步骤三、修改 `src/main.js`,添加以下代码:

```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')
Expand Down Expand Up @@ -165,3 +169,13 @@ Vue2 中切换语言的写法如下:
}
</script>
```
### 在 JavaScript 中使用
有时我们需要在纯 JavaScript 文件中使用国际化词条,而不是在 Vue 组件中,这时我们可以直接导入 `i18n` 变量,调用 `i18n.global.t` 方法即可。
```js
import { i18n } from './i18n'
const test = i18n.global.t('test')
```

0 comments on commit cc0d740

Please sign in to comment.