Angular 2路线翻译 [英] Angular 2 route translation

查看:74
本文介绍了Angular 2路线翻译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个支持多种语言的网络应用。我正在使用@ ngx-translate成功翻译应用程序的内容。然而;我还需要翻译路线链接。我的链接看起来像这样



www.somewebsite.com/dashboard



现在我将语言改为土耳其语;我需要链接看起来像



www.somewebsite.com/anasayfa



我怎样才能轻松实现这一目标?任何帮助表示赞赏。提前致谢。



我的尝试:



尝试使用

 ngx-i18n-router 

 localize-router 

,但它没有帮助。

解决方案

这是一个糟糕的想法,因为你需要为每种语言构建几个路由。通常我会建议您使用语言配置路由。例如,



www.somewebsite.com/en/dashboard



如果它是一种不同的语言那应该是,



www.somewebsite.com/es/dashboard。





但是如果你真的想用不同的语言路线构建那么你必须这样做,



创建一个新的路径:{路径:':lang / dashboard',component:dashboardComponent} 

this.route.params.subscribe(params => {
translate.use(params ['lang']);
switch(params ['lang']){
case'en':
location.replace(`index.html#/


{params ['lang']} / dashboard`)
break;
case'fr':
location.replace(`index.html#/


{params ['lang']} / anasayfa`);
break;
}

})


I have a web app that support multiple languages. I am successfully translating the content of the app with @ngx-translate. However; I also need to translate the route link. My link look like this

www.somewebsite.com/dashboard

Now when I change the language to something like Turkish; I need the link look like

www.somewebsite.com/anasayfa

How can I achieve that easily? Any help is appreciated. Thanks in advance.

What I have tried:

Tried using

ngx-i18n-router

and

localize-router

, but it did not help.

解决方案

This is sort of bad idea to implement, since you need to build several routes for each language. Generally i would recommend you to configure routes with languages. For example,

www.somewebsite.com/en/dashboard

and if its a differencet language then it should be,

www.somewebsite.com/es/dashboard.


However if you really want to build with different language routes then you have to do,

Create a new path : { path: ':lang/dashboard', component: dashboardComponent }

this.route.params.subscribe(params => {
            translate.use(params['lang']);
            switch (params['lang']) {
                case 'en':
                    location.replace(`index.html#/


{params['lang']}/dashboard`) break; case 'fr': location.replace(`index.html#/


{params['lang']}/anasayfa`); break; } })


这篇关于Angular 2路线翻译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