Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: optimize i18n docs #2483

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
```
Loading