From 8f813fe325f40c3be9562718d92a3d6102b8b1e6 Mon Sep 17 00:00:00 2001 From: Michael van Tellingen Date: Tue, 15 Oct 2024 20:49:57 +0200 Subject: [PATCH] Use Intl.Locale() to parse locales --- .changeset/forty-cherries-drive.md | 5 +++++ src/i18n.ts | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 .changeset/forty-cherries-drive.md diff --git a/.changeset/forty-cherries-drive.md b/.changeset/forty-cherries-drive.md new file mode 100644 index 0000000..ea3b867 --- /dev/null +++ b/.changeset/forty-cherries-drive.md @@ -0,0 +1,5 @@ +--- +"@labdigital/toolkit": patch +--- + +Use Intl.Locale() to parse locales diff --git a/src/i18n.ts b/src/i18n.ts index 4b0b80e..f31065c 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -95,8 +95,11 @@ export const getLocalizedValue = ( export const parseLocale = ( locale: string, ): { languageTag: string; subTag: string | undefined } => { - const [languageTag, subTag] = locale.split("-"); - return { languageTag, subTag }; + const loc = new Intl.Locale(locale); + return { + languageTag: loc.language, + subTag: loc.region || loc.script || undefined, // Uses region or script as the subtag if available + }; }; // Locales where family name comes first