I have an angular application that was supporting only one locale (French). I then evolved the application to support another language where the same components are available under another root route /ar
My problem now is that the application uses a lot of [routerLink]="['some', 'thing'] and other router.navigate('...'). Instead of i18n all links, I'm thinking of a smart way to perform the url translation.
I have done the following :
this.translateService.onLangChange
.combineLatest(this.router.events)
.subscribe(([langEvent, event]) => {
if (event instanceof NavigationStart) {
let currentUrl = event.url;
let locale = Locale.getLocaleByShortcut(langEvent.lang);
if (locale) {
if (locale.isArabic()) {
if (!ContextUtils.isArabicUrl(currentUrl)) {
this.router.navigate(['/ar/' + currentUrl], {queryParams: this.route.snapshot.queryParams});
}
} else {
if (ContextUtils.isArabicUrl(currentUrl)) {
let url = ContextUtils.frenchifyUrl(currentUrl);
this.router.navigate([url], {queryParams: this.route.snapshot.queryParams});
}
}
}
}
if (event instanceof NavigationEnd) {
if (Config.IS_WEB()) {
$(document).ready(() =>
setTimeout(() => {
Materialize.updateTextFields()
}, 10)
);
}
this.scrollToTop();
}
});
The problem is that this solutions does not work perfectally especially with queryParams that are kept when navigating from an url having queryParams to another that does not have them.
Is there any way smart way to prefix the UrlSegments the router is navigating to ?
Example
<a[routerLink]="['some', 'thing']></a> ----> redirect to : /ar/some/thing when AR locale is set. And to /some/thing when not.
Thank you.
/some/thingcan not relatively change to/ar/some/thing. If you have/en/some/thingyou can relatively change to/ar/some/thing. The only other option is to make the language a query parameter so that the routing itself doesn't change. - Reactgular