Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 - route with language

Tags:

angular

Hello i would like to create routes with language in this format:

www.domain.com/lang/sometimes

Example:

www.domain.com/en/sometimes
www.domain.com/de/sometimes

Is it possible write to route something like:

RouterModule.forChild({
   path: ':lang/sometimes', component: TestComponent
})

Is it possible? How to set to url default language? For example when app starting, set dynamically lang parameter to url.

Thank you for your advices

like image 993
bluray Avatar asked Jul 14 '17 06:07

bluray


People also ask

Which route Guard can be used to mediate navigation to a route?

The Angular router's navigation guards allow to grant or remove access to certain parts of the navigation. Another route guard, the CanDeactivate guard, even allows you to prevent a user from accidentally leaving a component with unsaved changes.

How do I enable Angular routing?

Add a default routelink To make the application navigate to the dashboard automatically, add the following route to the routes array. content_copy { path: '', redirectTo: '/dashboard', pathMatch: 'full' }, This route redirects a URL that fully matches the empty path to the route whose path is '/dashboard' .

Which service can be used to extract route parameters inside component?

Router service needs to be explicitly provided in angular module to use it in another component via DI. You can chain multiple pipe in a single expression along with “async” pipe.

How do I test a router navigate in Angular 8?

We can test routing in Angular by using RouterTestingModule instead of RouterModule to provide our routes. This uses a spy implementation of Location which doesn't trigger a request for a new URL but does let us know the target URL which we can use in our test specs.


1 Answers

You can do something like this then. You can create two routes, one for default route and another for other Routes.

 RouterModule.forChild([
  { path: 'english/users/sometimes', component: UserComponent, useAsDefault: true },
  { path: ':lang/users/sometimes', component: UserCOmponent }
])

Added: For subscribing to the param:

import { ActivatedRoute } from '@angular/router';

constructior(private route: ActivatedRoute)

ngOnInit(){
this.route.params.subscribe(value => {
    let lang = value['lang']);
    console.log(lang);    

});
}
like image 101
nirazlatu Avatar answered Oct 07 '22 23:10

nirazlatu