简体   繁体   English

如何使用 i18n 角度翻译管理链接

[英]How to manage links with i18n angular translation

I am doing an angular app and I am now translating it using i18n .我正在做一个有角度的应用程序,我现在正在使用i18n翻译它。

My question is : How do we manage links over different locales ?我的问题是:我们如何管理不同地区的链接?

My first idea was to translate them in order to add and manage the locale code in the url, but I am afraid this wont work.我的第一个想法是翻译它们以便在 url 中添加和管理区域设置代码,但我担心这行不通。

For example :例如 :

<a href="/login">Login</a> would become <a href="/fr/login" i18n-href>Login</a> <a href="/login">Login</a>将变成<a href="/fr/login" i18n-href>Login</a>

And then that fr would be replaced in each locale file.然后该fr将在每个语言环境文件中被替换。

But I'm afraid this is not the good way, and I'm afraid it will end in something like mysite.com/fr/fr/login , what won't work.但恐怕这不是好方法,恐怕它会以mysite.com/fr/fr/login内容结尾,这是行不通的。

I have the same question for routerLinks but I think this should be the same thing.我对routerLinks有同样的问题,但我认为这应该是同一件事。

So after a few testing it appears that the behavior is different for routerLinks and href因此,经过几次测试后, routerLinkshref的行为似乎有所不同

The routerLink itself is managed by angular, so you don't need to add a prefix in the URL to manage your locale. routerLink本身是由 angular 管理的,所以你不需要在 URL 中添加前缀来管理你的语言环境。 routerLink=/my/page will lead to /fr/my/page if you are currently in a /fr page.如果您当前在/fr页面中,则routerLink=/my/page将指向/fr/my/page page。

For href , it's different.对于href ,它是不同的。 You have to manage the locales yourself and if you don't do, you will get 404 errors (as /my/page will not exist anymore once the localize package is enabled).您必须自己管理语言环境,如果不这样做,您将收到 404 错误(因为启用localize包后/my/page将不再存在)。

Therefore, I would not recommand to use href for intern links (it was already a pretty bad habbit in my opinion but it gets a real problem here).因此,我不建议将href用于实习链接(在我看来这已经是一个非常糟糕的习惯,但它在这里遇到了真正的问题)。

Have a nice day !祝你今天过得愉快 ! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM