nuxt_site/middleware/i18n.global.js
2024-05-02 21:28:18 +08:00

14 lines
613 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export default defineNuxtRouteMiddleware((to, from) => {
// skip middleware on broswer
if (import.meta.browser) return
const broswerLocale = useBrowserLocale()
const cookieLocale = useCookie('i18n_redirected')
// 如果访问的是默认页面,如 http://localhost/about ,则跳转到 http://localhost/zh/about
// 如果访问的不是默认页面,如 http://localhost/en/about则不做处理
if (to.name.includes('___en') && !/^\/en\/.*$/.test(to.path)) {
if (broswerLocale === 'zh') {
return navigateTo(`/${broswerLocale}${to.path}`)
}
}
});