toonvanstrijp / nestjs-i18n

The i18n module for nestjs.
https://nestjs-i18n.com
Other
655 stars 113 forks source link

AcceptLanguageResolver incorrectly picks the first partially matched language #208

Closed KCFindstr closed 3 years ago

KCFindstr commented 3 years ago

I noticed that AcceptLanguageResolver uses accept-language-parser in loose mode, which means the order of the languages matter from the documentation.

In loose mode the order of supportedLanguagesArray matters, as it is the first partially matching language that is returned. It means that if you want to pick more specific langauges first, you should list it first as well.

This resulted in some strange behaviors. For example, if the provided languages are: ['zh-CN', 'zh-TW'] zh-CN (simplified Chinese) will always be picked even if the accept language is zh-TW or zh-Hant-TW (traditional Chinese). I also tried ['zh-Hans-CN', 'zh-Hant-TW'] but neither worked. Is there a workaround other than providing my own language resolver?

toonvanstrijp commented 3 years ago

Woow great that you mention it! Never would have found it without your help! I think we can change the behaviour of the default accept-language resolver so that it works with your scenario? Lemme know what you thinks :)

KCFindstr commented 3 years ago

I'm not familiar with accept-language-parser but it seems that the loose option was added in #179 to fix another bug? I don't know if any other accept-language parsers can handle this better, but a temporary fix I can think about is to pick without loose first, and if nothing matched, then pick in loose mode again?

KCFindstr commented 3 years ago

Are there any updates on this? If not, I'm going to write my own accept language resolver.

toonvanstrijp commented 3 years ago

@KCFindstr sorry I’ve had the time yet to look into this. (Currently on vacation). Feel free to make a pull request (as long as it doesn’t break any existing tests)