Skip to content
Snippets Groups Projects
Verified Commit 3a53507e authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat: new internal Translation

parent 794d42b2
No related branches found
No related tags found
No related merge requests found
...@@ -3,4 +3,4 @@ ...@@ -3,4 +3,4 @@
commit = "cbc74f88e513f20e72635f63627bffbcbe69d607"; commit = "cbc74f88e513f20e72635f63627bffbcbe69d607";
name = "Monster"; name = "Monster";
mnemonic = "monster"; mnemonic = "monster";
} }
\ No newline at end of file
...@@ -19,6 +19,7 @@ import { Server } from "../server.mjs"; ...@@ -19,6 +19,7 @@ import { Server } from "../server.mjs";
import { WriteError } from "./restapi/writeerror.mjs"; import { WriteError } from "./restapi/writeerror.mjs";
import { DataFetchError } from "./restapi/data-fetch-error.mjs"; import { DataFetchError } from "./restapi/data-fetch-error.mjs";
import { clone } from "../../../util/clone.mjs"; import { clone } from "../../../util/clone.mjs";
import {getInternalLocalizationMessage} from "../../../i18n/internal.mjs";
export { RestAPI }; export { RestAPI };
...@@ -224,7 +225,9 @@ function fetchData(init, key, callback) { ...@@ -224,7 +225,9 @@ function fetchData(init, key, callback) {
if (acceptedStatus.indexOf(resp.status) === -1) { if (acceptedStatus.indexOf(resp.status) === -1) {
throw new DataFetchError( throw new DataFetchError(
`the response does not contain an accepted status (actual: ${resp.status}).`, getInternalLocalizationMessage(
`i18n{the-response-does-not-contain-an-accepted-status::status=${resp.status}}`
),
response, response,
); );
} }
...@@ -244,7 +247,9 @@ function fetchData(init, key, callback) { ...@@ -244,7 +247,9 @@ function fetchData(init, key, callback) {
} }
throw new DataFetchError( throw new DataFetchError(
`the response does not contain a valid json (actual: ${body}).`, getInternalLocalizationMessage(
`i18n{the-response-does-not-contain-a-valid-json::actual=${body}}`
),
response, response,
); );
} }
......
...@@ -39,6 +39,8 @@ class Formatter extends TextFormatter { ...@@ -39,6 +39,8 @@ class Formatter extends TextFormatter {
* Default values for the markers are `${` and `}` * Default values for the markers are `${` and `}`
* *
* @param {object} object * @param {object} object
* @param {Translations} translation
* @param {object} [options]
* @throws {TypeError} value is not a object * @throws {TypeError} value is not a object
*/ */
constructor(object, translation, options) { constructor(object, translation, options) {
......
/**
* Copyright © schukai GmbH and all contributing authors, {{copyRightYear}}. All rights reserved.
* Node module: @schukai/monster
*
* This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3).
* The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html
*
* For those who do not wish to adhere to the AGPLv3, a commercial license is available.
* Acquiring a commercial license allows you to use this software without complying with the AGPLv3 terms.
* For more information about purchasing a commercial license, please contact schukai GmbH.
*
* SPDX-License-Identifier: AGPL-3.0
*/
import {Formatter} from "./formatter.mjs";
import {getLocaleOfDocument} from "../dom/locale.mjs";
import {Translations} from "./translations.mjs";
export {getInternalLocalizationMessage};
let internalTranslations = null;
getInternalTranslations();
function getInternalTranslations() {
if (internalTranslations) {
return internalTranslations;
}
let locale = "en";
try {
let locale = getLocaleOfDocument();
} catch (error) {
}
let messages = {};
switch (locale.language) {
case "de":
messages = {
"the-response-does-not-contain-an-accepted-status": `Der Server hat die Anfrage nicht akzeptiert (Status \${status}).`,
"the-response-does-not-contain-a-valid-json": `Die Antwort des Servers ist kein gültiges JSON (actual=\${actual}).`,
};
break;
case "es":
messages = {
"the-response-does-not-contain-an-accepted-status": `El servidor no ha aceptado la solicitud (Estado \${status}).`,
"the-response-does-not-contain-a-valid-json": `La respuesta del servidor no es un JSON válido (actual=\${actual}).`,
};
break;
case "zh":
messages = {
"the-response-does-not-contain-an-accepted-status": `服务器未接受请求(状态 \${status})。`,
"the-response-does-not-contain-a-valid-json": `服务器响应不是有效的JSON(实际=\${actual})。`,
};
break;
case "fr":
messages = {
"the-response-does-not-contain-an-accepted-status": `Le serveur n'a pas accepté la demande (Statut \${status}).`,
"the-response-does-not-contain-a-valid-json": `La réponse du serveur n'est pas un JSON valide (actuel=\${actual}).`,
};
break;
case "it":
messages = {
"the-response-does-not-contain-an-accepted-status": `Il server non ha accettato la richiesta (Stato \${status}).`,
"the-response-does-not-contain-a-valid-json": `La risposta del server non è un JSON valido (attuale=\${actual}).`,
};
break;
case "nl":
messages = {
"the-response-does-not-contain-an-accepted-status": `De server heeft het verzoek niet geaccepteerd (Status \${status}).`,
"the-response-does-not-contain-a-valid-json": `De serverrespons is geen geldige JSON (actueel=\${actual}).`,
};
break;
case "sv":
messages = {
"the-response-does-not-contain-an-accepted-status": `Servern accepterade inte begäran (Status \${status}).`,
"the-response-does-not-contain-a-valid-json": `Serverns svar är inte en giltig JSON (faktisk=\${actual}).`,
};
break;
case "pl":
messages = {
"the-response-does-not-contain-an-accepted-status": `Serwer nie zaakceptował żądania (Status \${status}).`,
"the-response-does-not-contain-a-valid-json": `Odpowiedź serwera nie jest prawidłowym JSON-em (aktualne=\${actual}).`,
};
break;
case "da":
messages = {
"the-response-does-not-contain-an-accepted-status": `Serveren accepterede ikke forespørgslen (Status \${status}).`,
"the-response-does-not-contain-a-valid-json": `Serverens svar er ikke en gyldig JSON (aktuel=\${actual}).`,
};
break;
case "fi":
messages = {
"the-response-does-not-contain-an-accepted-status": `Palvelin ei hyväksynyt pyyntöä (Tila \${status}).`,
"the-response-does-not-contain-a-valid-json": `Palvelimen vastaus ei ole kelvollinen JSON (todellinen=\${actual}).`,
};
break;
case "no":
messages = {
"the-response-does-not-contain-an-accepted-status": `Serveren aksepterte ikke forespørselen (Status \${status}).`,
"the-response-does-not-contain-a-valid-json": `Serverens respons er ikke en gyldig JSON (faktisk=\${actual}).`,
};
break;
case "cs":
messages = {
"the-response-does-not-contain-an-accepted-status": `Server nepřijal požadavek (Stav \${status}).`,
"the-response-does-not-contain-a-valid-json": `Odpověď serveru není platný JSON (skutečný=\${actual}).`,
};
break;
default: // English
messages = {
"the-response-does-not-contain-an-accepted-status": `The server did not accept the request (Status \${status}).`,
"the-response-does-not-contain-a-valid-json": `The server response is not a valid JSON (actual=\${actual}).`,
};
}
const translation = new Translations(locale);
translation.assignTranslations(messages);
internalTranslations = translation;
return translation
}
/**
* Returns the internal localization message.
* @param message
* @returns {string}
*/
function getInternalLocalizationMessage(message) {
const formatter = new Formatter({}, getInternalTranslations());
return formatter.format(message);
}
...@@ -12,6 +12,35 @@ ...@@ -12,6 +12,35 @@
* SPDX-License-Identifier: AGPL-3.0 * SPDX-License-Identifier: AGPL-3.0
*/ */
/**
* Curretnly from Monster supported languages.
* @type {string[]}
*/
export const currentSupportedLanguages = [
"en", // English
"de", // German
"es", // Spanish
"zh", // Mandarin
"hi", // Hindi
"bn", // Bengali
"pt", // Portuguese
"ru", // Russian
"ja", // Japanese
"pa", // Western Punjabi
"mr", // Marathi
"fr", // French
"it", // Italian
"nl", // Dutch
"sv", // Swedish
"pl", // Polish
"da", // Danish
"fi", // Finnish
"no", // Norwegian
"cs" // Czech
];
export const languages = { export const languages = {
en: "English", en: "English",
"en-GB": "English (United Kingdom)", "en-GB": "English (United Kingdom)",
......
...@@ -13,12 +13,12 @@ ...@@ -13,12 +13,12 @@
*/ */
import { import {
validateFunction, validateFunction,
validateObject, validateObject,
validateString, validateString,
} from "./validate.mjs"; } from "./validate.mjs";
export { getGlobal, getGlobalObject, getGlobalFunction }; export {getGlobal, getGlobalObject, getGlobalFunction};
/** /**
* @type {object} * @type {object}
...@@ -31,39 +31,40 @@ let globalReference; ...@@ -31,39 +31,40 @@ let globalReference;
* @throws {Error} unsupported environment. * @throws {Error} unsupported environment.
*/ */
(function () { (function () {
if (typeof globalThis === "object") { if (typeof globalThis === "object") {
globalReference = globalThis; globalReference = globalThis;
return; return;
} }
if (typeof self !== "undefined") { if (typeof self !== "undefined") {
globalReference = self; globalReference = self;
return; return;
} else if (typeof window !== "undefined") { } else if (typeof window !== "undefined") {
globalReference = window; globalReference = window;
return; return;
} }
Object.defineProperty(Object.prototype, "__monster__", { Object.defineProperty(Object.prototype, "__monster__", {
get: function () { get: function () {
return this; return this;
}, },
configurable: true, configurable: true,
}); });
if (typeof __monster__ === "object") { if (typeof __monster__ === "object") {
__monster__.globalThis = __monster__; __monster__.globalThis = __monster__;
delete Object.prototype.__monster__; delete Object.prototype.__monster__;
globalReference = globalThis; globalReference = globalThis;
return; return;
} }
try { try {
globalReference = Function("return this")(); globalReference = Function("return this")();
} catch (e) {} } catch (e) {
}
throw new Error("unsupported environment."); throw new Error("unsupported environment.");
})(); })();
/** /**
...@@ -76,7 +77,7 @@ let globalReference; ...@@ -76,7 +77,7 @@ let globalReference;
* @return {object} globalThis * @return {object} globalThis
*/ */
function getGlobal() { function getGlobal() {
return globalReference; return globalReference;
} }
/** /**
...@@ -111,12 +112,12 @@ function getGlobal() { ...@@ -111,12 +112,12 @@ function getGlobal() {
* @throws {TypeError} value is not a string * @throws {TypeError} value is not a string
*/ */
function getGlobalObject(name) { function getGlobalObject(name) {
validateString(name); validateString(name);
const o = globalReference?.[name]; const o = globalReference?.[name];
if (typeof o === "undefined") if (typeof o === "undefined")
throw new Error(`the object ${name} is not defined`); throw new Error(`the object ${name} is not defined`);
validateObject(o); validateObject(o);
return o; return o;
} }
/** /**
...@@ -149,10 +150,11 @@ function getGlobalObject(name) { ...@@ -149,10 +150,11 @@ function getGlobalObject(name) {
* @throws {TypeError} value is not a string * @throws {TypeError} value is not a string
*/ */
function getGlobalFunction(name) { function getGlobalFunction(name) {
validateString(name); validateString(name);
const f = globalReference?.[name]; const f = globalReference?.[name];
if (typeof f === "undefined") if (typeof f === "undefined") {
throw new Error(`the function ${name} is not defined`); throw new Error(`the function ${name} is not defined`);
validateFunction(f); }
return f; validateFunction(f);
return f;
} }
...@@ -156,7 +156,7 @@ function getMonsterVersion() { ...@@ -156,7 +156,7 @@ function getMonsterVersion() {
} }
/** don't touch, replaced by make with package.json version */ /** don't touch, replaced by make with package.json version */
monsterVersion = new Version("3.102.6"); monsterVersion = new Version("3.104.1");
return monsterVersion; return monsterVersion;
} }
...@@ -7,7 +7,7 @@ describe('Monster', function () { ...@@ -7,7 +7,7 @@ describe('Monster', function () {
let monsterVersion let monsterVersion
/** don´t touch, replaced by make with package.json version */ /** don´t touch, replaced by make with package.json version */
monsterVersion = new Version("3.102.6") monsterVersion = new Version("3.104.1")
let m = getMonsterVersion(); let m = getMonsterVersion();
......
...@@ -9,8 +9,8 @@ ...@@ -9,8 +9,8 @@
</head> </head>
<body> <body>
<div id="headline" style="display: flex;align-items: center;justify-content: center;flex-direction: column;"> <div id="headline" style="display: flex;align-items: center;justify-content: center;flex-direction: column;">
<h1 style='margin-bottom: 0.1em;'>Monster 3.102.6</h1> <h1 style='margin-bottom: 0.1em;'>Monster 3.104.1</h1>
<div id="lastupdate" style='font-size:0.7em'>last update So 2. Feb 23:18:18 CET 2025</div> <div id="lastupdate" style='font-size:0.7em'>last update Mi 5. Feb 19:28:33 CET 2025</div>
</div> </div>
<div id="mocha-errors" <div id="mocha-errors"
style="color: red;font-weight: bold;display: flex;align-items: center;justify-content: center;flex-direction: column;margin:20px;"></div> style="color: red;font-weight: bold;display: flex;align-items: center;justify-content: center;flex-direction: column;margin:20px;"></div>
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment