Ovi*_*Ovi 6 internationalization vue.js vue-router vuejs2
我正在使用 TypeScript 和 VueJS 的脚手架 AspNetCore 2.1 站点。我正在尝试将kazupon i18n插件与路由器视图集成。没有 URL 集成,它工作得很好。
我无法像http://localhost/en/product和http://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'))
});
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试过添加前缀,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') },
]
}];
Run Code Online (Sandbox Code Playgroud)
我也试过依靠router.beforeEach来设置语言环境。
router.beforeEach((to, from, next) => {
let language = to.params.locale;
if (!language) {
language = 'en';
}
i18n.locale = language;
next();
});
Run Code Online (Sandbox Code Playgroud)
这也不起作用。
我从github.com/ashour/vuejs-i18n-demo和vue-i18n-sap-multilingual-best-practice 中汲取了灵感,但似乎在 i18n 迁移期间,一些示例可能已经过时或丢失,而那些使用-cases 不再起作用。
Jul*_*yag 10
您主要需要的是指定 base在 vue-router 构造函数中选项。
但首先,您需要从 url 中提取语言环境:
let locale = window.location.pathname.replace(/^\/([^\/]+).*/i,'$1');
Run Code Online (Sandbox Code Playgroud)
然后base在你的VueRouter构造函数中指定:
const router = new VueRouter({
...
base: (locale.trim().length && locale != "/") ? '/' + locale : undefined
...
});
Run Code Online (Sandbox Code Playgroud)
最后但并非最不重要的一点是,将语言环境传递给您的VueI18n构造函数:
const i18n = new VueI18n({
locale: (locale.trim().length && locale != "/") ? locale : defaultLocale,
fallbackLocale: 'en',
messages
})
Run Code Online (Sandbox Code Playgroud)
请参阅更新的示例:
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'))
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9056 次 |
| 最近记录: |