Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: locale handling for logger #3391

Merged
merged 4 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/thirty-drinks-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'create-astro': patch
---

Fix [#3309](https://github.com/withastro/astro/issues/3309) default logger locale behavior.
22 changes: 10 additions & 12 deletions packages/astro/src/core/logger/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
18 changes: 18 additions & 0 deletions packages/astro/test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
})
})
23 changes: 11 additions & 12 deletions packages/create-astro/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down