Skip to content

Commit

Permalink
Use TypeScript for translation service
Browse files Browse the repository at this point in the history
  • Loading branch information
skodapetr committed Sep 14, 2024
1 parent 68d5654 commit 763247b
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 51 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion server/http/api/v2/quality/api-v2-quality-presenter.mjs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion server/http/dataset-detail/dataset-detail-presenter.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion server/http/dataset-list/dataset-list-presenter.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion server/http/publisher-list/publisher-list-presenter.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion server/http/suggestion-list/suggestion-list-presenter.mjs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
40 changes: 0 additions & 40 deletions server/service/translation-service.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion server/service/translation-service.test.mjs
Original file line number Diff line number Diff line change
@@ -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.", () => {

Expand Down
51 changes: 51 additions & 0 deletions server/service/translation-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

export type Translations = Record<string, string | Function>;

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);
}

}

1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"esModuleInterop": true,
"lib": ["ESNext", "DOM"],
"skipLibCheck": true,
"allowJs": true,
},
"exclude": [
"node_modules"
Expand Down

0 comments on commit 763247b

Please sign in to comment.