@schukai/monster 4.27.0 → 4.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,7 +41,7 @@ class Formatter extends TextFormatter {
41
41
  * @param {object} object
42
42
  * @param {Translations} translation
43
43
  * @param {object} [options]
44
- * @throws {TypeError} value is not a object
44
+ * @throws {TypeError} value is not an object
45
45
  */
46
46
  constructor(object, translation, options) {
47
47
  super(object, options);
@@ -12,21 +12,21 @@
12
12
  * SPDX-License-Identifier: AGPL-3.0
13
13
  */
14
14
 
15
- import { instanceSymbol } from "../constants.mjs";
16
- import { getLinkedObjects, hasObjectLink } from "../dom/attributes.mjs";
17
- import { ATTRIBUTE_OBJECTLINK } from "../dom/constants.mjs";
18
- import { getDocument } from "../dom/util.mjs";
19
- import { Base } from "../types/base.mjs";
20
- import { isObject, isString } from "../types/is.mjs";
15
+ import {instanceSymbol} from "../constants.mjs";
16
+ import {getLinkedObjects, hasObjectLink} from "../dom/attributes.mjs";
17
+ import {ATTRIBUTE_OBJECTLINK} from "../dom/constants.mjs";
18
+ import {getDocument} from "../dom/util.mjs";
19
+ import {Base} from "../types/base.mjs";
20
+ import {isObject, isString} from "../types/is.mjs";
21
21
  import {
22
- validateInteger,
23
- validateObject,
24
- validateString,
22
+ validateInteger,
23
+ validateObject,
24
+ validateString,
25
25
  } from "../types/validate.mjs";
26
- import { Locale, parseLocale } from "./locale.mjs";
27
- import { translationsLinkSymbol } from "./provider.mjs";
26
+ import {Locale, parseLocale} from "./locale.mjs";
27
+ import {translationsLinkSymbol} from "./provider.mjs";
28
28
 
29
- export { Translations, getDocumentTranslations };
29
+ export {Translations, getDocumentTranslations};
30
30
 
31
31
  /**
32
32
  * With this class you can manage translations and access the keys.
@@ -38,176 +38,178 @@ export { Translations, getDocumentTranslations };
38
38
  * @see https://datatracker.ietf.org/doc/html/rfc3066
39
39
  */
40
40
  class Translations extends Base {
41
- /**
42
- *
43
- * @param {Locale} locale
44
- */
45
- constructor(locale) {
46
- super();
47
-
48
- if (locale instanceof Locale) {
49
- this.locale = locale;
50
- } else {
51
- this.locale = parseLocale(validateString(locale));
52
- }
53
-
54
- this.storage = new Map();
55
- }
56
-
57
- /**
58
- * This method is called by the `instanceof` operator.
59
- * @return {symbol}
60
- * @since 3.27.0
61
- */
62
- static get [instanceSymbol]() {
63
- return Symbol.for("@schukai/monster/i18n/translations@@instance");
64
- }
65
-
66
- /**
67
- * Fetches a text using the specified key.
68
- * If no suitable key is found, `defaultText` is taken.
69
- *
70
- * @param {string} key
71
- * @param {string|undefined} defaultText
72
- * @return {string}
73
- * @throws {Error} key not found
74
- */
75
- getText(key, defaultText) {
76
- if (!this.storage.has(key)) {
77
- if (defaultText === undefined) {
78
- throw new Error(`key ${key} not found`);
79
- }
80
-
81
- return validateString(defaultText);
82
- }
83
-
84
- const r = this.storage.get(key);
85
- if (isObject(r)) {
86
- return this.getPluralRuleText(key, "other", defaultText);
87
- }
88
-
89
- return this.storage.get(key);
90
- }
91
-
92
- /**
93
- * A number `count` can be passed to this method. In addition to a number, one of the keywords can also be passed directly.
94
- * "zero", "one", "two", "few", "many" and "other". Remember: not every language has all rules.
95
- *
96
- * The appropriate text for this number is then selected. If no suitable key is found, `defaultText` is taken.
97
- *
98
- * @param {string} key
99
- * @param {integer|string} count
100
- * @param {string|undefined} defaultText
101
- * @return {string}
102
- */
103
- getPluralRuleText(key, count, defaultText) {
104
- if (!this.storage.has(key)) {
105
- return validateString(defaultText);
106
- }
107
-
108
- const r = validateObject(this.storage.get(key));
109
-
110
- let keyword;
111
- if (isString(count)) {
112
- keyword = count.toLocaleString();
113
- } else {
114
- count = validateInteger(count);
115
- if (count === 0) {
116
- // special handling for zero count
117
- if (r.hasOwnProperty("zero")) {
118
- return validateString(r?.zero);
119
- }
120
- }
121
-
122
- keyword = new Intl.PluralRules(this.locale.toString()).select(
123
- validateInteger(count),
124
- );
125
- }
126
-
127
- if (r.hasOwnProperty(keyword)) {
128
- return validateString(r[keyword]);
129
- }
130
-
131
- // @deprecated since 2023-03-14
132
- // DEFAULT_KEY is undefined
133
- // if (r.hasOwnProperty(DEFAULT_KEY)) {
134
- // return validateString(r[DEFAULT_KEY]);
135
- // }
136
-
137
- return validateString(defaultText);
138
- }
139
-
140
- /**
141
- * Set a text for a key
142
- *
143
- * ```
144
- * translations.setText("text1", "Make my day!");
145
- * // plural rules
146
- * translations.setText("text6", {
147
- * "zero": "There are no files on Disk.",
148
- * "one": "There is one file on Disk.",
149
- * "other": "There are files on Disk."
150
- * "default": "There are files on Disk."
151
- * });
152
- * ```
153
- *
154
- * @param {string} key
155
- * @param {string|object} text
156
- * @return {Translations}
157
- * @throws {TypeError} value is not a string or object
158
- */
159
- setText(key, text) {
160
- if (isString(text) || isObject(text)) {
161
- this.storage.set(validateString(key), text);
162
- return this;
163
- }
164
-
165
- throw new TypeError("value is not a string or object");
166
- }
167
-
168
- /**
169
- * This method can be used to transfer overlays from an object. The keys are transferred, and the values are entered
170
- * as text.
171
- *
172
- * The values can either be character strings or, in the case of texts with plural forms, objects. The plural forms
173
- * must be stored as text via a standard key "zero", "one", "two", "few", "many" and "other".
174
- *
175
- * Additionally, the key default can be specified, which will be used if no other key fits.
176
- *
177
- * In some languages, like for example in German, there is no own more number at the value 0. In these languages,
178
- * the function applies additionally zero.
179
- *
180
- * ```
181
- * translations.assignTranslations({
182
- * "text1": "Make my day!",
183
- * "text2": "I'll be back!",
184
- * "text6": {
185
- * "zero": "There are no files on Disk.",
186
- * "one": "There is one file on Disk.",
187
- * "other": "There are files on Disk."
188
- * "default": "There are files on Disk."
189
- * });
190
- * ```
191
- *
192
- * @param {object} translations
193
- * @return {Translations}
194
- */
195
- assignTranslations(translations) {
196
- validateObject(translations);
197
-
198
- if (translations instanceof Translations) {
199
- translations.storage.forEach((v, k) => {
200
- this.setText(k, v);
201
- });
202
- return this;
203
- }
204
-
205
- for (const [k, v] of Object.entries(translations)) {
206
- this.setText(k, v);
207
- }
208
-
209
- return this;
210
- }
41
+ /**
42
+ *
43
+ * @param {Locale} locale
44
+ */
45
+ constructor(locale) {
46
+ super();
47
+
48
+ if (locale instanceof Locale) {
49
+ this.locale = locale;
50
+ } else if (isString(locale)) {
51
+ this.locale = parseLocale(validateString(locale));
52
+ } else {
53
+ this.locale = getDocumentTranslations()
54
+ }
55
+
56
+ this.storage = new Map();
57
+ }
58
+
59
+ /**
60
+ * This method is called by the `instanceof` operator.
61
+ * @return {symbol}
62
+ * @since 3.27.0
63
+ */
64
+ static get [instanceSymbol]() {
65
+ return Symbol.for("@schukai/monster/i18n/translations@@instance");
66
+ }
67
+
68
+ /**
69
+ * Fetches a text using the specified key.
70
+ * If no suitable key is found, `defaultText` is taken.
71
+ *
72
+ * @param {string} key
73
+ * @param {string|undefined} defaultText
74
+ * @return {string}
75
+ * @throws {Error} key not found
76
+ */
77
+ getText(key, defaultText) {
78
+ if (!this.storage.has(key)) {
79
+ if (defaultText === undefined) {
80
+ throw new Error(`key ${key} not found`);
81
+ }
82
+
83
+ return validateString(defaultText);
84
+ }
85
+
86
+ const r = this.storage.get(key);
87
+ if (isObject(r)) {
88
+ return this.getPluralRuleText(key, "other", defaultText);
89
+ }
90
+
91
+ return this.storage.get(key);
92
+ }
93
+
94
+ /**
95
+ * A number `count` can be passed to this method. In addition to a number, one of the keywords can also be passed directly.
96
+ * "zero", "one", "two", "few", "many" and "other". Remember: not every language has all rules.
97
+ *
98
+ * The appropriate text for this number is then selected. If no suitable key is found, `defaultText` is taken.
99
+ *
100
+ * @param {string} key
101
+ * @param {integer|string} count
102
+ * @param {string|undefined} defaultText
103
+ * @return {string}
104
+ */
105
+ getPluralRuleText(key, count, defaultText) {
106
+ if (!this.storage.has(key)) {
107
+ return validateString(defaultText);
108
+ }
109
+
110
+ const r = validateObject(this.storage.get(key));
111
+
112
+ let keyword;
113
+ if (isString(count)) {
114
+ keyword = count.toLocaleString();
115
+ } else {
116
+ count = validateInteger(count);
117
+ if (count === 0) {
118
+ // special handling for zero count
119
+ if (r.hasOwnProperty("zero")) {
120
+ return validateString(r?.zero);
121
+ }
122
+ }
123
+
124
+ keyword = new Intl.PluralRules(this.locale.toString()).select(
125
+ validateInteger(count),
126
+ );
127
+ }
128
+
129
+ if (r.hasOwnProperty(keyword)) {
130
+ return validateString(r[keyword]);
131
+ }
132
+
133
+ // @deprecated since 2023-03-14
134
+ // DEFAULT_KEY is undefined
135
+ // if (r.hasOwnProperty(DEFAULT_KEY)) {
136
+ // return validateString(r[DEFAULT_KEY]);
137
+ // }
138
+
139
+ return validateString(defaultText);
140
+ }
141
+
142
+ /**
143
+ * Set a text for a key
144
+ *
145
+ * ```
146
+ * translations.setText("text1", "Make my day!");
147
+ * // plural rules
148
+ * translations.setText("text6", {
149
+ * "zero": "There are no files on Disk.",
150
+ * "one": "There is one file on Disk.",
151
+ * "other": "There are files on Disk."
152
+ * "default": "There are files on Disk."
153
+ * });
154
+ * ```
155
+ *
156
+ * @param {string} key
157
+ * @param {string|object} text
158
+ * @return {Translations}
159
+ * @throws {TypeError} value is not a string or object
160
+ */
161
+ setText(key, text) {
162
+ if (isString(text) || isObject(text)) {
163
+ this.storage.set(validateString(key), text);
164
+ return this;
165
+ }
166
+
167
+ throw new TypeError("value is not a string or object");
168
+ }
169
+
170
+ /**
171
+ * This method can be used to transfer overlays from an object. The keys are transferred, and the values are entered
172
+ * as text.
173
+ *
174
+ * The values can either be character strings or, in the case of texts with plural forms, objects. The plural forms
175
+ * must be stored as text via a standard key "zero", "one", "two", "few", "many" and "other".
176
+ *
177
+ * Additionally, the key default can be specified, which will be used if no other key fits.
178
+ *
179
+ * In some languages, like for example in German, there is no own more number at the value 0. In these languages,
180
+ * the function applies additionally zero.
181
+ *
182
+ * ```
183
+ * translations.assignTranslations({
184
+ * "text1": "Make my day!",
185
+ * "text2": "I'll be back!",
186
+ * "text6": {
187
+ * "zero": "There are no files on Disk.",
188
+ * "one": "There is one file on Disk.",
189
+ * "other": "There are files on Disk."
190
+ * "default": "There are files on Disk."
191
+ * });
192
+ * ```
193
+ *
194
+ * @param {object} translations
195
+ * @return {Translations}
196
+ */
197
+ assignTranslations(translations) {
198
+ validateObject(translations);
199
+
200
+ if (translations instanceof Translations) {
201
+ translations.storage.forEach((v, k) => {
202
+ this.setText(k, v);
203
+ });
204
+ return this;
205
+ }
206
+
207
+ for (const [k, v] of Object.entries(translations)) {
208
+ this.setText(k, v);
209
+ }
210
+
211
+ return this;
212
+ }
211
213
  }
212
214
 
213
215
  /**
@@ -221,34 +223,34 @@ class Translations extends Base {
221
223
  * @throws {Error} Missing translations.
222
224
  */
223
225
  function getDocumentTranslations(element) {
224
- const d = getDocument();
225
-
226
- if (!(element instanceof HTMLElement)) {
227
- element = d.querySelector(
228
- `[${ATTRIBUTE_OBJECTLINK}~="${translationsLinkSymbol.toString()}"]`,
229
- );
230
- if (element === null) {
231
- throw new Error(
232
- "Cannot find the element with translations. Add the translation object to the document.",
233
- );
234
- }
235
- }
236
-
237
- if (!(element instanceof HTMLElement)) {
238
- throw new Error("Element is not an HTMLElement.");
239
- }
240
-
241
- if (!hasObjectLink(element, translationsLinkSymbol)) {
242
- throw new Error("This element has no translations.");
243
- }
244
-
245
- const obj = getLinkedObjects(element, translationsLinkSymbol);
246
-
247
- for (const t of obj) {
248
- if (t instanceof Translations) {
249
- return t;
250
- }
251
- }
252
-
253
- throw new Error("Missing translations.");
226
+ const d = getDocument();
227
+
228
+ if (!(element instanceof HTMLElement)) {
229
+ element = d.querySelector(
230
+ `[${ATTRIBUTE_OBJECTLINK}~="${translationsLinkSymbol.toString()}"]`,
231
+ );
232
+ if (element === null) {
233
+ throw new Error(
234
+ "Cannot find the element with translations. Add the translation object to the document.",
235
+ );
236
+ }
237
+ }
238
+
239
+ if (!(element instanceof HTMLElement)) {
240
+ throw new Error("Element is not an HTMLElement.");
241
+ }
242
+
243
+ if (!hasObjectLink(element, translationsLinkSymbol)) {
244
+ throw new Error("This element has no translations.");
245
+ }
246
+
247
+ const obj = getLinkedObjects(element, translationsLinkSymbol);
248
+
249
+ for (const t of obj) {
250
+ if (t instanceof Translations) {
251
+ return t;
252
+ }
253
+ }
254
+
255
+ throw new Error("Missing translations.");
254
256
  }
@@ -156,7 +156,7 @@ function getMonsterVersion() {
156
156
  }
157
157
 
158
158
  /** don't touch, replaced by make with package.json version */
159
- monsterVersion = new Version("4.25.3");
159
+ monsterVersion = new Version("4.28.0");
160
160
 
161
161
  return monsterVersion;
162
162
  }
@@ -7,7 +7,7 @@ describe('Monster', function () {
7
7
  let monsterVersion
8
8
 
9
9
  /** don´t touch, replaced by make with package.json version */
10
- monsterVersion = new Version("4.25.3")
10
+ monsterVersion = new Version("4.28.0")
11
11
 
12
12
  let m = getMonsterVersion();
13
13
 
@@ -9,8 +9,8 @@
9
9
  </head>
10
10
  <body>
11
11
  <div id="headline" style="display: flex;align-items: center;justify-content: center;flex-direction: column;">
12
- <h1 style='margin-bottom: 0.1em;'>Monster 4.25.3</h1>
13
- <div id="lastupdate" style='font-size:0.7em'>last update Do 3. Jul 13:10:44 CEST 2025</div>
12
+ <h1 style='margin-bottom: 0.1em;'>Monster 4.28.0</h1>
13
+ <div id="lastupdate" style='font-size:0.7em'>last update Mo 7. Jul 19:03:48 CEST 2025</div>
14
14
  </div>
15
15
  <div id="mocha-errors"
16
16
  style="color: red;font-weight: bold;display: flex;align-items: center;justify-content: center;flex-direction: column;margin:20px;"></div>