Skip to content

Commit

Permalink
Use Intl.Locale() to parse locales
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantellingen committed Oct 15, 2024
1 parent 65b6a85 commit 8f813fe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-cherries-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/toolkit": patch
---

Use Intl.Locale() to parse locales
7 changes: 5 additions & 2 deletions src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ export const getLocalizedValue = <T = string>(
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
Expand Down

0 comments on commit 8f813fe

Please sign in to comment.