From 10a0617601224cdaf406ed2c18d1277e67214de8 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Tue, 17 May 2022 15:25:15 -0500 Subject: [PATCH 1/4] fix(#3309): use system default locale --- .changeset/thirty-drinks-shout.md | 5 +++++ packages/astro/src/core/logger/core.ts | 22 ++++++++++------------ 2 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 .changeset/thirty-drinks-shout.md diff --git a/.changeset/thirty-drinks-shout.md b/.changeset/thirty-drinks-shout.md new file mode 100644 index 000000000000..d749298a3212 --- /dev/null +++ b/.changeset/thirty-drinks-shout.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix [#3309](https://github.com/withastro/astro/issues/3309) default logger locale behavior. diff --git a/packages/astro/src/core/logger/core.ts b/packages/astro/src/core/logger/core.ts index bfe5287f365a..46045fd81155 100644 --- a/packages/astro/src/core/logger/core.ts +++ b/packages/astro/src/core/logger/core.ts @@ -14,18 +14,16 @@ export interface LogOptions { level: LoggerLevel; } -function getLoggerLocale(): string { - const defaultLocale = 'en-US'; - if (process.env.LANG) { - const extractedLocale = process.env.LANG.split('.')[0].replace(/_/g, '-'); - // Check if language code is atleast two characters long (ie. en, es). - // NOTE: if "c" locale is encountered, the default locale will be returned. - if (extractedLocale.length < 2) return defaultLocale; - else return extractedLocale.substring(0, 5); - } else return defaultLocale; -} - -export const dateTimeFormat = new Intl.DateTimeFormat(getLoggerLocale(), { +// Hey, locales are pretty complicated! Be careful modifying this logic... +// If we throw at the top-level, international users can't use Astro. +// +// Using `[]` sets the default locale properly from the system! +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#parameters +// +// Here be the dragons we've slain: +// https://github.com/withastro/astro/issues/2625 +// https://github.com/withastro/astro/issues/3309 +export const dateTimeFormat = new Intl.DateTimeFormat([], { hour: '2-digit', minute: '2-digit', second: '2-digit', From 437d6e874edac33662ae98234f6c07b2decc578b Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Tue, 17 May 2022 15:35:09 -0500 Subject: [PATCH 2/4] fix(#3309): use system default locale in create-astro --- .changeset/thirty-drinks-shout.md | 1 + packages/create-astro/src/logger.ts | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.changeset/thirty-drinks-shout.md b/.changeset/thirty-drinks-shout.md index d749298a3212..815e14dc144d 100644 --- a/.changeset/thirty-drinks-shout.md +++ b/.changeset/thirty-drinks-shout.md @@ -1,5 +1,6 @@ --- 'astro': patch +'create-astro': patch --- Fix [#3309](https://github.com/withastro/astro/issues/3309) default logger locale behavior. diff --git a/packages/create-astro/src/logger.ts b/packages/create-astro/src/logger.ts index 65f354632c00..96965e7ea03a 100644 --- a/packages/create-astro/src/logger.ts +++ b/packages/create-astro/src/logger.ts @@ -6,20 +6,19 @@ type ConsoleStream = Writable & { fd: 1 | 2; }; -function getLoggerLocale(): string { - const defaultLocale = 'en-US'; - if (process.env.LANG) { - const extractedLocale = process.env.LANG.split('.')[0].replace(/_/g, '-'); - // Check if language code is atleast two characters long (ie. en, es). - // NOTE: if "c" locale is encountered, the default locale will be returned. - if (extractedLocale.length < 2) return defaultLocale; - else return extractedLocale; - } else return defaultLocale; -} - -const dt = new Intl.DateTimeFormat(getLoggerLocale(), { +// Hey, locales are pretty complicated! Be careful modifying this logic... +// If we throw at the top-level, international users can't use Astro. +// +// Using `[]` sets the default locale properly from the system! +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#parameters +// +// Here be the dragons we've slain: +// https://github.com/withastro/astro/issues/2625 +// https://github.com/withastro/astro/issues/3309 +const dt = new Intl.DateTimeFormat([], { hour: '2-digit', minute: '2-digit', + second: '2-digit', }); export const defaultLogDestination = new Writable({ From 25731303b2e42c6bd56a3c92ae07a5760d348249 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Tue, 17 May 2022 15:52:30 -0500 Subject: [PATCH 3/4] test: add locale regression tests --- packages/astro/test/cli.test.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/astro/test/cli.test.js b/packages/astro/test/cli.test.js index 369c1210330e..98e0ade0a6be 100644 --- a/packages/astro/test/cli.test.js +++ b/packages/astro/test/cli.test.js @@ -25,6 +25,18 @@ describe('astro cli', () => { expect(proc.stdout).to.include(pkgVersion); }); + const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); + 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); + const proc = cli('dev', '--root', fileURLToPath(projectRootURL), { extendEnv: false, env: { LANG: locale }}); + + await Promise.race([proc, sleep(5000).then(() => proc.kill(0))]); + expect(proc.exitCode).to.equal(0, ``); + }); + }) + it('astro build', async () => { const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url); const proc = await cli('build', '--root', fileURLToPath(projectRootURL)); From 4f307f79846f1a46c1136ee0beb78b036ca6a4d5 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Wed, 18 May 2022 10:28:26 -0500 Subject: [PATCH 4/4] test: add i18n regression test --- packages/astro/test/cli.test.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/packages/astro/test/cli.test.js b/packages/astro/test/cli.test.js index 98e0ade0a6be..480a530d0c37 100644 --- a/packages/astro/test/cli.test.js +++ b/packages/astro/test/cli.test.js @@ -25,18 +25,6 @@ describe('astro cli', () => { expect(proc.stdout).to.include(pkgVersion); }); - const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); - 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); - const proc = cli('dev', '--root', fileURLToPath(projectRootURL), { extendEnv: false, env: { LANG: locale }}); - - await Promise.race([proc, sleep(5000).then(() => proc.kill(0))]); - expect(proc.exitCode).to.equal(0, ``); - }); - }) - it('astro build', async () => { const projectRootURL = new URL('./fixtures/astro-basic/', import.meta.url); const proc = await cli('build', '--root', fileURLToPath(projectRootURL)); @@ -122,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; + }); + }) +})