From e72cc8b9885643ea5279c58b244e065298587c68 Mon Sep 17 00:00:00 2001 From: kazuya kawaguchi Date: Thu, 12 Oct 2023 14:45:33 +0900 Subject: [PATCH] fix: refactor `validateLangTag` (#23) --- src/shared.ts | 3 +-- src/shim.d.ts | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 src/shim.d.ts diff --git a/src/shared.ts b/src/shared.ts index 70bf362..3f0837a 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -42,8 +42,7 @@ export function toLocale(val: string | Intl.Locale): Intl.Locale { */ export function validateLangTag(lang: string): boolean { try { - // TODO: if we have a better way to validate the language tag, we should use it. - new Intl.Locale(lang) + Intl.getCanonicalLocales(lang) return true } catch { return false diff --git a/src/shim.d.ts b/src/shim.d.ts new file mode 100644 index 0000000..4f541fa --- /dev/null +++ b/src/shim.d.ts @@ -0,0 +1,3 @@ +declare namespace Intl { + function getCanonicalLocales(locales: string | string[]): string[] +}