robisim74 / angular-l10n

Angular library to translate texts, dates and numbers
MIT License
380 stars 59 forks source link

Language detection fix for root url. #316

Closed s02 closed 3 years ago

s02 commented 3 years ago

Currently query params for root url are not working using L10nRoutingModule. Example: https://angular-ivy-anhgcn.stackblitz.io/

Opening https://angular-ivy-anhgcn.stackblitz.io/en-US/about?aaa=111 is working fine Opening https://angular-ivy-anhgcn.stackblitz.io/en-US?aaa=111 or https://angular-ivy-anhgcn.stackblitz.io/en-US/?aaa=111 causes error: Error: Cannot match any routes. URL Segment: 'en-US' I've changed regexp to allow question mark.

robisim74 commented 3 years ago

@s02 Thanks!

exeleon commented 3 years ago

@robisim74 I added this fix in my codebase (I am using "en" and "es" locales), but I had to modify the proposed PR by @s02 as follows:

// Double backslash (\\) to properly escape the characters, especially the last (? character),
// because (?) without escape means 0 or 1 of the preceding token.
const regex = new RegExp(`(\\/${language}\\/)|(\\/${language}$)|(\\/${language}\\?)`);

if (segments != null) {
  // Remove (?) character if present, because future logic in the chain not deals with it
   return segments[0].replace('?', '');
}
robisim74 commented 3 years ago

Hi @exeleon,

there is a new update made by @s02 to this proposal, using the positive lookahead: https://github.com/robisim74/angular-l10n/pull/317

Give it a try and let me know.

However I think we should also add fragment support when root url.