fix: locale handling for logger (#3391)

* fix(#3309): use system default locale

* fix(#3309): use system default locale in create-astro

* test: add locale regression tests

* test: add i18n regression test
This commit is contained in:
Nate Moore 2022-05-18 10:45:09 -05:00 committed by GitHub
parent 6cec1b8ef2
commit cf8015eaa2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 24 deletions

View file

@ -0,0 +1,6 @@
---
'astro': patch
'create-astro': patch
---
Fix [#3309](https://github.com/withastro/astro/issues/3309) default logger locale behavior.

View file

@ -14,18 +14,16 @@ export interface LogOptions {
level: LoggerLevel; level: LoggerLevel;
} }
function getLoggerLocale(): string { // Hey, locales are pretty complicated! Be careful modifying this logic...
const defaultLocale = 'en-US'; // If we throw at the top-level, international users can't use Astro.
if (process.env.LANG) { //
const extractedLocale = process.env.LANG.split('.')[0].replace(/_/g, '-'); // Using `[]` sets the default locale properly from the system!
// Check if language code is atleast two characters long (ie. en, es). // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#parameters
// NOTE: if "c" locale is encountered, the default locale will be returned. //
if (extractedLocale.length < 2) return defaultLocale; // Here be the dragons we've slain:
else return extractedLocale.substring(0, 5); // https://github.com/withastro/astro/issues/2625
} else return defaultLocale; // https://github.com/withastro/astro/issues/3309
} export const dateTimeFormat = new Intl.DateTimeFormat([], {
export const dateTimeFormat = new Intl.DateTimeFormat(getLoggerLocale(), {
hour: '2-digit', hour: '2-digit',
minute: '2-digit', minute: '2-digit',
second: '2-digit', second: '2-digit',

View file

@ -110,3 +110,21 @@ describe('astro cli', () => {
}); });
}); });
}); });
describe('astro cli i18n', () => {
const LOCALES = ['en_US', 'sv_SE', 'es_419.UTF-8', 'es_ES@euro', 'C'];
LOCALES.forEach((locale) => {
it(`astro does NOT throw on "${locale}" locales`, async () => {
const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url);
let error = null;
try {
const proc = cli('dev', '--root', fileURLToPath(projectRootURL), { env: { LANG: locale }});
await parseCliDevStart(proc)
} catch (e) {
console.log(e);
error = e.message;
}
expect(error).to.be.null;
});
})
})

View file

@ -6,20 +6,19 @@ type ConsoleStream = Writable & {
fd: 1 | 2; fd: 1 | 2;
}; };
function getLoggerLocale(): string { // Hey, locales are pretty complicated! Be careful modifying this logic...
const defaultLocale = 'en-US'; // If we throw at the top-level, international users can't use Astro.
if (process.env.LANG) { //
const extractedLocale = process.env.LANG.split('.')[0].replace(/_/g, '-'); // Using `[]` sets the default locale properly from the system!
// Check if language code is atleast two characters long (ie. en, es). // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#parameters
// NOTE: if "c" locale is encountered, the default locale will be returned. //
if (extractedLocale.length < 2) return defaultLocale; // Here be the dragons we've slain:
else return extractedLocale; // https://github.com/withastro/astro/issues/2625
} else return defaultLocale; // https://github.com/withastro/astro/issues/3309
} const dt = new Intl.DateTimeFormat([], {
const dt = new Intl.DateTimeFormat(getLoggerLocale(), {
hour: '2-digit', hour: '2-digit',
minute: '2-digit', minute: '2-digit',
second: '2-digit',
}); });
export const defaultLogDestination = new Writable({ export const defaultLogDestination = new Writable({