@brightspace-ui/intl 3.25.0 → 3.26.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/lib/common.js +11 -131
- package/lib/document-locale-settings.js +141 -0
- package/lib/localize.js +10 -1
- package/package.json +1 -1
package/lib/common.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { DocumentLocaleSettings } from './document-locale-settings.js';
|
|
2
|
+
|
|
1
3
|
export const defaultLocale = 'en';
|
|
2
4
|
export const supportedBaseLocales = ['ar', 'cy', 'da', 'de', 'en', 'es', 'fr', 'haw', 'hi', 'ja', 'ko', 'mi', 'nl', 'pt', 'sv', 'tr', 'zh'];
|
|
3
5
|
export const supportedLangpacks = ['ar', 'cy', 'da', 'de', 'en', 'en-gb', 'es', 'es-es', 'fr', 'fr-fr', 'fr-on', 'haw', 'hi', 'ja', 'ko', 'mi', 'nl', 'pt', 'sv', 'tr', 'zh-cn', 'zh-tw'];
|
|
@@ -89,136 +91,6 @@ export function validateFormatValue(value) {
|
|
|
89
91
|
return value;
|
|
90
92
|
}
|
|
91
93
|
|
|
92
|
-
class DocumentLocaleSettings {
|
|
93
|
-
|
|
94
|
-
constructor() {
|
|
95
|
-
this._cache = new Map();
|
|
96
|
-
this._htmlElem = window.document.getElementsByTagName('html')[0];
|
|
97
|
-
this._listeners = [];
|
|
98
|
-
this._overrides = {};
|
|
99
|
-
this._observer = new MutationObserver(this._handleObserverChange.bind(this));
|
|
100
|
-
this._observer.observe(this._htmlElem, { attributes: true });
|
|
101
|
-
this.sync();
|
|
102
|
-
this._languageInitial = this._language;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
get fallbackLanguage() { return this._fallbackLanguage; }
|
|
106
|
-
set fallbackLanguage(val) {
|
|
107
|
-
const normalized = this._normalize(val);
|
|
108
|
-
if (normalized === this._fallbackLanguage) return;
|
|
109
|
-
this._cache.clear();
|
|
110
|
-
this._fallbackLanguage = normalized;
|
|
111
|
-
this._listeners.forEach((cb) => cb());
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
get language() { return this._language; }
|
|
115
|
-
set language(val) {
|
|
116
|
-
const normalized = this._normalize(val);
|
|
117
|
-
if (normalized === this._language) return;
|
|
118
|
-
this._cache.clear();
|
|
119
|
-
this._language = normalized;
|
|
120
|
-
this._listeners.forEach((cb) => cb());
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
get overrides() { return this._overrides; }
|
|
124
|
-
set overrides(val) {
|
|
125
|
-
if (val.date) {
|
|
126
|
-
if (val.date.calendar) {
|
|
127
|
-
delete val.date.calendar.dayPeriods;
|
|
128
|
-
}
|
|
129
|
-
if (val.date.formats) {
|
|
130
|
-
delete val.date.formats.timeFormats;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
this._cache.clear();
|
|
134
|
-
this._overrides = val;
|
|
135
|
-
this._listeners.forEach((cb) => cb());
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
addChangeListener(cb) {
|
|
139
|
-
this._listeners.push(cb);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
getCacheItem(key, provider) {
|
|
143
|
-
if (!this._cache.has(key)) {
|
|
144
|
-
this._cache.set(key, provider());
|
|
145
|
-
}
|
|
146
|
-
return this._cache.get(key);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
removeChangeListener(cb) {
|
|
150
|
-
const index = this._listeners.indexOf(cb);
|
|
151
|
-
if (index < 0) return;
|
|
152
|
-
this._listeners.splice(index, 1);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
reset() {
|
|
156
|
-
this._cache.clear();
|
|
157
|
-
this._language = this._languageInitial;
|
|
158
|
-
this._fallbackLanguage = null;
|
|
159
|
-
this.overrides = {};
|
|
160
|
-
this.timezone = { name: '', identifier: '' };
|
|
161
|
-
this._listeners = [];
|
|
162
|
-
this.oslo = { batch: null, collection: null, version: null };
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
sync() {
|
|
166
|
-
this.language = this._htmlElem.getAttribute('lang');
|
|
167
|
-
this.fallbackLanguage = this._htmlElem.getAttribute('data-lang-default');
|
|
168
|
-
this.overrides = this._tryParseHtmlElemAttr('data-intl-overrides', {});
|
|
169
|
-
this.timezone = this._tryParseHtmlElemAttr('data-timezone', { name: '', identifier: '' });
|
|
170
|
-
this.oslo = this._tryParseHtmlElemAttr('data-oslo', { batch: null, collection: null, version: null });
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
_handleObserverChange(mutations) {
|
|
174
|
-
let localeAttributeChange = false;
|
|
175
|
-
for (let i = 0; i < mutations.length; i++) {
|
|
176
|
-
const mutation = mutations[i];
|
|
177
|
-
if (mutation.attributeName === 'lang') {
|
|
178
|
-
this.language = this._htmlElem.getAttribute('lang');
|
|
179
|
-
} else if (mutation.attributeName === 'data-lang-default') {
|
|
180
|
-
this.fallbackLanguage = this._htmlElem.getAttribute('data-lang-default');
|
|
181
|
-
} else if (mutation.attributeName === 'data-intl-overrides') {
|
|
182
|
-
this.overrides = this._tryParseHtmlElemAttr('data-intl-overrides', {});
|
|
183
|
-
} else if (mutation.attributeName === 'data-timezone') {
|
|
184
|
-
this.timezone = this._tryParseHtmlElemAttr('data-timezone', { name: '', identifier: '' });
|
|
185
|
-
localeAttributeChange = true;
|
|
186
|
-
} else if (mutation.attributeName === 'data-oslo') {
|
|
187
|
-
this.oslo = this._tryParseHtmlElemAttr('data-oslo', { batch: null, collection: null, version: null });
|
|
188
|
-
localeAttributeChange = true;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
if (localeAttributeChange) this._listeners.forEach((cb) => cb());
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
_normalize(langTag) {
|
|
195
|
-
|
|
196
|
-
if (langTag === undefined || langTag === null) return null;
|
|
197
|
-
|
|
198
|
-
langTag = langTag.trim().toLowerCase();
|
|
199
|
-
|
|
200
|
-
const subtags = langTag.split('-');
|
|
201
|
-
if (subtags.length < 2) {
|
|
202
|
-
return langTag;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
return `${subtags[0]}-${subtags[subtags.length - 1]}`;
|
|
206
|
-
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
_tryParseHtmlElemAttr(attrName, defaultValue) {
|
|
210
|
-
if (this._htmlElem.hasAttribute(attrName)) {
|
|
211
|
-
try {
|
|
212
|
-
return JSON.parse(this._htmlElem.getAttribute(attrName));
|
|
213
|
-
} catch {
|
|
214
|
-
// swallow exception
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
return defaultValue;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
222
94
|
let documentLocaleSettings = null;
|
|
223
95
|
export function getDocumentLocaleSettings() {
|
|
224
96
|
if (documentLocaleSettings === null) {
|
|
@@ -227,8 +99,16 @@ export function getDocumentLocaleSettings() {
|
|
|
227
99
|
return documentLocaleSettings;
|
|
228
100
|
}
|
|
229
101
|
|
|
102
|
+
const localeRegEx = /[^a-zA-Z0-9-]/g;
|
|
103
|
+
|
|
230
104
|
function updateLocalNames() {
|
|
231
|
-
const
|
|
105
|
+
const possibleLocales = [documentLocaleSettings.language, navigator.language, defaultLocale].filter(l => l && !localeRegEx.test(l));
|
|
106
|
+
let localName;
|
|
107
|
+
try {
|
|
108
|
+
localName = new Intl.DisplayNames(possibleLocales, { type: 'language' });
|
|
109
|
+
} catch {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
232
112
|
supportedLocalesDetails.forEach(l => {
|
|
233
113
|
l.localName = localName.of(l.overrideCode || l.code);
|
|
234
114
|
});
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
const host = document.documentElement;
|
|
2
|
+
|
|
3
|
+
const DEFAULTS = {
|
|
4
|
+
pseudoLocalization: {},
|
|
5
|
+
_fallbackLanguage: null,
|
|
6
|
+
overrides: {},
|
|
7
|
+
timezone: { name: '', identifier: '' },
|
|
8
|
+
oslo: { batch: null, collection: null, version: null }
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export class DocumentLocaleSettings {
|
|
12
|
+
|
|
13
|
+
constructor() {
|
|
14
|
+
this._cache = new Map();
|
|
15
|
+
this._listeners = [];
|
|
16
|
+
this._overrides = DEFAULTS.overrides;
|
|
17
|
+
|
|
18
|
+
const observer = new MutationObserver(this._handleObserverChange.bind(this));
|
|
19
|
+
observer.observe(host, { attributes: true });
|
|
20
|
+
|
|
21
|
+
this.sync();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get fallbackLanguage() { return this._fallbackLanguage; }
|
|
25
|
+
set fallbackLanguage(val) {
|
|
26
|
+
const normalized = this._normalize(val);
|
|
27
|
+
if (normalized === this._fallbackLanguage) return;
|
|
28
|
+
this._cache.clear();
|
|
29
|
+
this._fallbackLanguage = normalized;
|
|
30
|
+
this._listeners.forEach((cb) => cb());
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get language() { return this._language; }
|
|
34
|
+
set language(val) {
|
|
35
|
+
const normalized = this._normalize(val);
|
|
36
|
+
if (normalized === this._language) return;
|
|
37
|
+
this._cache.clear();
|
|
38
|
+
this._language = normalized;
|
|
39
|
+
this._listeners.forEach((cb) => cb());
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
get overrides() { return this._overrides; }
|
|
43
|
+
set overrides(val) {
|
|
44
|
+
if (val.date) {
|
|
45
|
+
if (val.date.calendar) {
|
|
46
|
+
delete val.date.calendar.dayPeriods;
|
|
47
|
+
}
|
|
48
|
+
if (val.date.formats) {
|
|
49
|
+
delete val.date.formats.timeFormats;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
this._cache.clear();
|
|
53
|
+
this._overrides = val;
|
|
54
|
+
this._listeners.forEach((cb) => cb());
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
addChangeListener(cb) {
|
|
58
|
+
this._listeners.push(cb);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
getCacheItem(key, provider) {
|
|
62
|
+
if (!this._cache.has(key)) {
|
|
63
|
+
this._cache.set(key, provider());
|
|
64
|
+
}
|
|
65
|
+
return this._cache.get(key);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
removeChangeListener(cb) {
|
|
69
|
+
const index = this._listeners.indexOf(cb);
|
|
70
|
+
if (index < 0) return;
|
|
71
|
+
this._listeners.splice(index, 1);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
reset() {
|
|
75
|
+
this._cache.clear();
|
|
76
|
+
this._language = this._languageInitial;
|
|
77
|
+
this._listeners = [];
|
|
78
|
+
Object.assign(this, DEFAULTS);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
sync() {
|
|
82
|
+
this.language = host.getAttribute('lang');
|
|
83
|
+
this._languageInitial ??= this._language;
|
|
84
|
+
this.fallbackLanguage = host.getAttribute('data-lang-default');
|
|
85
|
+
this.overrides = this._tryParseHtmlElemAttr('data-intl-overrides', DEFAULTS.overrides);
|
|
86
|
+
this.timezone = this._tryParseHtmlElemAttr('data-timezone', DEFAULTS.timezone);
|
|
87
|
+
this.pseudoLocalization = this._tryParseHtmlElemAttr('data-pseudo-localization', DEFAULTS.pseudoLocalization);
|
|
88
|
+
this.oslo = this._tryParseHtmlElemAttr('data-oslo', DEFAULTS.oslo);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
_handleObserverChange(mutations) {
|
|
92
|
+
let localeAttributeChange = false;
|
|
93
|
+
for (let i = 0; i < mutations.length; i++) {
|
|
94
|
+
const mutation = mutations[i];
|
|
95
|
+
if (mutation.attributeName === 'lang') {
|
|
96
|
+
this.language = host.getAttribute('lang');
|
|
97
|
+
} else if (mutation.attributeName === 'data-lang-default') {
|
|
98
|
+
this.fallbackLanguage = host.getAttribute('data-lang-default');
|
|
99
|
+
} else if (mutation.attributeName === 'data-intl-overrides') {
|
|
100
|
+
this.overrides = this._tryParseHtmlElemAttr('data-intl-overrides', DEFAULTS.overrides);
|
|
101
|
+
} else if (mutation.attributeName === 'data-timezone') {
|
|
102
|
+
this.timezone = this._tryParseHtmlElemAttr('data-timezone', DEFAULTS.timezone);
|
|
103
|
+
localeAttributeChange = true;
|
|
104
|
+
} else if (mutation.attributeName === 'data-pseudo-localization') {
|
|
105
|
+
this.pseudoLocalization = this._tryParseHtmlElemAttr('data-pseudo-localization', DEFAULTS.pseudoLocalization);
|
|
106
|
+
localeAttributeChange = true;
|
|
107
|
+
} else if (mutation.attributeName === 'data-oslo') {
|
|
108
|
+
this.oslo = this._tryParseHtmlElemAttr('data-oslo', DEFAULTS.oslo);
|
|
109
|
+
localeAttributeChange = true;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (localeAttributeChange) this._listeners.forEach((cb) => cb());
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
_normalize(langTag) {
|
|
116
|
+
|
|
117
|
+
if (langTag === undefined || langTag === null) return null;
|
|
118
|
+
|
|
119
|
+
langTag = langTag.trim().toLowerCase();
|
|
120
|
+
|
|
121
|
+
const subtags = langTag.split('-');
|
|
122
|
+
if (subtags.length < 2) {
|
|
123
|
+
return langTag;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return `${subtags[0]}-${subtags[subtags.length - 1]}`;
|
|
127
|
+
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
_tryParseHtmlElemAttr(attrName, defaultValue) {
|
|
131
|
+
if (host.hasAttribute(attrName)) {
|
|
132
|
+
try {
|
|
133
|
+
return JSON.parse(host.getAttribute(attrName));
|
|
134
|
+
} catch {
|
|
135
|
+
// swallow exception
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return defaultValue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
}
|
package/lib/localize.js
CHANGED
|
@@ -35,6 +35,13 @@ const noAllowedTagsRegex = getDisallowedTagsRegex([]);
|
|
|
35
35
|
export const getLocalizeClass = (superclass = class {}) => class LocalizeClass extends superclass {
|
|
36
36
|
|
|
37
37
|
static documentLocaleSettings = getDocumentLocaleSettings();
|
|
38
|
+
|
|
39
|
+
static pseudoLocalize(localize, name, ...replacements) {
|
|
40
|
+
const str = localize(name, ...replacements);
|
|
41
|
+
return this.documentLocaleSettings.pseudoLocalization.textFormat
|
|
42
|
+
.replace(/\{(0|1)\}/g, m => { return m === '{0}' ? str : name; });
|
|
43
|
+
}
|
|
44
|
+
|
|
38
45
|
static setLocalizeMarkup(localizeMarkup) {
|
|
39
46
|
this.#localizeMarkup ??= localizeMarkup;
|
|
40
47
|
}
|
|
@@ -258,7 +265,9 @@ export const Localize = class extends getLocalizeClass() {
|
|
|
258
265
|
constructor(config) {
|
|
259
266
|
super();
|
|
260
267
|
super.constructor.setLocalizeMarkup(localizeMarkup);
|
|
261
|
-
this.localize =
|
|
268
|
+
this.localize = super.constructor.documentLocaleSettings.pseudoLocalization?.textFormat
|
|
269
|
+
? (...args) => super.constructor.pseudoLocalize((...args) => super.localize(...args), ...args)
|
|
270
|
+
: (...args) => super.localize(...args);
|
|
262
271
|
this.config = config;
|
|
263
272
|
this.connect();
|
|
264
273
|
}
|
package/package.json
CHANGED