Greentube / localize-router

An implementation of routes localisation for Angular
MIT License
192 stars 93 forks source link

Pipe and LocalizeRouterService.translateRoute incorrectly translate string route-parameters #120

Open chancezeus opened 6 years ago

chancezeus commented 6 years ago

When using string values (for example slugs) as route parameters, the translateRoute method on the service (and by extension the pipe) incorrectly try to translate this into something localized. In my opinion the service should never try to translate any route segment that is a parameter.

Since it (might) be difficult to detect if a segment is a parameter, I think a "prefix" that should never occur in a regular uri.

To test my theory I used a hack to override the current translateText (could not extend it since the method is private) to use '*' as a value prefix and return key.substr(1) if it is found. I also added ':' prefix detection since this is used to identify the segment as a parameter by the router and just return the key as is (this fixes #118).

chancezeus commented 6 years ago

Working example (inside parser):

  private translateText(key: string): string {
    if (key) {
      if (key[0] === ':') {
        return key;
      }

      if (key[0] === '*') {
        return key.substr(1);
      }
    }

    if (!this._translationObject) {
      return key;
    }
    let res = this.translate.getParsedResult(this._translationObject, this.prefix + key);
    return res || key;
  }
}
rafa-suagu commented 6 years ago

Same here, I think that this issue its a must because for example for me, the library its unusable.

ydg commented 6 years ago

I am also running into this issue, for some routes my :id param is a string and the lib attempts to translate it

ydg commented 6 years ago

I found a fix (ugly!) that I will use for now:

since the localize-router will not find a translation in the resource files for the :id path segment, it will use the missing translation handler to generate the translated string.

You can override the implementation of the missingTranslationHandler (check the README) to a custom implementation that fit your needs -> for me it was, if I detect that it is fact an ID that is being attempted to translate, return the key value after removing 'ROUTES'

meeroslav commented 5 years ago

Fixed by 718b8e61be37ac6196f02c4eea41123772b859a3

rafa-suagu commented 5 years ago

Fixed by 718b8e6

@meeroslav this issue it's about the route parameters and your commit doesn't solve this problem, solves this another one #119.

Please reopen this issue.

I still need this piece of code:

export class MyMissingTranslationHandler implements MissingTranslationHandler {

  private static isRouteParam(key: string): boolean {
    return /^ROUTES\.:(.*)$/.test(key);
  }

  private static isRoute(key: string): boolean {
    return /^ROUTES\./.test(key);
  }

  handle(params: MissingTranslationHandlerParams): string {
    if (!MyMissingTranslationHandler.isRouteParam(params.key) && !environment.production) {
      console.warn(`Missing translation for key: ${params.key}`);
    }

    return MyMissingTranslationHandler.isRoute(params.key) ? params.key.slice(params.key.indexOf('.') + 1) : `## ${params.key} ##`;
  }
}
meeroslav commented 5 years ago

@rafa-as, please provide a small example using https://stackblitz.com/fork/localize-router-v2-issue-template as I'm not sure how to reproduce this.

Thanks!

rafa-suagu commented 5 years ago

Hi @meeroslav , thanks for reopen!

I'll try today do the stackbliz