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
2 changes: 1 addition & 1 deletion MN.L10n.Javascript/Javascript/L10n.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions MN.L10n.Javascript/Javascript/L10n.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 35 additions & 35 deletions MN.L10n.Javascript/Javascript/package.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
{
"name": "@multinet/mn-l10n",
"version": "3.0.2",
"description": "Multinet translation library",
"main": "dist/L10n.js",
"types": "dist/types",
"files": [
"src",
"dist"
],
"publishConfig": {
"registry": "https://www.myget.org/F/multinet/npm/"
},
"repository": "https://github.com/MultinetInteractive/MN.L10n",
"author": "Chris Gårdenberg, Linus Centerström",
"license": "GPL-3.0",
"sideEffects": true,
"devDependencies": {
"@babel/core": "^7.19.6",
"@types/jest": "^29.2.1",
"@types/node": "^18.11.9",
"esbuild": "^0.15.13",
"esbuild-plugin-babel": "^0.2.3",
"jest": "^29.2.2",
"ts-jest": "^29.0.3",
"typescript": "^4.8.4"
},
"scripts": {
"build": "tsc && node scripts/build.mjs",
"buildAndMinify": "tsc && node scripts/build.mjs --min",
"test": "node node_modules/jest/bin/jest.js",
"test-watch": "node node_modules/jest/bin/jest.js --watch",
"test-debug": "node --inspect-brk node_modules/jest/bin/jest.js --runInBand",
"test-coverage": "node node_modules/jest/bin/jest.js --collect-coverage",
"buildAndPublish": "yarn buildAndMinify && npm publish"
}
"name": "@multinet/mn-l10n",
"version": "3.1.0",
"description": "Multinet translation library",
"main": "dist/L10n.js",
"types": "dist/types",
"files": [
"src",
"dist"
],
"publishConfig": {
"registry": "https://www.myget.org/F/multinet/npm/"
},
"repository": "https://github.com/MultinetInteractive/MN.L10n",
"author": "Chris Gårdenberg, Linus Centerström",
"license": "GPL-3.0",
"sideEffects": true,
"devDependencies": {
"@babel/core": "^7.19.6",
"@types/jest": "^29.2.1",
"@types/node": "^18.11.9",
"esbuild": "^0.15.13",
"esbuild-plugin-babel": "^0.2.3",
"jest": "^29.2.2",
"ts-jest": "^29.0.3",
"typescript": "^4.8.4"
},
"scripts": {
"build": "tsc && node scripts/build.mjs",
"buildAndMinify": "tsc && node scripts/build.mjs --min",
"test": "node node_modules/jest/bin/jest.js",
"test-watch": "node node_modules/jest/bin/jest.js --watch",
"test-debug": "node --inspect-brk node_modules/jest/bin/jest.js --runInBand",
"test-coverage": "node node_modules/jest/bin/jest.js --collect-coverage",
"buildAndPublish": "yarn buildAndMinify && npm publish"
}
}
230 changes: 127 additions & 103 deletions MN.L10n.Javascript/Javascript/src/internal/GetPhrase.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,118 +3,142 @@ import { getPhrase } from "./GetPhrase";
import * as getL10nModule from "./GetL10n";

jest.mock("./GetL10n");
const getL10nMock = (getL10nModule.getL10n as any) as MockedFunction<
typeof getL10nModule.getL10n
const getL10nMock = getL10nModule.getL10n as any as MockedFunction<
typeof getL10nModule.getL10n
>;

beforeEach(() => getL10nMock.mockReset());

