Skip to content

Commit

Permalink
Empty path issue fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cyber-dude authored Jun 6, 2019
1 parent 81f6edd commit 095f7d8
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions src/localize-router.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,28 @@ export class LocalizeRouterService {
)
.subscribe(this._routeChanged());
}

/**
* Change language and navigate to translated route
* @param lang
/**
* Extracts new segment value based on routeConfig and url
* @param snapshot
* @returns {string}
*/
changeLanguage(lang: string): void {
if (lang !== this.parser.currentLang) {
const rootSnapshot: ActivatedRouteSnapshot = this.router.routerState.snapshot.root;

this.parser.translateRoutes(lang)
.pipe(
// set new routes to router
tap(() => this.router.resetConfig(this.parser.routes))
)
.subscribe(() => {
const urlSegments = this.traverseSnapshot(rootSnapshot, true)
.filter((path: string, i: number) => {
return !i || path; // filter out empty paths
});

const navigationExtras: NavigationExtras = {
...rootSnapshot.queryParamMap.keys.length ? { queryParams: rootSnapshot.queryParams } : {},
...rootSnapshot.fragment ? { fragment: rootSnapshot.fragment } : {}
};

// use navigate to keep extras unchanged
this.router.navigate(urlSegments, navigationExtras);
});
private parseSegmentValue(snapshot: ActivatedRouteSnapshot): string {
if (snapshot.routeConfig) {
if (snapshot.routeConfig.path === '**') {
return this.parser.translateRoute(snapshot.url
.filter((segment: UrlSegment) => segment.path)
.map((segment: UrlSegment) => segment.path)
.join('/'));
} else if (snapshot.routeConfig.data && snapshot.routeConfig.data.localizeRouter) {
const subPathSegments = snapshot.routeConfig.data.localizeRouter.path.split('/');
return subPathSegments
.map((s: string, i: number) => s.indexOf(':') === 0 ?
snapshot.url[i].path :
this.parser.translateRoute(s))
.join('/');
}
}
return '';
}

/**
Expand Down

0 comments on commit 095f7d8

Please sign in to comment.