Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/great-avocados-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@lingo.dev/_spec": patch
"lingo.dev": patch
---

add helper method to spec
31 changes: 18 additions & 13 deletions packages/spec/src/locales.spec.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
16 changes: 4 additions & 12 deletions packages/spec/src/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "_";
Expand All @@ -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");
}