vue3 i18n 未定义语言环境

发布于 2025-01-17 22:45:28 字数 1909 浏览 0 评论 0原文

我正在尝试做一个多语言项目,但是问题是 重定向返回未定义的。有什么方法可以解决此问题吗? 尽管我做了所有的努力,但它仍然说不确定,当我在地址栏中键入lang参数时,它行不通。固定en.json工作

https://vue-i18n.intlify。 dev/installation.html

app.js

import {createApp} from 'vue';
import App from './App.vue'
import i18n from './i18n';
import WebsiteRouter from './Website/router';
axios.defaults.withCredentials = true;

createApp(App)
    .use(WebsiteRouter)
    .use(i18n)
    .mount("#app");

/router/index.js

import {createRouter, createWebHistory} from 'vue-router'
import i18n from "../../i18n";
import routes from './routes'


const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {

    let language = to.params.lang;
    if (!language) {
        language = 'en'
    }
    i18n.locale = language
    next()
})

export default router;


doutes.js

import i18n from "../../i18n";

export default [
    {
        path: '/',
        redirect: `/${i18n.locale}`
    },
    {
        path: '/:lang',
        component: {
            render (c) { return c('router-view') }
        },
        children: [
            {
                path: '/',
                name: 'home',
                component: () => import('../Views/Home.vue'),
            },
            {
                path: '/about',
                name: 'about',
                component: () => import('../Views/About.vue'),
            },
            {
                path: '/contact',
                name: 'contact',
                component: () => import('../Views/Contact.vue'),
            }
        ]
    }
];

i am trying to do a multilingual project, but the problem is
redirect returns undefined. is there any way to fix this?
Despite all my efforts, it still says undefined and it doesn't work when I type the lang parameter in the address bar. fixed en.json working

https://vue-i18n.intlify.dev/installation.html

app.js

import {createApp} from 'vue';
import App from './App.vue'
import i18n from './i18n';
import WebsiteRouter from './Website/router';
axios.defaults.withCredentials = true;

createApp(App)
    .use(WebsiteRouter)
    .use(i18n)
    .mount("#app");

/router/index.js

import {createRouter, createWebHistory} from 'vue-router'
import i18n from "../../i18n";
import routes from './routes'


const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {

    let language = to.params.lang;
    if (!language) {
        language = 'en'
    }
    i18n.locale = language
    next()
})

export default router;


routes.js

import i18n from "../../i18n";

export default [
    {
        path: '/',
        redirect: `/${i18n.locale}`
    },
    {
        path: '/:lang',
        component: {
            render (c) { return c('router-view') }
        },
        children: [
            {
                path: '/',
                name: 'home',
                component: () => import('../Views/Home.vue'),
            },
            {
                path: '/about',
                name: 'about',
                component: () => import('../Views/About.vue'),
            },
            {
                path: '/contact',
                name: 'contact',
                component: () => import('../Views/Contact.vue'),
            }
        ]
    }
];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北凤男飞 2025-01-24 22:45:28

尝试用于

redirect: `/${i18n.globale.locale}` 

重定向

i18n.locale = language

i18n.globale.locale = language

ROUTES.JS

Try to use

redirect: `/${i18n.globale.locale}` 

for the redirect in routes.js

You may also try to replace:

i18n.locale = language

to:

i18n.globale.locale = language

in you router.beforeEach

Cheers :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文