Skip to content

Commit 09776ef

Browse files
committed
updates
1 parent 5b7fd74 commit 09776ef

File tree

7 files changed

+428
-273
lines changed

7 files changed

+428
-273
lines changed

docs/advanced/lazy.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ Let´s assume we have a project directory similar to the one below:
2828
The `pages` folder is where our arbitrary Vue component files like the `About.vue`, router inits, i18n inits and other reside. The `locales` folder is where all of our localization files reside, and In `i18n.js`, the functions for i18n-related process are defined as follows:
2929

3030
```js
31+
import { nextTick } from 'vue'
3132
import { createI18n } from 'vue-i18n'
3233

34+
export const SUPPORT_LOCALES = ['en', 'ja']
35+
3336
export function setupI18n(options = { locale: 'en' }) {
3437
const i18n = createI18n(options)
3538
setI18nLanguage(i18n, options.locale)
@@ -53,13 +56,15 @@ export function setI18nLanguage(i18n, locale) {
5356
}
5457

5558
export async function loadLocaleMessages(i18n, locale) {
56-
// load locale messages
57-
if (!i18n.global.availableLocales.includes(locale)) {
58-
const messages = await import(
59-
/* webpackChunkName: "locale-[request]" */ `./locales/${locale}.json`
60-
)
61-
i18n.global.setLocaleMessage(locale, messages.default)
62-
}
59+
// load locale messages with dynami import
60+
const messages = await import(
61+
/* webpackChunkName: "locale-[request]" */ `./locales/${locale}.json`
62+
)
63+
64+
// set locale and locale message
65+
i18n.global.setLocaleMessage(locale, messages.default)
66+
67+
return nextTick()
6368
}
6469
```
6570

@@ -83,19 +88,21 @@ Here the code for the vue-router beforeEach hook part of `router.js`:
8388

