Vue i18n-使用routerview将语言环境添加到URL

时间:2018-06-27 14:43:49

标签: vue.js internationalization vuejs2 vue-router

我正在使用TypeScript将一个脚手架的AspNetCore 2.1站点与VueJS一起使用。 我正在尝试将kazupon i18n插件与路由器视图集成。没有URL集成,它就可以正常工作。

我无法像http://localhost/en/producthttp://localhost/fr/product那样获得适当的重定向

这是初始的boot.ts,使用的是VueI18n

import Vue from 'vue';
import VueRouter from 'vue-router';
import VueI18n from 'vue-i18n'

Vue.use(VueRouter);
Vue.use(VueI18n);

import { messages, defaultLocale } from './lang/i18n';

const i18n = new VueI18n({
    locale: defaultLocale,
    fallbackLocale: 'en',
    messages
})

const routes = [
        { path: '/', component: require('./components/home/home.vue.html') },
        { path: '/product', component: require('./components/product/product.vue.html') },
];

const router = new VueRouter({
    mode: 'history',
    routes: routes
});

new Vue({
    el: '#app-root',
    router: router,
    i18n: i18n,
    render: h => h(require('./components/app/app.vue.html'))
});

到目前为止,我已经尝试为routes加上前缀,但这只是破坏了功能:

const routes = [{
    path: '/',
    redirect: `/${defaultLocale}`,
},
{
    path: '/:locale',
    children: [
        { path: '/', component: require('./components/home/home.vue.html') },
        { path: '/counter', component: require('./components/product/product.vue.html') },
    ]
}];

我还尝试过依靠router.beforeEach来设置语言环境。

router.beforeEach((to, from, next) => {
    let language = to.params.locale;
    if (!language) {
        language = 'en';
    }

    i18n.locale = language;
    next();
});

这也不起作用。

我从github.com/ashour/vuejs-i18n-demovue-i18n-sap-multilingual-best-practice中汲取了灵感,但似乎在i18n迁移期间,某些示例可能已过时或丢失,并且这些用例不再起作用。

2 个答案:

答案 0 :(得分:2)

您最需要的是在vue-router构造函数中指定base选项。

但是首先,您需要从url中提取语言环境:

let locale = window.location.pathname.replace(/^\/([^\/]+).*/i,'$1');

,然后在base构造函数中指定VueRouter

const router = new VueRouter({
    ...
    base: (locale.trim().length && locale != "/") ? '/' + locale : undefined
    ...
});

最后但并非最不重要的一点是,将语言环境传递到您的VueI18n构造函数中:

const i18n = new VueI18n({
    locale: (locale.trim().length && locale != "/") ? locale : defaultLocale,
    fallbackLocale: 'en',
    messages
})

查看更新后的示例:

import Vue from 'vue';
import VueRouter from 'vue-router';
import VueI18n from 'vue-i18n'

Vue.use(VueRouter);
Vue.use(VueI18n);

import { messages, defaultLocale } from './lang/i18n';

var locale = window.location.pathname.replace(/^\/([^\/]+).*/i,'$1');


const i18n = new VueI18n({
    locale: (locale.trim().length && locale != "/") ? locale : defaultLocale ,
    fallbackLocale: 'en',
    messages
})

const routes = [
        { path: '/', component: require('./components/home/home.vue.html') },
        { path: '/product', component: require('./components/product/product.vue.html') },
];

const router = new VueRouter({
    base: (locale.trim().length && locale != "/") ? '/' + locale : undefined,
    mode: 'history',
    routes: routes
});

new Vue({
    el: '#app-root',
    router: router,
    i18n: i18n,
    render: h => h(require('./components/app/app.vue.html'))
});

答案 1 :(得分:1)

非常感谢@Julian Paolo Dayag,我的问题中的方法存在三个问题:

  1. 它需要重定向到"/" + defaultLocale + to.path
  2. 子级路由不应以/开头,而应以product而不是/product
  3. 不再需要router.beforeEach...

我最终得到了这段代码:

const routes = [
  {
    path: "/",
    redirect: `/${defaultLocale}`
  },
  {
    path: `/(fr|en)`,
    component: require("./components/app/routertemplate.vue.html"),
    children: [
      { path: "", component: require("./components/home/home.vue.html") },
      {
        path: "product",
        component: require("./components/product/product.vue.html")
      }
    ]
  },
  {
    path: "/(.*)",
    redirect: (to: any) => {
      return "/" + defaultLocale + to.path;
    }
  }
];