gilsdav / ngx-translate-router

Translate routes using ngx-translate
131 stars 43 forks source link

redirectTo does not preserve query parameters and fragments #132

Closed mstieranka closed 2 years ago

mstieranka commented 2 years ago

Hi, we are having an issue with this library on the project I'm working on, and both seem to be connected to redirectTo. We use it along with localizeRedirectTo to redirect the user from, say, /login to /{lang}/login. However, in these scenarios, the query parameters and fragments are removed. According to https://github.com/angular/angular/issues/13315#issuecomment-427254639, the way to fix this is by removing the beginning slash from the redirectTo value. In this library, that would mean modifying localize-router.parser.ts in the following way:

addPrefixToUrl(url: string): string {
    const plitedUrl = url.split('?');
    plitedUrl[0] = plitedUrl[0].replace(/\/$/, '');
    return `${this.urlPrefix}${plitedUrl.join('?')}`; // <- removed the slash in the beginning of this string
}

Upon testing this locally by modifying the .mjs files, this seems to fix this specific issue, however I'm not 100% sure that it won't affect anything else.

Alternatively, a solution that would surely fix this without affecting else, but at the cost of essentially being a workaround, would be to remove the slash only when translating redirectTo in _translateRouteTree:

private _translateRouteTree(routes: Routes): void {
    routes.forEach((route: Route) => {
      const skipRouteLocalization = (route.data && route.data['skipRouteLocalization']);
      const localizeRedirection = !skipRouteLocalization || skipRouteLocalization['localizeRedirectTo'];

      if (route.redirectTo && localizeRedirection) {
        this._translateProperty(route, 'redirectTo', !route.redirectTo.indexOf('/'));
        route.redirectTo = route.redirectTo.slice(1);
      }
...

Would it be possible to look into this?

gilsdav commented 2 years ago

Fixed in version 5.1.0