From 763247ba7b6952f60525aa76e0d7e161aa30f4ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0koda?= Date: Sat, 14 Sep 2024 15:09:48 +0200 Subject: [PATCH] Use TypeScript for translation service --- package.json | 2 +- .../v2/quality/api-v2-quality-presenter.mjs | 2 +- .../application-detail-presenter.mjs | 2 +- .../application-list-presenter.mjs | 2 +- .../dataset-detail-presenter.mjs | 2 +- .../dataset-list/dataset-list-presenter.mjs | 2 +- .../local-catalog-list-presenter.mjs | 2 +- .../publisher-list-presenter.mjs | 2 +- .../suggestion-detail-presenter.mjs | 2 +- .../suggestion-list-presenter.mjs | 2 +- server/service/translation-service.mjs | 40 --------------- server/service/translation-service.test.mjs | 2 +- server/service/translation-service.ts | 51 +++++++++++++++++++ tsconfig.json | 1 + 14 files changed, 63 insertions(+), 51 deletions(-) delete mode 100644 server/service/translation-service.mjs create mode 100644 server/service/translation-service.ts diff --git a/package.json b/package.json index 8466b4e..391399e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "type": "module", "scripts": { "start": "tsx ./server/server.ts", - "test": "tsc && node --test", + "test": "tsc && node --import tsx --test", "lint": "eslint . --fix" }, "author": "Digitální a informační agentura", diff --git a/server/http/api/v2/quality/api-v2-quality-presenter.mjs b/server/http/api/v2/quality/api-v2-quality-presenter.mjs index 7005bb3..79b96bf 100644 --- a/server/http/api/v2/quality/api-v2-quality-presenter.mjs +++ b/server/http/api/v2/quality/api-v2-quality-presenter.mjs @@ -1,4 +1,4 @@ -import { createTranslationService } from "../../../../service/translation-service.mjs"; +import { createTranslationService } from "../../../../service/translation-service"; import local from "./api-v2-quality-presenter-localization.mjs"; export default function createHandler(services) { diff --git a/server/http/application-detail/application-detail-presenter.mjs b/server/http/application-detail/application-detail-presenter.mjs index df4987e..8238763 100644 --- a/server/http/application-detail/application-detail-presenter.mjs +++ b/server/http/application-detail/application-detail-presenter.mjs @@ -1,5 +1,5 @@ import { ROUTE } from "../route-name.mjs"; -import { createTranslationService } from "../../service/translation-service.mjs"; +import { createTranslationService } from "../../service/translation-service"; import { parseClientQuery } from "./application-detail-query.mjs"; import { prepareData } from "./application-detail-model.mjs"; import { renderHtml } from "./application-detail-view-html.mjs"; diff --git a/server/http/application-list/application-list-presenter.mjs b/server/http/application-list/application-list-presenter.mjs index 06d55d3..a83cc67 100644 --- a/server/http/application-list/application-list-presenter.mjs +++ b/server/http/application-list/application-list-presenter.mjs @@ -1,5 +1,5 @@ import { ROUTE } from "../route-name.mjs"; -import { createTranslationService } from "../../service/translation-service.mjs"; +import { createTranslationService } from "../../service/translation-service"; import { parseClientQuery, beforeLinkCallback } from "./application-list-query.mjs"; import { prepareData } from "./application-list-model.mjs"; import { renderHtml } from "./application-list-view-html.mjs"; diff --git a/server/http/dataset-detail/dataset-detail-presenter.mjs b/server/http/dataset-detail/dataset-detail-presenter.mjs index cb26214..06fbef1 100644 --- a/server/http/dataset-detail/dataset-detail-presenter.mjs +++ b/server/http/dataset-detail/dataset-detail-presenter.mjs @@ -1,5 +1,5 @@ import { ROUTE } from "../route-name.mjs"; -import { createTranslationService } from "../../service/translation-service.mjs"; +import { createTranslationService } from "../../service/translation-service"; import { parseClientQuery, beforeLinkCallback } from "./dataset-detail-query.mjs"; import { prepareData } from "./dataset-detail-model.mjs"; import { renderHtml } from "./dataset-detail-view-html.mjs"; diff --git a/server/http/dataset-list/dataset-list-presenter.mjs b/server/http/dataset-list/dataset-list-presenter.mjs index 5322056..51fcefa 100644 --- a/server/http/dataset-list/dataset-list-presenter.mjs +++ b/server/http/dataset-list/dataset-list-presenter.mjs @@ -1,5 +1,5 @@ import { ROUTE } from "../route-name.mjs"; -import { createTranslationService } from "../../service/translation-service.mjs"; +import { createTranslationService } from "../../service/translation-service"; import { parseClientQuery, beforeLinkCallback } from "./dataset-list-query.mjs"; import { prepareData } from "./dataset-list-model.mjs"; import { renderHtml } from "./dataset-list-view-html.mjs"; diff --git a/server/http/local-catalog-list/local-catalog-list-presenter.mjs b/server/http/local-catalog-list/local-catalog-list-presenter.mjs index 6ce6f2d..39cdc34 100644 --- a/server/http/local-catalog-list/local-catalog-list-presenter.mjs +++ b/server/http/local-catalog-list/local-catalog-list-presenter.mjs @@ -1,5 +1,5 @@ import { ROUTE } from "../route-name.mjs"; -import { createTranslationService } from "../../service/translation-service.mjs"; +import { createTranslationService } from "../../service/translation-service"; import { prepareData } from "./local-catalog-list-model.mjs"; import { renderHtml } from "./local-catalog-list-view-html.mjs"; import localization from "./local-catalog-list-localization.mjs"; diff --git a/server/http/publisher-list/publisher-list-presenter.mjs b/server/http/publisher-list/publisher-list-presenter.mjs index cf3596a..8923a3b 100644 --- a/server/http/publisher-list/publisher-list-presenter.mjs +++ b/server/http/publisher-list/publisher-list-presenter.mjs @@ -1,5 +1,5 @@ import { ROUTE } from "../route-name.mjs"; -import { createTranslationService } from "../../service/translation-service.mjs"; +import { createTranslationService } from "../../service/translation-service"; import { prepareData } from "./publisher-list-model.mjs"; import { renderHtml } from "./publisher-list-view-html.mjs"; import localization from "./publisher-list-localization.mjs"; diff --git a/server/http/suggestion-detail/suggestion-detail-presenter.mjs b/server/http/suggestion-detail/suggestion-detail-presenter.mjs index 36123e5..5b9bd2e 100644 --- a/server/http/suggestion-detail/suggestion-detail-presenter.mjs +++ b/server/http/suggestion-detail/suggestion-detail-presenter.mjs @@ -1,5 +1,5 @@ import { ROUTE } from "../route-name.mjs"; -import { createTranslationService } from "../../service/translation-service.mjs"; +import { createTranslationService } from "../../service/translation-service"; import { parseClientQuery } from "./suggestion-detail-query.mjs"; import { prepareData } from "./suggestion-detail-model.mjs"; import { renderHtml } from "./suggestion-detail-view-html.mjs"; diff --git a/server/http/suggestion-list/suggestion-list-presenter.mjs b/server/http/suggestion-list/suggestion-list-presenter.mjs index fb3ecb6..5e2c06e 100644 --- a/server/http/suggestion-list/suggestion-list-presenter.mjs +++ b/server/http/suggestion-list/suggestion-list-presenter.mjs @@ -1,5 +1,5 @@ import { ROUTE } from "../route-name.mjs"; -import { createTranslationService } from "../../service/translation-service.mjs"; +import { createTranslationService } from "../../service/translation-service"; import { parseClientQuery, beforeLinkCallback } from "./suggestion-list-query.mjs"; import { prepareData } from "./suggestion-list-model.mjs"; import { renderHtml } from "./suggestion-list-view-html.mjs"; diff --git a/server/service/translation-service.mjs b/server/service/translation-service.mjs deleted file mode 100644 index 123e08e..0000000 --- a/server/service/translation-service.mjs +++ /dev/null @@ -1,40 +0,0 @@ -/** - * Create translation service from pairs or server local strings. - */ -export function createTranslationService(serverToLocal) { - return { - /** - * @param {string} serverMessage - * @param {number | object | undefined} args - */ - "translate": (serverMessage, args) => - translate(serverToLocal, serverMessage, args), - }; -} - -function translate(serverToLocal, serverMessage, args) { - let result; - const entry = serverToLocal[serverMessage]; - // When given a function we do not care about anything else. - if (entry instanceof Function) { - return entry(args); - } - // We allow for simple "{}" substitution. - if (Array.isArray(entry)) { - // Initial value. - result = entry[0][1]; - for (let [separator, localizedMessage] of entry) { - if (separator > args) { - break; - } - result = localizedMessage; - } - } else { - result = entry; - if (result === undefined) { - console.error("Missing localization entry.", {serverMessage}); - result = ""; - } - } - return result.replace("{}", args); -} diff --git a/server/service/translation-service.test.mjs b/server/service/translation-service.test.mjs index f8429c8..b4ad2d7 100644 --- a/server/service/translation-service.test.mjs +++ b/server/service/translation-service.test.mjs @@ -1,6 +1,6 @@ import assert from "node:assert"; import { describe, it } from "node:test" -import { createTranslationService } from "./translation-service.mjs"; +import { createTranslationService } from "./translation-service"; describe("Translation service.", () => { diff --git a/server/service/translation-service.ts b/server/service/translation-service.ts new file mode 100644 index 0000000..b5eb338 --- /dev/null +++ b/server/service/translation-service.ts @@ -0,0 +1,51 @@ + +export type Translations = Record; + +export interface TranslationService { + translate: (serverMessage: string, args: any) => string; +} + +/** + * Create translation service from pairs or server local strings. + */ +export function createTranslationService(serverToLocal: Translations) { + return new DefaultTranslationService(serverToLocal); +} + +class DefaultTranslationService implements TranslationService { + + readonly serverToLocal: Translations; + + constructor(serverToLocal: Translations) { + this.serverToLocal = serverToLocal; + } + + translate(serverMessage: string, args: any) { + let result; + const entry = this.serverToLocal[serverMessage]; + // When given a function we do not care about anything else. + if (entry instanceof Function) { + return entry(args); + } + // We allow for simple "{}" substitution. + if (Array.isArray(entry)) { + // Initial value. + result = entry[0][1]; + for (let [separator, localizedMessage] of entry) { + if (separator > args) { + break; + } + result = localizedMessage; + } + } else { + result = entry; + if (result === undefined) { + console.error("Missing localization entry.", { serverMessage }); + result = ""; + } + } + return result.replace("{}", args); + } + +} + diff --git a/tsconfig.json b/tsconfig.json index 1b16b0a..e46f88a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,6 +11,7 @@ "esModuleInterop": true, "lib": ["ESNext", "DOM"], "skipLibCheck": true, + "allowJs": true, }, "exclude": [ "node_modules"