@schukai/monster 4.38.9 → 4.39.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.
- package/CHANGELOG.md +16 -0
- package/package.json +1 -1
- package/source/components/accessibility/locale-picker.mjs +561 -551
- package/source/components/accessibility/locale-select.mjs +270 -0
- package/source/components/form/select.mjs +2854 -2851
- package/source/i18n/util.mjs +149 -142
package/source/i18n/util.mjs
CHANGED
@@ -23,24 +23,24 @@ import { languages } from "./map/languages.mjs";
|
|
23
23
|
* @return {number} The normalized number as a floating-point value. Returns NaN if the input is not a parsable number.
|
24
24
|
*/
|
25
25
|
export function normalizeNumber(input, locale = navigator.language) {
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
26
|
+
if (input === null || input === undefined) return NaN;
|
27
|
+
if (typeof input === "number") return input; // If input is already a number, return it directly
|
28
|
+
if (typeof input !== "string") return NaN;
|
29
|
+
|
30
|
+
const cleaned = input.trim().replace(/\s/g, "");
|
31
|
+
const decimalSeparator = getDecimalSeparator(locale);
|
32
|
+
let normalized = cleaned;
|
33
|
+
|
34
|
+
if (decimalSeparator === "," && cleaned.includes(".")) {
|
35
|
+
normalized = cleaned.replace(/\./g, "").replace(",", ".");
|
36
|
+
} else if (decimalSeparator === "." && cleaned.includes(",")) {
|
37
|
+
normalized = cleaned.replace(/,/g, "").replace(".", ".");
|
38
|
+
} else if (decimalSeparator === "," && cleaned.includes(",")) {
|
39
|
+
normalized = cleaned.replace(",", ".");
|
40
|
+
}
|
41
|
+
|
42
|
+
const result = parseFloat(normalized);
|
43
|
+
return Number.isNaN(result) ? NaN : result;
|
44
44
|
}
|
45
45
|
|
46
46
|
/**
|
@@ -50,9 +50,9 @@ export function normalizeNumber(input, locale = navigator.language) {
|
|
50
50
|
* @return {string} The decimal separator used in the specified locale.
|
51
51
|
*/
|
52
52
|
function getDecimalSeparator(locale = navigator.language) {
|
53
|
-
|
54
|
-
|
55
|
-
|
53
|
+
const numberWithDecimal = 1.1;
|
54
|
+
const formatted = new Intl.NumberFormat(locale).format(numberWithDecimal);
|
55
|
+
return formatted.replace(/\d/g, "")[0]; // z.B. "," oder "."
|
56
56
|
}
|
57
57
|
|
58
58
|
/**
|
@@ -64,125 +64,132 @@ function getDecimalSeparator(locale = navigator.language) {
|
|
64
64
|
* @return {Object} An object containing information about the detected language, preferred language, and available languages.
|
65
65
|
*/
|
66
66
|
export function detectUserLanguagePreference() {
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
67
|
+
const currentLang = document.documentElement.lang;
|
68
|
+
|
69
|
+
let preferredLanguages = [];
|
70
|
+
|
71
|
+
if (typeof navigator.language === "string" && navigator.language.length > 0) {
|
72
|
+
preferredLanguages = [navigator.language];
|
73
|
+
}
|
74
|
+
|
75
|
+
if (Array.isArray(navigator.languages) && navigator.languages.length > 0) {
|
76
|
+
preferredLanguages = navigator.languages;
|
77
|
+
}
|
78
|
+
|
79
|
+
// add to preferredLanguages all the base languages of the preferred languages
|
80
|
+
preferredLanguages = preferredLanguages.concat(
|
81
|
+
preferredLanguages.map((lang) => lang.split("-")[0]),
|
82
|
+
);
|
83
|
+
|
84
|
+
if (!currentLang && preferredLanguages.length === 0) {
|
85
|
+
return {
|
86
|
+
message: "No language information available.",
|
87
|
+
};
|
88
|
+
}
|
89
|
+
|
90
|
+
const linkTags = document.querySelectorAll("link[hreflang]");
|
91
|
+
if (linkTags.length === 0) {
|
92
|
+
return {
|
93
|
+
current: currentLang || null,
|
94
|
+
message: "No <link> tags with hreflang available.",
|
95
|
+
};
|
96
|
+
}
|
97
|
+
|
98
|
+
const availableLanguages = [...linkTags].map((link) => {
|
99
|
+
const fullLang = link.getAttribute("hreflang");
|
100
|
+
const baseLang = fullLang.split("-")[0];
|
101
|
+
let label = link.getAttribute("data-monster-label");
|
102
|
+
if (!label) {
|
103
|
+
label = link.getAttribute("title");
|
104
|
+
if (!label) {
|
105
|
+
label = languages?.[fullLang];
|
106
|
+
if (!label) {
|
107
|
+
label = languages?.[baseLang];
|
108
|
+
}
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
return {
|
113
|
+
fullLang,
|
114
|
+
baseLang,
|
115
|
+
label,
|
116
|
+
href: link.getAttribute("href"),
|
117
|
+
};
|
118
|
+
});
|
119
|
+
|
120
|
+
// filter availableLanguages to only include languages that are in the preferredLanguages array
|
121
|
+
const offerableLanguages = availableLanguages.filter(
|
122
|
+
(lang) =>
|
123
|
+
preferredLanguages.includes(lang.fullLang) ||
|
124
|
+
preferredLanguages.includes(lang.baseLang),
|
125
|
+
);
|
126
|
+
|
127
|
+
const allOfferableLanguages = availableLanguages.filter(
|
128
|
+
(lang) => lang.baseLang !== currentLang && lang.fullLang !== currentLang,
|
129
|
+
);
|
130
|
+
|
131
|
+
if (offerableLanguages.length === 0) {
|
132
|
+
return {
|
133
|
+
current: currentLang || null,
|
134
|
+
message: "No available languages match the user's preferences.",
|
135
|
+
available: availableLanguages.map((lang) => ({
|
136
|
+
...lang,
|
137
|
+
weight: 1,
|
138
|
+
})),
|
139
|
+
allOfferable: allOfferableLanguages,
|
140
|
+
};
|
141
|
+
}
|
142
|
+
|
143
|
+
// Helper function to determine the "weight" of a language match
|
144
|
+
function getWeight(langEntry) {
|
145
|
+
// Full match has priority 3
|
146
|
+
if (preferredLanguages.includes(langEntry.fullLang)) return 3;
|
147
|
+
// Base language match has priority 2
|
148
|
+
if (preferredLanguages.includes(langEntry.baseLang)) return 2;
|
149
|
+
// No match is priority 1
|
150
|
+
return 1;
|
151
|
+
}
|
152
|
+
|
153
|
+
// Sort the available languages by descending weight
|
154
|
+
offerableLanguages.sort((a, b) => getWeight(b) - getWeight(a));
|
155
|
+
|
156
|
+
// The best match is the first in the sorted list
|
157
|
+
const bestMatch = offerableLanguages[0];
|
158
|
+
const bestMatchWeight = getWeight(bestMatch);
|
159
|
+
|
160
|
+
const currentLabel = languages?.[currentLang] || currentLang;
|
161
|
+
|
162
|
+
// If we found a language that matches user preferences (weight > 0)
|
163
|
+
if (bestMatchWeight > 0) {
|
164
|
+
return {
|
165
|
+
current: currentLang || null,
|
166
|
+
currentLabel: currentLabel,
|
167
|
+
preferred: {
|
168
|
+
full: bestMatch.fullLang,
|
169
|
+
base: bestMatch.baseLang,
|
170
|
+
label: bestMatch.label,
|
171
|
+
href: bestMatch.href,
|
172
|
+
},
|
173
|
+
available: availableLanguages.map((lang) => ({
|
174
|
+
...lang,
|
175
|
+
weight: getWeight(lang),
|
176
|
+
})),
|
177
|
+
offerable: offerableLanguages.map((lang) => ({
|
178
|
+
...lang,
|
179
|
+
weight: getWeight(lang),
|
180
|
+
})),
|
181
|
+
allOfferable: allOfferableLanguages,
|
182
|
+
};
|
183
|
+
}
|
184
|
+
|
185
|
+
// If no language matched the user's preferences
|
186
|
+
return {
|
187
|
+
current: currentLang || null,
|
188
|
+
message: "None of the preferred languages are available.",
|
189
|
+
available: availableLanguages.map((lang) => ({
|
190
|
+
...lang,
|
191
|
+
weight: getWeight(lang),
|
192
|
+
})),
|
193
|
+
allOfferable: allOfferableLanguages,
|
194
|
+
};
|
188
195
|
}
|