@schukai/monster 4.38.2 → 4.38.4

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