From 3873f76856b60fdd32ed42239eb8a2f06ce10502 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Wed, 21 Feb 2024 21:39:43 +0800 Subject: [PATCH 1/2] feat: use `Intl.getCanonicalLocales` check locale --- index.ts | 5 +++-- utils/getLanguage.ts | 7 +++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/index.ts b/index.ts index 16bf9e14..bf54070d 100755 --- a/index.ts +++ b/index.ts @@ -61,6 +61,9 @@ function emptyDir(dir) { } async function init() { + // The language information obtained may be incorrect. In this case, an error should be thrown and the banner information should not be printed. + const language = getLanguage() + console.log() console.log( process.stdout.isTTY && process.stdout.getColorDepth() > 8 @@ -124,8 +127,6 @@ async function init() { const forceOverwrite = argv.force - const language = getLanguage() - let result: { projectName?: string shouldOverwrite?: boolean diff --git a/utils/getLanguage.ts b/utils/getLanguage.ts index b0de77bd..bf18d9ac 100644 --- a/utils/getLanguage.ts +++ b/utils/getLanguage.ts @@ -51,8 +51,9 @@ interface Language { * @returns locale that linked with correct name */ function linkLocale(locale: string) { - let linkedLocale: string - switch (locale) { + // @ts-ignore + let linkedLocale: string = Intl.getCanonicalLocales(locale)[0] + switch (linkedLocale) { case 'zh-TW': case 'zh-HK': case 'zh-MO': @@ -62,8 +63,6 @@ function linkLocale(locale: string) { case 'zh-SG': linkedLocale = 'zh-Hans' break - default: - linkedLocale = locale } return linkedLocale From c843312b20ac93fcaa72c4815c7017bbf4d5ae30 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Wed, 21 Feb 2024 21:58:39 +0800 Subject: [PATCH 2/2] feat: update --- index.ts | 5 ++--- utils/getLanguage.ts | 11 +++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/index.ts b/index.ts index bf54070d..16bf9e14 100755 --- a/index.ts +++ b/index.ts @@ -61,9 +61,6 @@ function emptyDir(dir) { } async function init() { - // The language information obtained may be incorrect. In this case, an error should be thrown and the banner information should not be printed. - const language = getLanguage() - console.log() console.log( process.stdout.isTTY && process.stdout.getColorDepth() > 8 @@ -127,6 +124,8 @@ async function init() { const forceOverwrite = argv.force + const language = getLanguage() + let result: { projectName?: string shouldOverwrite?: boolean diff --git a/utils/getLanguage.ts b/utils/getLanguage.ts index bf18d9ac..6b853c3b 100644 --- a/utils/getLanguage.ts +++ b/utils/getLanguage.ts @@ -51,8 +51,13 @@ interface Language { * @returns locale that linked with correct name */ function linkLocale(locale: string) { - // @ts-ignore - let linkedLocale: string = Intl.getCanonicalLocales(locale)[0] + let linkedLocale: string + try { + // @ts-ignore + linkedLocale = Intl.getCanonicalLocales(locale)[0] + } catch (error) { + console.log(`${error.toString()}\n`) + } switch (linkedLocale) { case 'zh-TW': case 'zh-HK': @@ -63,6 +68,8 @@ function linkLocale(locale: string) { case 'zh-SG': linkedLocale = 'zh-Hans' break + default: + linkedLocale = locale } return linkedLocale