I’m doing an i18n Angular app, so far it works great.
However I have same route strings for different languages, which isn’t best for SEO.
Is it possible to have ‘path’ properties of Routes array be different for each language?
Ex:
const appRoutesEN: Routes = [
{ path: 'crisis-center', component: CrisisListComponent },
{ path: 'hero/:id', component: HeroDetailComponent },
{
path: 'heroes',
component: HeroListComponent,
data: { title: 'Heroes List' }
},
{ path: '**', component: PageNotFoundComponent }
];
Is it possible to define also a appRoutesFR: Routes
if yes how I can use it? Shall I inject LOCALE_ID
to routing module? If so how?
3
Answers
For the moment there seems to be no easy solution for this. I'll update if I find one.
Angular i18n people are working to integrate code level internationalization, maybe then.
Best solution I can come up with is to change routing module(s) in your code and
routerLink
attributes in templates and all links in other parts of your code for each language, then build your app for each language separately.Not ideal at all.
UPDATE
As recommended by @estus, I've tried out Greentube/localize-router.
I wasn't happy to install dependencies like
@ngx-translate/core
and@ngx-translate/http-loader
since I'm using Angular i18n implementation/tools and not ngx-translate.It works till it hits a lazy-loaded module with child routes.
So if you don't have any lazy loaded module with children,
localize-router
is the way to go.UPDATE
Lazy loaded modules are now supported in the latest version of localize-router.
Angular i18n will be supported once the programatic translations arrive.
If you need different routing per language as you describe in your comment you could have a dedicated routing module for each individual language. Then define in the angular-cli.json for each language a dedicated app with it’s own main.ts and it’s own AppModule, pulling in only the routing module required for the particular language.
You then build for each language the app like this:
This way you set it up once and can build each time without commenting out anything.
I do not have the full context. You say having routes per language is better for SEO. I do not understand this, but if you say so, OK. However I would not like dedicated routing for each language. This means a lot of redundancy and more maintenance.
If you are already using something like @ngx-translate/core then you can create your own mapping instead of adding more external libraries.
Example:
And then use it like: