Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
61 changes: 60 additions & 1 deletion packages/spec/src/locales.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from "vitest";
import { normalizeLocale } from "./locales";
import { getLocaleCodeDelimiter, normalizeLocale, resolveLocaleCode, resolveOverridenLocale } from "./locales";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@khalatevarun the resolveOverridenLocale function was renamed in #576 to resolveOverriddenLocale. Once this is resolved we can merge it 🙌

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved, renamed the function


describe("normalizeLocale", () => {
it("should return normalized locale for short locale codes", () => {
Expand All @@ -24,3 +24,62 @@ describe("normalizeLocale", () => {
expect(normalizeLocale("zh-rCN")).toEqual("zh-CN");
});
});

describe("resolveLocaleCode", () => {
it("should resolve a short locale code to the first full locale code in the map", () => {
expect(resolveLocaleCode("en")).toEqual("en-US");
expect(resolveLocaleCode("fr")).toEqual("fr-FR");
expect(resolveLocaleCode("az")).toEqual("az-AZ");
});

it("should return the full locale code if it is already provided", () => {
expect(resolveLocaleCode("en-US")).toEqual("en-US");
expect(resolveLocaleCode("fr-CA")).toEqual("fr-CA");
expect(resolveLocaleCode("es-MX")).toEqual("es-MX");
});

it("should throw an error for an invalid or unsupported locale code", () => {
expect(() => resolveLocaleCode("az-US")).toThrow("Invalid locale code");
expect(() => resolveLocaleCode("au")).toThrow("Invalid locale code");
});

it("should return first code for locales with multiple variants", () => {
expect(resolveLocaleCode("sr")).toEqual("sr-RS");
expect(resolveLocaleCode("zh")).toEqual("zh-CN");
});
});


describe("getLocaleCodeDelimiter", () => {
it("should return '-' for locale codes with hyphen delimiter", () => {
expect(getLocaleCodeDelimiter("en-US")).toEqual("-");
expect(getLocaleCodeDelimiter("fr-FR")).toEqual("-");
});

it("should return '_' for locale codes with underscore delimiter", () => {
expect(getLocaleCodeDelimiter("en_US")).toEqual("_");
expect(getLocaleCodeDelimiter("fr_FR")).toEqual("_");
});

it("should return undefined for locale codes without a recognized delimiter", () => {
expect(getLocaleCodeDelimiter("enUS")).toBeNull();
expect(getLocaleCodeDelimiter("frFR")).toBeNull();
});
});

describe("resolveOverridenLocale", () => {
it("should return the same locale if no delimiter is provided", () => {
expect(resolveOverridenLocale("en-US")).toEqual("en-US");
expect(resolveOverridenLocale("fr_FR")).toEqual("fr_FR");
});

it("should replace the delimiter with the specified one", () => {
expect(resolveOverridenLocale("en-US", "_")).toEqual("en_US");
expect(resolveOverridenLocale("fr_FR", "-")).toEqual("fr-FR");
});

it("should return the same locale if no recognized delimiter is found", () => {
expect(resolveOverridenLocale("enUS", "_")).toEqual("enUS");
expect(resolveOverridenLocale("frFR", "-")).toEqual("frFR");
});
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe prettier adds an empty line to files. Please run prettier on all code you contribute. Thanks.

2 changes: 1 addition & 1 deletion packages/spec/src/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export const localeCodeSchema = Z.string().refine((value) => localeCodes.include
message: "Invalid locale code",
});

export const resolveLocaleCode = (value: LocaleCode): LocaleCodeFull => {
export const resolveLocaleCode = (value: string): LocaleCodeFull => {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the function already handles case for an invalid code on line 239, we can make the input param type as string

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! 🚀

const existingFullLocaleCode = Object.values(localeMap)
.flat()
.includes(value as any);
Expand Down
Loading