ZeroCool
ZeroCool

Reputation: 629

Angular 2 routing with culture

I need to add culture before every route for application. I am still using RC4. How can I modify my current routes to achieve the desired result?

export const routes: RouterConfig = [
...ItemRoutes,
...LibraryRoutes,

{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent },
{ path: '**', redirectTo: 'dashboard' }

];

Now after authentication, my redirect link is

localhost/en

I have the application config with the current culture when App component is loaded, ultimately I would like to set the language there and have it as a prefix route.

With my current setup, I will get redirected to dashboard. How can I add the culture parameter as the first parameter to all the routes? Is there something I should know regarding the routing and culture/language settings? Whats the best way to set the language on the front end side?

Upvotes: 0

Views: 561

Answers (1)

Madhu Ranjan
Madhu Ranjan

Reputation: 17914

Below solution is not exactly adding a prefix to all routes, but it may help,

export const routes: RouterConfig = [
...ItemRoutes,
...LibraryRoutes,

{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent },
{ path: '**', redirectTo: 'dashboard' }
]

export const localeRoutes: RouterConfig = [
 { path: '', redirectTo: 'en' , pathMatch: 'full'},
 { path: 'en', children: routes },
 { path: 'fr', children: routes }
]

You have to make sure all your routes are configured from here only, If Routes are defined in the imported module, they will be treated separately. However this approach will work with Lazy loaded routes if configured here.

Hope this helps!!

Upvotes: 1

Related Questions