describe("GetPhrase", () => {
it("returns the phrase if l10n is unavailable", () => {
getL10nMock.mockImplementation(() => null);
const phrase = getPhrase("Testar $__count$", {
__count: 1,
});
expect(phrase).toBe("Testar $__count$");
});
it("returns the phrase if it is unavailable", () => {
getL10nMock.mockImplementation(() => ({
Phrases: {},
ruleEvaluator: (c) => c,
}));
const phrase = getPhrase("Testar $__count$", {
__count: 1,
});
expect(phrase).toBe("Testar $__count$");
});
it("uses the correct phrase if available", () => {
getL10nMock.mockImplementation(() => ({
ruleEvaluator: (c) => c,
Phrases: {
["test"]: {
r: {
"0": "test2",
},
},
["test2"]: {
r: {
"0": "test3",
},
},
},
}));
it("returns the phrase if l10n is unavailable", () => {
getL10nMock.mockImplementation(() => null);
const phrase = getPhrase("Testar $__count$", {
__count: 1
});
expect(phrase).toBe("Testar $__count$");
});
it("returns the phrase if it is unavailable", () => {
getL10nMock.mockImplementation(() => ({
Phrases: {},
ruleEvaluator: (c) => c
}));
const phrase = getPhrase("Testar $__count$", {
__count: 1
});
expect(phrase).toBe("Testar $__count$");
});
it("uses the correct phrase if available", () => {
getL10nMock.mockImplementation(() => ({
ruleEvaluator: (c) => c,
Phrases: {
["test"]: {
r: {
"0": "test2"
}
},
["test2"]: {
r: {
"0": "test3"
}
}
}
}));

const phrase = getPhrase("test");
expect(phrase).toBe("test2");
});
it("uses the 0 phrase if no other is available", () => {
getL10nMock.mockImplementation(() => ({
ruleEvaluator: (c) => c,
Phrases: {
["test"]: {
r: {
"0": "test2",
},
},
["test2"]: {
r: {
"0": "test3",
},
},
},
}));
const phrase = getPhrase("test");
expect(phrase).toBe("test2");
});
it("uses the 0 phrase if no other is available", () => {
getL10nMock.mockImplementation(() => ({
ruleEvaluator: (c) => c,
Phrases: {
["test"]: {
r: {
"0": "test2"
}
},
["test2"]: {
r: {
"0": "test3"
}
}
}
}));

const phrase = getPhrase("test", {
__count: 3,
});
expect(phrase).toBe("test2");
});
const phrase = getPhrase("test", {
__count: 3
});
expect(phrase).toBe("test2");
});

it("uses the correct counted phrase if available", () => {
getL10nMock.mockImplementation(() => ({
ruleEvaluator: (c) => c,
Phrases: {
["test"]: {
r: {
"0": "test2",
"1": "test4",
},
},
["test2"]: {
r: {
"0": "test3",
},
},
},
}));
it("uses the correct counted phrase if available", () => {
getL10nMock.mockImplementation(() => ({
ruleEvaluator: (c) => c,
Phrases: {
["test"]: {
r: {
"0": "test2",
"1": "test4"
}
},
["test2"]: {
r: {
"0": "test3"
}
}
}
}));

const phrase = getPhrase("test", {
__count: 1,
});
expect(phrase).toBe("test4");
});
const phrase = getPhrase("test", {
__count: 1
});
expect(phrase).toBe("test4");
});

it("uses the correct counted phrase if available 2", () => {
getL10nMock.mockImplementation(() => ({
ruleEvaluator: (c) => (c === 1 ? 6 : c),
Phrases: {
["test"]: {
r: {
"0": "test2",
"6": "test4",
},
},
["test2"]: {
r: {
"0": "test3",
},
},
},
}));
it("uses the correct counted phrase if available 2", () => {
getL10nMock.mockImplementation(() => ({
ruleEvaluator: (c) => (c === 1 ? 6 : c),
Phrases: {
["test"]: {
r: {
"0": "test2",
"6": "test4"
}
},
["test2"]: {
r: {
"0": "test3"
}
}
}
}));

const phrase = getPhrase("test", {
__count: 1,
});
expect(phrase).toBe("test4");
});
const phrase = getPhrase("test", {
__count: 1
});
expect(phrase).toBe("test4");
});

it("uses replaces metadata if needed", () => {
getL10nMock.mockImplementation(() => ({
ruleEvaluator: (c) => (c === 1 ? 6 : c),
Phrases: {
["test !ctx=my test data"]: {
r: {
"0": "test2 !ctx=my test data",
"6": "test4 !ctx=my test data"
}
},
["test2"]: {
r: {
"0": "test3"
}
}
}
}));

const phrase = getPhrase("test !ctx=my test data", {
__count: 1
});
expect(phrase).toBe("test4");
});
});
46 changes: 24 additions & 22 deletions MN.L10n.Javascript/Javascript/src/internal/GetPhrase.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import { getL10n } from "./GetL10n";
import { replaceMetadata } from "./MetadataReplacer";

/**
* Fetches the phrase from the internal list of phrases, and evaluates potential count-rules
*/
export function getPhrase(
phrase: string,
args?: {
__count?: number;
}
phrase: string,
args?: {
__count?: number;
}
) {
if ("undefined" === typeof args) args = {};
var l10n = getL10n();
if (l10n == null) {
return phrase;
}
if ("undefined" === typeof args) args = {};
var l10n = getL10n();
if (l10n == null) {
return phrase;
}

var _p = l10n.Phrases[phrase];
var _ri: string | undefined;
if ("undefined" !== typeof args.__count) {
_ri = l10n.ruleEvaluator(args.__count).toString();
}
if ("undefined" !== typeof _p) {
if ("undefined" !== typeof _ri && "undefined" !== typeof _p.r[_ri]) {
phrase = _p.r[_ri]!;
} else {
phrase = _p.r["0"];
}
}
return phrase;
var _p = l10n.Phrases[phrase];
var _ri: string | undefined;
if ("undefined" !== typeof args.__count) {
_ri = l10n.ruleEvaluator(args.__count).toString();
}
if ("undefined" !== typeof _p) {
if ("undefined" !== typeof _ri && "undefined" !== typeof _p.r[_ri]) {
phrase = _p.r[_ri]!;
} else {
phrase = _p.r["0"];
}
}

return replaceMetadata(phrase);
}
Loading
Loading