While `Intl.DateTimeFormat` should be the most reliable way to get the
user's locale, it lacks flexibility.
For example, on Windows PowerShell, there's no way to change the
detection result in-session (you have to logout and login again).
So in order to provide an escape hatch for developers and users who
want to temporarily change the locale, we prioritize environment
variables over `Intl.DateTimeFormat`.
In POSIX shells, you can do `LANG=zh_CN.UTF-8` to change the locale to
Chinese;
In CMD, it's `set LANG=zh_CN.UTF-8`;
In PowerShell, it's `$env:LANG = 'zh_CN.UTF-8'`.
Closes #369
function getLocale() {
const shellLocale =
- Intl.DateTimeFormat().resolvedOptions().locale || // Built-in ECMA-402 support
process.env.LC_ALL || // POSIX locale environment variables
process.env.LC_MESSAGES ||
process.env.LANG ||
- // TODO: Windows support if needed, could consider https://www.npmjs.com/package/os-locale
+ Intl.DateTimeFormat().resolvedOptions().locale || // Built-in ECMA-402 support
'en-US' // Default fallback
const locale = shellLocale.split('.')[0].replace('_', '-')