i18n-ai-translate
Version:
AI-powered localization CLI, Node library, and GitHub Action. Translate i18next JSON, Gettext PO, Java .properties, and iOS .strings with ChatGPT, Claude, Gemini, or local Ollama models.
86 lines • 4.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
describe("getLanguageCodeFromFilename", () => {
it("returns a plain ISO-639-1 code as-is", () => {
expect((0, utils_1.getLanguageCodeFromFilename)("fr.json")).toBe("fr");
expect((0, utils_1.getLanguageCodeFromFilename)("/a/b/de.json")).toBe("de");
});
it("accepts BCP-47 tags by falling back to the language subtag", () => {
expect((0, utils_1.getLanguageCodeFromFilename)("es-ES.json")).toBe("es");
expect((0, utils_1.getLanguageCodeFromFilename)("pt-BR.json")).toBe("pt");
expect((0, utils_1.getLanguageCodeFromFilename)("zh-CN.json")).toBe("zh");
});
it("strips additional extensions", () => {
expect((0, utils_1.getLanguageCodeFromFilename)("fr.locale.json")).toBe("fr");
});
it("returns the raw prefix if neither form is a valid ISO-639-1 code", () => {
// Caller decides what to do with an unknown code.
expect((0, utils_1.getLanguageCodeFromFilename)("klingon.json")).toBe("klingon");
});
});
describe("getLanguageName", () => {
it("expands common ISO-639-1 codes to English names", () => {
expect((0, utils_1.getLanguageName)("en")).toBe("English");
expect((0, utils_1.getLanguageName)("fr")).toBe("French");
expect((0, utils_1.getLanguageName)("ja")).toBe("Japanese");
expect((0, utils_1.getLanguageName)("zh")).toBe("Chinese");
});
it("returns the raw code when the lookup fails", () => {
expect((0, utils_1.getLanguageName)("xx")).toBe("xx");
expect((0, utils_1.getLanguageName)("klingon")).toBe("klingon");
});
});
describe("resolveLanguageCode", () => {
it("returns valid ISO-639-1 codes unchanged", () => {
expect((0, utils_1.resolveLanguageCode)("en")).toBe("en");
expect((0, utils_1.resolveLanguageCode)("fr")).toBe("fr");
});
it("resolves English names to their ISO code", () => {
expect((0, utils_1.resolveLanguageCode)("English")).toBe("en");
expect((0, utils_1.resolveLanguageCode)("French")).toBe("fr");
expect((0, utils_1.resolveLanguageCode)("Japanese")).toBe("ja");
});
it("is case-insensitive and tolerates whitespace", () => {
expect((0, utils_1.resolveLanguageCode)("ENGLISH")).toBe("en");
expect((0, utils_1.resolveLanguageCode)(" english ")).toBe("en");
expect((0, utils_1.resolveLanguageCode)("EnGlIsH")).toBe("en");
});
it("returns the raw input when no match is found", () => {
expect((0, utils_1.resolveLanguageCode)("Klingon")).toBe("Klingon");
expect((0, utils_1.resolveLanguageCode)("xx")).toBe("xx");
});
});
describe("getTranslationDirectoryKey", () => {
it("survives Windows-style paths that contain a drive-letter colon", () => {
// Before the delimiter fix this would have produced a compound
// key with two colons and split() would have shredded the path.
// Paths are normalised to forward slashes so the language-swap
// replace works on both platforms.
const key = (0, utils_1.getTranslationDirectoryKey)("C:\\repo\\i18n\\en\\app.json", "welcome", "en", "fr");
expect(key).toBe(`C:/repo/i18n/fr/app.json${utils_1.DIRECTORY_KEY_DELIMITER}welcome`);
// Round-trip: the key part should come back out intact.
const [pathPart, keyPart] = key.split(utils_1.DIRECTORY_KEY_DELIMITER);
expect(pathPart).toBe("C:/repo/i18n/fr/app.json");
expect(keyPart).toBe("welcome");
});
it("swaps the input language segment for the output language", () => {
const key = (0, utils_1.getTranslationDirectoryKey)("/base/en/app.json", "hello", "en", "fr");
expect(key.startsWith("/base/fr/app.json")).toBe(true);
});
it("swaps the language segment on Windows-style backslash paths", () => {
// Before the normalisation fix the replace looked for '/en/'
// against a path containing '\\en\\', found nothing, and
// silently left the language segment unchanged.
const key = (0, utils_1.getTranslationDirectoryKey)("C:\\repo\\i18n\\en\\app.json", "welcome", "en", "fr");
expect(key.startsWith("C:/repo/i18n/fr/app.json")).toBe(true);
expect(key.includes("/en/")).toBe(false);
});
it("handles mixed separators on the same path", () => {
// Happens when users join via path.posix on Windows or build
// paths via concatenation rather than path.join.
const key = (0, utils_1.getTranslationDirectoryKey)("C:\\repo/i18n/en\\app.json", "hello", "en", "fr");
expect(key.startsWith("C:/repo/i18n/fr/app.json")).toBe(true);
});
});
//# sourceMappingURL=utils.spec.js.map