Locale parsing with unmatched dash locale overrides global
- Dominant language
- JavaScript
- Stars
- 48.7k
- Forks
- 2.5k
- PR merge metrics
- No merged PRs in 30d
Description
When making an instance call to set a locale, if the locale is supplied including the country code, but dayjs only has the generic language code, the global instance locale ends up getting overwritten.
For example, dayjs has the locale `nb` but if the full language tag with the country code is supplied to an **instance** `locale()` update such as `nb-no`, the **global** instance locale will get changed.
```javascript
[
dayjs.locale(), //en
dayjs().locale('nb-no'), //nb
dayjs.locale() //nb !!global locale is changed!!
]
```
I suspect it's related to this fallback scenario in `parseLocale()` where the supplied locale is split and just the first part is recursively passed into `parseLocale()`, but it does not pass forward the additional parameters, so `isLocal` is not passed forward which ultimately causes the recursive locale parsing to improperly set the global instance locale.
https://github.com/iamkun/dayjs/blob/bdcc336613c9fa466b385574e88d0b43629475bc/src/index.js#L26-L29
Contributor guide
Assessment
This issue has not been assessed yet.