@schukai/monster 3.9.1 → 3.10.1
Sign up to get free protection for your applications and to get access to all the features.
package/package.json
CHANGED
@@ -11,7 +11,7 @@ import { Pathfinder } from "../data/pathfinder.mjs";
|
|
11
11
|
|
12
12
|
import { parseDataURL } from "../types/dataurl.mjs";
|
13
13
|
import { getGlobalObject } from "../types/global.mjs";
|
14
|
-
import {
|
14
|
+
import {isArray, isFunction, isIterable, isObject, isString} from "../types/is.mjs";
|
15
15
|
import { Observer } from "../types/observer.mjs";
|
16
16
|
import { ProxyObserver } from "../types/proxyobserver.mjs";
|
17
17
|
import { validateFunction, validateInstance, validateObject, validateString } from "../types/validate.mjs";
|
@@ -27,6 +27,7 @@ import {
|
|
27
27
|
import { findDocumentTemplate, Template } from "./template.mjs";
|
28
28
|
import { addObjectWithUpdaterToElement } from "./updater.mjs";
|
29
29
|
import { instanceSymbol } from "../constants.mjs";
|
30
|
+
import {getDocumentTranslations, Translations} from "../i18n/translations.mjs";
|
30
31
|
|
31
32
|
export {
|
32
33
|
CustomElement,
|
@@ -281,6 +282,39 @@ class CustomElement extends HTMLElement {
|
|
281
282
|
};
|
282
283
|
}
|
283
284
|
|
285
|
+
|
286
|
+
/**
|
287
|
+
* This method updates the labels of the element.
|
288
|
+
* The labels are defined in the options object.
|
289
|
+
* The key of the label is used to retrieve the translation from the document.
|
290
|
+
* If the translation is different from the label, the label is updated.
|
291
|
+
*
|
292
|
+
* Before you can use this method, you must have loaded the translations.
|
293
|
+
*
|
294
|
+
* @returns {Monster.DOM.CustomElement}
|
295
|
+
*/
|
296
|
+
updateI18n() {
|
297
|
+
|
298
|
+
const translations = getDocumentTranslations();
|
299
|
+
if (!translations) {
|
300
|
+
return this;
|
301
|
+
}
|
302
|
+
|
303
|
+
const labels = this.getOption("labels");
|
304
|
+
if(!isIterable(labels)){
|
305
|
+
return this;
|
306
|
+
}
|
307
|
+
|
308
|
+
for (const key in labels) {
|
309
|
+
const text = translations.getText(key, labels[key]);
|
310
|
+
if (text !== labels[key]) {
|
311
|
+
this.setOption("labels." + key, text);
|
312
|
+
}
|
313
|
+
|
314
|
+
}
|
315
|
+
return this;
|
316
|
+
}
|
317
|
+
|
284
318
|
/**
|
285
319
|
* There is no check on the name by this class. the developer is responsible for assigning an appropriate tag.
|
286
320
|
* if the name is not valid, registerCustomElement() will issue an error
|
@@ -293,6 +327,7 @@ class CustomElement extends HTMLElement {
|
|
293
327
|
static getTag() {
|
294
328
|
throw new Error("the method getTag must be overwritten by the derived class.");
|
295
329
|
}
|
330
|
+
|
296
331
|
|
297
332
|
/**
|
298
333
|
* At this point a `CSSStyleSheet` object can be returned. If the environment does not
|
@@ -190,10 +190,12 @@ class Translations extends Base {
|
|
190
190
|
/**
|
191
191
|
* Returns the translations for the current document.
|
192
192
|
*
|
193
|
-
* @param element
|
194
|
-
* @returns {
|
195
|
-
* @throws {Error} Element is not an HTMLElement
|
196
|
-
* @throws {Error}
|
193
|
+
* @param {HTMLElement|undefined} [element] - Element to search for translations. Default: element with objectlink @schukai/monster/i18n/translations@@link.
|
194
|
+
* @returns {Translations}
|
195
|
+
* @throws {Error} Element is not an HTMLElement.
|
196
|
+
* @throws {Error} Cannot find element with translations. Add a translations object to the document.
|
197
|
+
* @throws {Error} This element has no translations.
|
198
|
+
* @throws {Error} Missing translations.
|
197
199
|
*/
|
198
200
|
function getDocumentTranslations(element) {
|
199
201
|
|
@@ -201,14 +203,17 @@ function getDocumentTranslations(element) {
|
|
201
203
|
|
202
204
|
if (!(element instanceof HTMLElement)) {
|
203
205
|
element = d.querySelector('['+ATTRIBUTE_OBJECTLINK+'~="' + translationsLinkSymbol.toString() + '"]');
|
206
|
+
if (element === null) {
|
207
|
+
throw new Error("Cannot find element with translations. Add a translations object to the document.");
|
208
|
+
}
|
204
209
|
}
|
205
210
|
|
206
211
|
if (!(element instanceof HTMLElement)) {
|
207
|
-
throw new Error("Element is not an HTMLElement");
|
212
|
+
throw new Error("Element is not an HTMLElement.");
|
208
213
|
}
|
209
214
|
|
210
215
|
if (!hasObjectLink(element, translationsLinkSymbol)) {
|
211
|
-
throw new Error("
|
216
|
+
throw new Error("This element has no translations.");
|
212
217
|
}
|
213
218
|
|
214
219
|
let obj = getLinkedObjects(element, translationsLinkSymbol);
|
@@ -219,7 +224,7 @@ function getDocumentTranslations(element) {
|
|
219
224
|
}
|
220
225
|
}
|
221
226
|
|
222
|
-
throw new Error("Missing translations");
|
227
|
+
throw new Error("Missing translations.");
|
223
228
|
|
224
229
|
}
|
225
230
|
|
package/source/types/version.mjs
CHANGED
package/test/cases/monster.mjs
CHANGED