Skip to content

Commit 9089b08

Browse files
authored
chore(spec): add helper method (#546)
1 parent dc2a844 commit 9089b08

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

.changeset/great-avocados-turn.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@lingo.dev/_spec": patch
3+
"lingo.dev": patch
4+
---
5+
6+
add helper method to spec

packages/spec/src/locales.spec.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import { describe, it, expect } from "vitest";
2-
import { getAlternativeLocaleCodes } from "./locales";
2+
import { normalizeLocale } from "./locales";
33

4-
describe("getAlternativeLocaleCodes", () => {
5-
it("should convert dash to underscore format", () => {
6-
expect(getAlternativeLocaleCodes("en-US")).toEqual(["en_US"]);
7-
expect(getAlternativeLocaleCodes("fr-FR")).toEqual(["fr_FR"]);
8-
expect(getAlternativeLocaleCodes("zh-Hans-CN")).toEqual(["zh_Hans_CN"]);
4+
describe("normalizeLocale", () => {
5+
it("should return normalized locale for short locale codes", () => {
6+
expect(normalizeLocale("en")).toEqual("en");
7+
expect(normalizeLocale("fr")).toEqual("fr");
98
});
109

11-
it("should convert underscore to dash format", () => {
12-
expect(getAlternativeLocaleCodes("en_US")).toEqual(["en-US"]);
13-
expect(getAlternativeLocaleCodes("fr_FR")).toEqual(["fr-FR"]);
14-
expect(getAlternativeLocaleCodes("zh_Hans_CN")).toEqual(["zh-Hans-CN"]);
10+
it("should return normalized locale for full locale codes", () => {
11+
expect(normalizeLocale("en-US")).toEqual("en-US");
12+
expect(normalizeLocale("fr-FR")).toEqual("fr-FR");
1513
});
1614

17-
it("should return empty array for simple locale codes", () => {
18-
expect(getAlternativeLocaleCodes("en")).toEqual([]);
19-
expect(getAlternativeLocaleCodes("fr")).toEqual([]);
15+
it("should return normalized locale for full underscore locale codes", () => {
16+
expect(normalizeLocale("en_US")).toEqual("en-US");
17+
expect(normalizeLocale("fr_FR")).toEqual("fr-FR");
18+
expect(normalizeLocale("zh_Hans_CN")).toEqual("zh-Hans-CN");
19+
});
20+
21+
it("should return normalized locale for full explicit region locale codes", () => {
22+
expect(normalizeLocale("en-rUS")).toEqual("en-US");
23+
expect(normalizeLocale("fr-rFR")).toEqual("fr-FR");
24+
expect(normalizeLocale("zh-rCN")).toEqual("zh-CN");
2025
});
2126
});

packages/spec/src/locales.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,6 @@ export const resolveLocaleCode = (value: LocaleCode): LocaleCodeFull => {
237237
throw new Error(`Invalid locale code: ${value}`);
238238
};
239239

240-
export const getAlternativeLocaleCodes = (locale: string): string[] => {
241-
if (locale.includes("-")) {
242-
// Convert all dashes to underscores
243-
return [locale.replace(/-/g, "_")];
244-
} else if (locale.includes("_")) {
245-
// Convert all underscores to dashes
246-
return [locale.replace(/_/g, "-")];
247-
} else {
248-
return [];
249-
}
250-
};
251-
252240
export const getLocaleCodeDelimiter = (locale: string): string | null => {
253241
if (locale.includes("_")) {
254242
return "_";
@@ -271,3 +259,7 @@ export const resolveOverridenLocale = (locale: string, delimiter?: "-" | "_" | n
271259

272260
return locale.replace(currentDelimiter, delimiter);
273261
};
262+
263+
export function normalizeLocale(locale: string) {
264+
return locale.replaceAll("_", "-").replace(/([a-z]{2,3}-)r/, "$1");
265+
}

0 commit comments

Comments
 (0)