UNPKG

laravel-inertia-react-translator

Version:
114 lines (113 loc) 5.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const vitest_1 = require("vitest"); const Translator_1 = __importDefault(require("../src/Translator")); const dictionary = { yes: "Yes", greetings: { hi: "Hi", hello: "Hello :name", }, companies: { label: "Company|Companies", title: "{0}No company available|{1} One company available|[2-10] :count companies available|[11-*] A lot of companies available", name: "{0}No company|{1}Company :name|{2}Company :name and :other|[3-*]More than 2 companies", }, labels: { person: "{0}Nobody|[5-*]A lot of people", dogs: "", }, defaults: { count: "There are :count items", name: "My name is :name", numericValue: "The numeric value is :value", stringValue: "The string value is :value", }, }; const translator = new Translator_1.default(dictionary); const __ = (key, replace) => translator.translate(key, replace); (0, vitest_1.test)("it can use a function as the dictionary", () => { const fTranslator = new Translator_1.default(() => dictionary); (0, vitest_1.expect)(fTranslator.getDictionary()).toEqual(dictionary); (0, vitest_1.expect)(fTranslator.translate("yes")).toBe("Yes"); }); (0, vitest_1.test)("it can translate a simple string", () => { (0, vitest_1.expect)(translator).toBeInstanceOf(Translator_1.default); (0, vitest_1.expect)(__("yes")).toBe("Yes"); (0, vitest_1.expect)(__("not-found")).toBe("not-found"); }); (0, vitest_1.test)("it can translate a string with placeholders", () => { (0, vitest_1.expect)(__("greetings.hi")).toBe("Hi"); (0, vitest_1.expect)(__("greetings.hello", { name: "John" })).toBe("Hello John"); (0, vitest_1.expect)(__("greetings.hello", { name: "Jane", other: "Sam" })).toBe("Hello Jane"); (0, vitest_1.expect)(__("greetings.hello", { other: "Sam" })).toBe("Hello :name"); }); (0, vitest_1.test)("it allows usage of a string or number as a replace value", () => { (0, vitest_1.expect)(__("defaults.count", 5)).toBe("There are 5 items"); (0, vitest_1.expect)(__("defaults.name", "John")).toBe("My name is John"); (0, vitest_1.expect)(__("defaults.numericValue", 5)).toBe("The numeric value is 5"); (0, vitest_1.expect)(__("defaults.stringValue", "five")).toBe("The string value is five"); }); // test("it can translate a plural string", () => { // expect(___("companies.title", 0)).toBe("No company available"); // expect(___("companies.title", 1)).toBe("One company available"); // expect(___("companies.title", 2)).toBe("2 companies available"); // expect(___("companies.title", 10)).toBe("10 companies available"); // expect(___("companies.title", 11)).toBe("A lot of companies available"); // expect(___("companies.title", 100)).toBe("A lot of companies available"); // expect(___("companies.label", 0)).toBe("Companies"); // expect(___("companies.label", 1)).toBe("Company"); // expect(___("companies.label", 2)).toBe("Companies"); // expect(___("hi", 1)).toBe("hi"); // expect(___("hi", 2)).toBe("hi"); // expect(___("companies.name", 0)).toBe("No company"); // expect(___("companies.name", 1, { name: "Apple" })).toBe("Company Apple"); // expect(___("companies.name", 2, { name: "Apple" })).toBe( // "Company Apple and :other" // ); // expect(___("companies.name", 2, { name: "Apple", other: "Microsoft" })).toBe( // "Company Apple and Microsoft" // ); // expect(___("companies.name", 3)).toBe("More than 2 companies"); // //weird cases // expect(___(null)).toBe(null); // expect(___(undefined)).toBe(undefined); // expect(___("")).toBe(""); // expect(___("labels.person", 1)).toBe("{0}Nobody"); // expect(___("labels.non-existing", 5)).toBe("labels.non-existing"); // }); (0, vitest_1.test)("it can translate and replace a key with a missing translation", () => { (0, vitest_1.expect)(__("Hi, my name is :name", { name: "John" })).toBe("Hi, my name is John"); }); (0, vitest_1.test)("it can translate a composite key", () => { //key and count (0, vitest_1.expect)(__("companies.label[1]")).toBe("Company"); (0, vitest_1.expect)(__("companies.label[2]")).toBe("Companies"); (0, vitest_1.expect)(__("companies.title[3]")).toBe("3 companies available"); //key and replace (0, vitest_1.expect)(__("greetings.hello{name:John}")).toBe("Hello John"); //key, count and replace (0, vitest_1.expect)(__("greetings.hello{name:Jane,other:Sam}")).toBe("Hello Jane"); (0, vitest_1.expect)(__("companies.name[2]{name:Apple,other:Microsoft}")).toBe("Company Apple and Microsoft"); //non-existing key and replace (0, vitest_1.expect)(__("Hi, my name is :name!{name:John}")).toBe("Hi, my name is John!"); //key and count for translation without plural forms (0, vitest_1.expect)(__("hi[1]")).toBe("hi"); (0, vitest_1.expect)(__("hi[2]{name: John}")).toBe("hi"); //bad count // expect(___("companies.title[0")).toBe("companies.title[0"); // expect(___("companies.title[abc]")).toBe("companies.title[abc]"); //bad replace (0, vitest_1.expect)(__("greetings.hello{name:John")).toBe("greetings.hello{name:John"); //bad count and replace (0, vitest_1.expect)(__("companies.name[2]{name:Apple")).toBe("companies.name[2]{name:Apple"); //bad key (0, vitest_1.expect)(__("[{")).toBe("[{"); //empty key (0, vitest_1.expect)(__("")).toBe(""); // expect(__(null)).toBe(null); // expect(__(undefined)).toBe(undefined); });