-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
router.ts
59 lines (49 loc) · 1.33 KB
/
router.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { createRouter, createWebHistory } from 'vue-router'
import { setI18nLanguage } from './i18n'
import type { Router, RouteRecordRaw } from 'vue-router'
import type { I18n, Locale } from 'vue-i18n'
import Home from './pages/Home.vue'
import About from './pages/About.vue'
export function setupRouter(i18n: I18n): Router {
const SUPPORT_LOCALES = ['en', 'ja']
const { global: composer } = i18n
// setup routes
const routes: RouteRecordRaw[] = [
{
path: '/:locale/',
name: 'home',
component: Home
},
{
path: '/:locale/about',
name: 'about',
component: About
},
{
path: '/:pathMatch(.*)*',
redirect: () => `/${composer.locale.value}`
}
]
// create router instance
const router = createRouter({
history: createWebHistory(),
routes
})
// navigation guards
router.beforeEach(async (to, from, next) => {
const locale = (to.params as any).locale as Locale
// check locale
if (!SUPPORT_LOCALES.includes(locale)) {
return false
}
// load locale messages
if (!composer.availableLocales.includes(locale)) {
const messages = await import(`./locales/${locale}`)
composer.setLocaleMessage(locale, messages.default)
}
// set i18n language
setI18nLanguage(i18n, locale)
return next()
})
return router
}