8489
```js
8590
// navigation guards
86-
router.beforeEach((to, from, next) => {
87-
const locale = to.params.locale
91+
router.beforeEach(async (to, from, next) => {
92+
const paramsLocale = to.params.locale
8893

89-
// check locale
90-
if (!SUPPORT_LOCALES.includes(locale)) {
91-
return false
94+
// use locale if paramsLocale is not in SUPPORT_LOCALES
95+
if (!SUPPORT_LOCALES.includes(paramsLocale)) {
96+
return next(`/${locale}`)
9297
}
9398

9499
// load locale messages
95-
loadLocaleMessages(i18n, locale)
100+
if (!i18n.global.availableLocales.includes(paramsLocale)) {
101+
await loadLocaleMessages(i18n, paramsLocale)
102+
}
96103

97104
// set i18n language
98-
setI18nLanguage(i18n, locale)
105+
setI18nLanguage(i18n, paramsLocale)
99106

100107
return next()
101108
})

examples/lazy-loading/webpack/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"dependencies": {
1010
"vue": "^3.0.4",
11-
"vue-i18n": "^9.0.0-beta.16",
11+
"vue-i18n": "^9.0.0-beta.18",
1212
"vue-router": "^4.0.1"
1313
},
1414
"devDependencies": {

examples/lazy-loading/webpack/src/App.vue

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,60 @@
1111
</div>
1212
<form class="language">
1313
<label>{{ t('labels.language') }}</label>
14-
<select v-model="locale">
15-
<option value="en">en</option>
16-
<option value="ja">ja</option>
14+
<select v-model="currentLocale">
15+
<option
16+
v-for="optionLocale in supportLocales"
17+
:key="optionLocale"
18+
:value="optionLocale"
19+
>
20+
{{ optionLocale }}
21+
</option>
1722
</select>
1823
</form>
1924
</nav>
2025
<router-view />
2126
</template>
2227

2328
<script>
24-
import { ref, watch, defineComponent, getCurrentInstance } from 'vue'
25-
import { useRoute, useRouter } from 'vue-router'
29+
import { defineComponent, watch, ref } from 'vue'
30+
import { useRouter } from 'vue-router'
2631
import { useI18n } from 'vue-i18n'
32+
import { SUPPORT_LOCALES } from './i18n'
2733
2834
export default defineComponent({
2935
name: 'App',
3036
setup() {
3137
const router = useRouter()
32-
const route = useRoute()
33-
const { t, locale } = useI18n()
38+
const { t, locale } = useI18n({ useScope: 'global' })
3439
35-
// when change the locale, go to locale route
36-
watch(locale, val => {
40+
/**
41+
* select locale value for language select form
42+
*
43+
* If you use the vue-i18n composer `locale` property directly, it will be re-rendering component when this property is changed,
44+
* before dynamic import was used to asynchronously load and apply locale messages
45+
* To avoid this, use the anoter locale reactive value.
46+
*/
47+
const currentLocale = ref(locale.value)
48+
49+
// sync to switch locale from router locale path
50+
watch(router.currentRoute, route => {
51+
currentLocale.value = route.params.locale
52+
})
53+
54+
/**
55+
* when change the locale, go to locale route
56+
*
57+
* when the changes are detected, load the locale message and set the language via vue-router navigation guard.
58+
* change the vue-i18n locale too.
59+
*/
60+
watch(currentLocale, val => {
3761
router.push({
38-
name: router.currentRoute._rawValue.name,
62+
name: router.currentRoute.value.name,
3963
params: { locale: val }
4064
})
4165
})
4266
43-
return { t, locale }
67+
return { t, locale, currentLocale, supportLocales: SUPPORT_LOCALES }
4468
}
4569
})
4670
</script>

examples/lazy-loading/webpack/src/i18n.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { nextTick } from 'vue'
12
import { createI18n } from 'vue-i18n'
23

4+
export const SUPPORT_LOCALES = ['en', 'ja']
5+
36
export function setupI18n(options = { locale: 'en' }) {
47
const i18n = createI18n(options)
58
setI18nLanguage(i18n, options.locale)
@@ -23,14 +26,13 @@ export function setI18nLanguage(i18n, locale) {
2326
}
2427

2528
export async function loadLocaleMessages(i18n, locale) {
26-
// load locale messages
27-
if (!i18n.global.availableLocales.includes(locale)) {
28-
const messages = await import(
29-
/* webpackChunkName: "locale-[request]" */ `./locales/${locale}.json`
30-
)
29+
// load locale messages with dynami import
30+
const messages = await import(
31+
/* webpackChunkName: "locale-[request]" */ `./locales/${locale}.json`
32+
)
3133

32-
// set locale and locale message
33-
i18n.global.setLocaleMessage(locale, messages.default)
34-
setI18nLanguage(i18n, locale)
35-
}
34+
// set locale and locale message
35+
i18n.global.setLocaleMessage(locale, messages.default)
36+
37+
return nextTick()
3638
}

examples/lazy-loading/webpack/src/router.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { createRouter, createWebHistory } from 'vue-router'
2-
import { loadLocaleMessages } from './i18n'
2+
import { setI18nLanguage, loadLocaleMessages, SUPPORT_LOCALES } from './i18n'
33

44
import Home from './pages/Home.vue'
55
import About from './pages/About.vue'
66

77
export function setupRouter(i18n) {
8-
const SUPPORT_LOCALES = ['en', 'ja']
98
const locale =
109
i18n.mode === 'legacy' ? i18n.global.locale : i18n.global.locale.value
1110

@@ -34,16 +33,21 @@ export function setupRouter(i18n) {
3433
})
3534

3635
// navigation guards
37-
router.beforeEach((to, from, next) => {
36+
router.beforeEach(async (to, from, next) => {
3837
const paramsLocale = to.params.locale
3938

4039
// use locale if paramsLocale is not in SUPPORT_LOCALES
4140
if (!SUPPORT_LOCALES.includes(paramsLocale)) {
4241
return next(`/${locale}`)
4342
}
4443

45-
// load locale messages and set i18n language
46-
loadLocaleMessages(i18n, paramsLocale)
44+
// load locale messages
45+
if (!i18n.global.availableLocales.includes(paramsLocale)) {
46+
await loadLocaleMessages(i18n, paramsLocale)
47+
}
48+
49+
// set i18n language
50+
setI18nLanguage(i18n, paramsLocale)
4751

4852
return next()
4953
})

examples/lazy-loading/webpack/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ module.exports = (env = {}) => ({
7070
stats: 'minimal',
7171
contentBase: __dirname,
7272
overlay: true,
73-
historyApiFallback: true // 404s will fallback to /index.html
73+
historyApiFallback: true // 404s will fallback to /index.html
7474
}
7575
})

0 commit comments

Comments
 (0)