diff --git a/.changeset/great-avocados-turn.md b/.changeset/great-avocados-turn.md new file mode 100644 index 000000000..781775351 --- /dev/null +++ b/.changeset/great-avocados-turn.md @@ -0,0 +1,6 @@ +--- +"@lingo.dev/_spec": patch +"lingo.dev": patch +--- + +add helper method to spec diff --git a/packages/spec/src/locales.spec.ts b/packages/spec/src/locales.spec.ts index c2cfa3994..0291979c7 100644 --- a/packages/spec/src/locales.spec.ts +++ b/packages/spec/src/locales.spec.ts @@ -1,21 +1,26 @@ import { describe, it, expect } from "vitest"; -import { getAlternativeLocaleCodes } from "./locales"; +import { normalizeLocale } from "./locales"; -describe("getAlternativeLocaleCodes", () => { - it("should convert dash to underscore format", () => { - expect(getAlternativeLocaleCodes("en-US")).toEqual(["en_US"]); - expect(getAlternativeLocaleCodes("fr-FR")).toEqual(["fr_FR"]); - expect(getAlternativeLocaleCodes("zh-Hans-CN")).toEqual(["zh_Hans_CN"]); +describe("normalizeLocale", () => { + it("should return normalized locale for short locale codes", () => { + expect(normalizeLocale("en")).toEqual("en"); + expect(normalizeLocale("fr")).toEqual("fr"); }); - it("should convert underscore to dash format", () => { - expect(getAlternativeLocaleCodes("en_US")).toEqual(["en-US"]); - expect(getAlternativeLocaleCodes("fr_FR")).toEqual(["fr-FR"]); - expect(getAlternativeLocaleCodes("zh_Hans_CN")).toEqual(["zh-Hans-CN"]); + it("should return normalized locale for full locale codes", () => { + expect(normalizeLocale("en-US")).toEqual("en-US"); + expect(normalizeLocale("fr-FR")).toEqual("fr-FR"); }); - it("should return empty array for simple locale codes", () => { - expect(getAlternativeLocaleCodes("en")).toEqual([]); - expect(getAlternativeLocaleCodes("fr")).toEqual([]); + it("should return normalized locale for full underscore locale codes", () => { + expect(normalizeLocale("en_US")).toEqual("en-US"); + expect(normalizeLocale("fr_FR")).toEqual("fr-FR"); + expect(normalizeLocale("zh_Hans_CN")).toEqual("zh-Hans-CN"); + }); + + it("should return normalized locale for full explicit region locale codes", () => { + expect(normalizeLocale("en-rUS")).toEqual("en-US"); + expect(normalizeLocale("fr-rFR")).toEqual("fr-FR"); + expect(normalizeLocale("zh-rCN")).toEqual("zh-CN"); }); }); diff --git a/packages/spec/src/locales.ts b/packages/spec/src/locales.ts index a0aa98342..d72160fbc 100644 --- a/packages/spec/src/locales.ts +++ b/packages/spec/src/locales.ts @@ -237,18 +237,6 @@ export const resolveLocaleCode = (value: LocaleCode): LocaleCodeFull => { throw new Error(`Invalid locale code: ${value}`); }; -export const getAlternativeLocaleCodes = (locale: string): string[] => { - if (locale.includes("-")) { - // Convert all dashes to underscores - return [locale.replace(/-/g, "_")]; - } else if (locale.includes("_")) { - // Convert all underscores to dashes - return [locale.replace(/_/g, "-")]; - } else { - return []; - } -}; - export const getLocaleCodeDelimiter = (locale: string): string | null => { if (locale.includes("_")) { return "_"; @@ -271,3 +259,7 @@ export const resolveOverridenLocale = (locale: string, delimiter?: "-" | "_" | n return locale.replace(currentDelimiter, delimiter); }; + +export function normalizeLocale(locale: string) { + return locale.replaceAll("_", "-").replace(/([a-z]{2,3}-)r/, "$1"); +}