i18nizeelement 0.3.0 → 0.5.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/README.md +10 -2
- package/dist/i18nizeelement-jamilih-plugin.d.ts +4 -0
- package/dist/i18nizeelement-jamilih-plugin.d.ts.map +1 -0
- package/dist/i18nizeelement-jamilih-plugin.es6.js +168 -149
- package/dist/i18nizeelement-jamilih-plugin.es6.js.map +1 -1
- package/dist/i18nizeelement-jamilih-plugin.umd.js +296 -277
- package/dist/i18nizeelement-jamilih-plugin.umd.js.map +1 -1
- package/dist/i18nizeelement.es6.js +152 -141
- package/dist/i18nizeelement.es6.js.map +1 -1
- package/dist/i18nizeelement.umd.js +257 -246
- package/dist/i18nizeelement.umd.js.map +1 -1
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/package.json +64 -49
- package/src/i18nizeelement-jamilih-plugin.js +14 -3
- package/src/index.js +30 -7
- package/CHANGES.md +0 -28
|
@@ -1,263 +1,274 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}(this, (function () { 'use strict';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
},
|
|
30
|
-
_toUpperCase: function (str, reserveReturnValue) {
|
|
31
|
-
if (typeof str !== 'string') {
|
|
32
|
-
return reserveReturnValue && str;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return str.toUpperCase();
|
|
36
|
-
},
|
|
37
|
-
_trim: function (str, delimiter, reserveReturnValue) {
|
|
38
|
-
var patterns = [];
|
|
39
|
-
var regexp;
|
|
40
|
-
|
|
41
|
-
var addPatterns = function (pattern) {
|
|
42
|
-
// Build trim RegExp pattern and push it to patterns array
|
|
43
|
-
patterns.push('^' + pattern + '+|' + pattern + '+$');
|
|
44
|
-
}; // fix reserveReturnValue value
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (typeof delimiter === 'boolean') {
|
|
48
|
-
reserveReturnValue = delimiter;
|
|
49
|
-
delimiter = null;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (typeof str !== 'string') {
|
|
53
|
-
return reserveReturnValue && str;
|
|
54
|
-
} // Trim based on delimiter array values
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (Array.isArray(delimiter)) {
|
|
58
|
-
// Loop through delimiter array
|
|
59
|
-
delimiter.map(function (item) {
|
|
60
|
-
// Escape delimiter to be valid RegExp Pattern
|
|
61
|
-
var pattern = self._escapeRegExpPattern(item); // Push pattern to patterns array
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
addPatterns(pattern);
|
|
65
|
-
});
|
|
66
|
-
} // Trim based on delimiter string value
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (typeof delimiter === 'string') {
|
|
70
|
-
// Escape delimiter to be valid RegExp Pattern
|
|
71
|
-
var patternDelimiter = self._escapeRegExpPattern(delimiter); // push pattern to patterns array
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
addPatterns(patternDelimiter);
|
|
75
|
-
} // If delimiter is not defined, Trim white spaces
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
if (!delimiter) {
|
|
79
|
-
// Push white space pattern to patterns array
|
|
80
|
-
addPatterns('\\s');
|
|
81
|
-
} // Build RegExp pattern
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
var pattern = '(' + patterns.join('|') + ')'; // Build RegExp object
|
|
85
|
-
|
|
86
|
-
regexp = new RegExp(pattern, 'g'); // trim string for all patterns
|
|
87
|
-
|
|
88
|
-
while (str.match(regexp)) {
|
|
89
|
-
str = str.replace(regexp, '');
|
|
90
|
-
} // Return trim string
|
|
91
|
-
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.i18nizeElement = factory());
|
|
5
|
+
})(this, (function () { 'use strict';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Copyright 2015, Yahoo! Inc.
|
|
9
|
+
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// Private vars - star
|
|
13
|
+
const _regexEscape = /([\.\*\+\^\$\[\]\\\(\)\|\{\}\,\-\:\?])/g; // eslint-disable-line no-useless-escape
|
|
14
|
+
const _regexParseLocale = /^([a-zA-Z]+)([-a-zA-Z]*)$/;
|
|
15
|
+
// Private vars - end
|
|
16
|
+
|
|
17
|
+
class Locale {
|
|
18
|
+
// eslint-disable-line consistent-this
|
|
19
|
+
|
|
20
|
+
constructor(locale) {
|
|
21
|
+
if (typeof locale !== 'string') {
|
|
22
|
+
throw new TypeError('A string argument is expected');
|
|
23
|
+
}
|
|
24
|
+
if (!_regexParseLocale.test(locale)) {
|
|
25
|
+
throw new RangeError('A proper locale must be provided');
|
|
26
|
+
}
|
|
27
|
+
this._locale = locale;
|
|
28
|
+
}
|
|
92
29
|
|
|
30
|
+
// Private functions - star
|
|
31
|
+
static _escapeRegExpPattern(str) {
|
|
32
|
+
if (typeof str !== 'string') {
|
|
93
33
|
return str;
|
|
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
|
-
if (!objLocale) {
|
|
126
|
-
return;
|
|
127
|
-
} // return true if the intel string lang exists in the BID RTL LANGS array else return false
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return self._BIDI_RTL_LANGS.indexOf(objLocale.lang) >= 0;
|
|
131
|
-
},
|
|
132
|
-
getLangDir: function (strLocale) {
|
|
133
|
-
// return 'rtl' if the intel string lang exists in the BID RTL LANGS array else return 'ltr'
|
|
134
|
-
return self.isRtlLang(strLocale) ? 'rtl' : 'ltr';
|
|
135
|
-
} // Public functions - End
|
|
136
|
-
|
|
137
|
-
}; // Const BIDI_RTL_LANGS Array
|
|
138
|
-
// BIDI_RTL_LANGS ref: http://en.wikipedia.org/wiki/Right-to-left
|
|
139
|
-
// Table of scripts in Unicode: https://en.wikipedia.org/wiki/Script_(Unicode)
|
|
140
|
-
|
|
141
|
-
Object.defineProperty(self, '_BIDI_RTL_LANGS', {
|
|
142
|
-
value: ['ae',
|
|
143
|
-
/* Avestan */
|
|
144
|
-
'ar',
|
|
145
|
-
/* 'العربية', Arabic */
|
|
146
|
-
'arc',
|
|
147
|
-
/* Aramaic */
|
|
148
|
-
'bcc',
|
|
149
|
-
/* 'بلوچی مکرانی', Southern Balochi */
|
|
150
|
-
'bqi',
|
|
151
|
-
/* 'بختياري', Bakthiari */
|
|
152
|
-
'ckb',
|
|
153
|
-
/* 'Soranî / کوردی', Sorani */
|
|
154
|
-
'dv',
|
|
155
|
-
/* Dhivehi */
|
|
156
|
-
'fa',
|
|
157
|
-
/* 'فارسی', Persian */
|
|
158
|
-
'glk',
|
|
159
|
-
/* 'گیلکی', Gilaki */
|
|
160
|
-
'he',
|
|
161
|
-
/* 'עברית', Hebrew */
|
|
162
|
-
'ku',
|
|
163
|
-
/* 'Kurdî / كوردی', Kurdish */
|
|
164
|
-
'mzn',
|
|
165
|
-
/* 'مازِرونی', Mazanderani */
|
|
166
|
-
'nqo',
|
|
167
|
-
/* N'Ko */
|
|
168
|
-
'pnb',
|
|
169
|
-
/* 'پنجابی', Western Punjabi */
|
|
170
|
-
'ps',
|
|
171
|
-
/* 'پښتو', Pashto, */
|
|
172
|
-
'sd',
|
|
173
|
-
/* 'سنڌي', Sindhi */
|
|
174
|
-
'ug',
|
|
175
|
-
/* 'Uyghurche / ئۇيغۇرچە', Uyghur */
|
|
176
|
-
'ur',
|
|
177
|
-
/* 'اردو', Urdu */
|
|
178
|
-
'yi'
|
|
179
|
-
/* 'ייִדיש', Yiddish */
|
|
180
|
-
],
|
|
181
|
-
writable: false,
|
|
182
|
-
enumerable: true,
|
|
183
|
-
configurable: false
|
|
184
|
-
});
|
|
185
|
-
var rtlDetect = RtlDetectLib;
|
|
186
|
-
|
|
187
|
-
var rtlDetect_1 = {
|
|
188
|
-
isRtlLang: rtlDetect.isRtlLang,
|
|
189
|
-
getLangDir: rtlDetect.getLangDir
|
|
190
|
-
};
|
|
191
|
-
|
|
192
|
-
// e.g., `i18nizeElement(document.title, {language: 'ar-AR'});`
|
|
193
|
-
|
|
194
|
-
const i18nizeElement = (element, {
|
|
195
|
-
language,
|
|
196
|
-
// These avoid setting if found to be already set with the same value
|
|
197
|
-
// on the closest ancestor
|
|
198
|
-
avoidLangIfSet = true,
|
|
199
|
-
avoidDirIfSet = true,
|
|
200
|
-
// This avoids setting LTR if there is no ancestor with `dir` (since
|
|
201
|
-
// LTR is the default)
|
|
202
|
-
avoidLTRByDefault = true
|
|
203
|
-
} = {}) => {
|
|
204
|
-
if (!element || !language) {
|
|
205
|
-
throw new TypeError('You must supply an element and language');
|
|
34
|
+
}
|
|
35
|
+
return str.replace(_regexEscape, '\\$1');
|
|
36
|
+
}
|
|
37
|
+
static _toLowerCase(str, reserveReturnValue) {
|
|
38
|
+
if (typeof str !== 'string') {
|
|
39
|
+
return reserveReturnValue && str;
|
|
40
|
+
}
|
|
41
|
+
return str.toLowerCase();
|
|
42
|
+
}
|
|
43
|
+
static _toUpperCase(str, reserveReturnValue) {
|
|
44
|
+
if (typeof str !== 'string') {
|
|
45
|
+
return reserveReturnValue && str;
|
|
46
|
+
}
|
|
47
|
+
return str.toUpperCase();
|
|
48
|
+
}
|
|
49
|
+
static _trim(str, delimiter, reserveReturnValue) {
|
|
50
|
+
const patterns = [];
|
|
51
|
+
let regexp;
|
|
52
|
+
const addPatterns = function (pattern) {
|
|
53
|
+
// Build trim RegExp pattern and push it to patterns array
|
|
54
|
+
patterns.push('^' + pattern + '+|' + pattern + '+$');
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// fix reserveReturnValue value
|
|
58
|
+
if (typeof delimiter === 'boolean') {
|
|
59
|
+
reserveReturnValue = delimiter;
|
|
60
|
+
delimiter = null;
|
|
61
|
+
}
|
|
62
|
+
if (typeof str !== 'string') {
|
|
63
|
+
return reserveReturnValue && str;
|
|
206
64
|
}
|
|
207
65
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
66
|
+
// Trim based on delimiter array values
|
|
67
|
+
if (Array.isArray(delimiter)) {
|
|
68
|
+
// Loop through delimiter array
|
|
69
|
+
delimiter.map(item => {
|
|
70
|
+
// Escape delimiter to be valid RegExp Pattern
|
|
71
|
+
const pattern = this._escapeRegExpPattern(item);
|
|
72
|
+
// Push pattern to patterns array
|
|
73
|
+
addPatterns(pattern);
|
|
74
|
+
});
|
|
217
75
|
}
|
|
218
76
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
// 2. There is no ancestor with `dir` (and this is either not an LTR
|
|
227
|
-
// direction or the user isn't avoiding (the default) LTR)
|
|
228
|
-
// 3. The closest ancestor with `dir` has a different `dir` from the
|
|
229
|
-
// direction of the supplied language
|
|
77
|
+
// Trim based on delimiter string value
|
|
78
|
+
if (typeof delimiter === 'string') {
|
|
79
|
+
// Escape delimiter to be valid RegExp Pattern
|
|
80
|
+
const patternDelimiter = this._escapeRegExpPattern(delimiter);
|
|
81
|
+
// push pattern to patterns array
|
|
82
|
+
addPatterns(patternDelimiter);
|
|
83
|
+
}
|
|
230
84
|
|
|
85
|
+
// If delimiter is not defined, Trim white spaces
|
|
86
|
+
if (!delimiter) {
|
|
87
|
+
// Push white space pattern to patterns array
|
|
88
|
+
addPatterns('\\s');
|
|
89
|
+
}
|
|
231
90
|
|
|
232
|
-
|
|
233
|
-
|
|
91
|
+
// Build RegExp pattern
|
|
92
|
+
const pattern = '(' + patterns.join('|') + ')';
|
|
93
|
+
// Build RegExp object
|
|
94
|
+
regexp = new RegExp(pattern, 'g');
|
|
234
95
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
presetDirElement = element.closest('[dir]');
|
|
96
|
+
// trim string for all patterns
|
|
97
|
+
while (str.match(regexp)) {
|
|
98
|
+
str = str.replace(regexp, '');
|
|
239
99
|
}
|
|
240
100
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
!
|
|
250
|
-
|
|
251
|
-
|
|
101
|
+
// Return trim string
|
|
102
|
+
return str;
|
|
103
|
+
}
|
|
104
|
+
static _parseLocale(strLocale) {
|
|
105
|
+
var matches = _regexParseLocale.exec(strLocale); // exec regex
|
|
106
|
+
let parsedLocale;
|
|
107
|
+
let lang;
|
|
108
|
+
let countryCode;
|
|
109
|
+
if (!strLocale || !matches) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
252
112
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
113
|
+
// fix countryCode string by trimming '-' and '_'
|
|
114
|
+
matches[2] = this._trim(matches[2], ['-', '_']);
|
|
115
|
+
lang = this._toLowerCase(matches[1]);
|
|
116
|
+
countryCode = this._toUpperCase(matches[2]) || countryCode;
|
|
117
|
+
|
|
118
|
+
// object with lang, countryCode properties
|
|
119
|
+
parsedLocale = {
|
|
120
|
+
lang,
|
|
121
|
+
countryCode
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// return parsed locale object
|
|
125
|
+
return parsedLocale;
|
|
126
|
+
}
|
|
127
|
+
// Private functions - End
|
|
128
|
+
|
|
129
|
+
_isRtlLang() {
|
|
130
|
+
const objLocale = this.constructor._parseLocale(this._locale);
|
|
131
|
+
|
|
132
|
+
// return true if the intel string lang exists in the BID RTL LANGS array else return false
|
|
133
|
+
return this.constructor._BIDI_RTL_LANGS.indexOf(objLocale.lang) >= 0;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Public functions - star
|
|
137
|
+
get textInfo() {
|
|
138
|
+
// return 'rtl' if the intel string lang exists in the BID RTL LANGS array else return 'ltr'
|
|
139
|
+
const direction = this._isRtlLang() ? 'rtl' : 'ltr';
|
|
140
|
+
return {
|
|
141
|
+
direction
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Public functions - End
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Why not working as static property
|
|
149
|
+
Locale._BIDI_RTL_LANGS = [
|
|
150
|
+
// Const BIDI_RTL_LANGS Array
|
|
151
|
+
// BIDI_RTL_LANGS ref: http://en.wikipedia.org/wiki/Right-to-left
|
|
152
|
+
// Table of scripts in Unicode: https://en.wikipedia.org/wiki/Script_(Unicode)
|
|
153
|
+
'ae', /* Avestan */
|
|
154
|
+
'ar', /* 'العربية', Arabic */
|
|
155
|
+
'arc', /* Aramaic */
|
|
156
|
+
'bcc', /* 'بلوچی مکرانی', Southern Balochi */
|
|
157
|
+
'bqi', /* 'بختياري', Bakthiari */
|
|
158
|
+
'ckb', /* 'Soranî / کوردی', Sorani */
|
|
159
|
+
'dv', /* Dhivehi */
|
|
160
|
+
'fa', /* 'فارسی', Persian */
|
|
161
|
+
'glk', /* 'گیلکی', Gilaki */
|
|
162
|
+
'he', /* 'עברית', Hebrew */
|
|
163
|
+
'ku', /* 'Kurdî / كوردی', Kurdish */
|
|
164
|
+
'mzn', /* 'مازِرونی', Mazanderani */
|
|
165
|
+
'nqo', /* N'Ko */
|
|
166
|
+
'pnb', /* 'پنجابی', Western Punjabi */
|
|
167
|
+
'prs', /* 'دری', Darī */
|
|
168
|
+
'ps', /* 'پښتو', Pashto, */
|
|
169
|
+
'sd', /* 'سنڌي', Sindhi */
|
|
170
|
+
'ug', /* 'Uyghurche / ئۇيغۇرچە', Uyghur */
|
|
171
|
+
'ur', /* 'اردو', Urdu */
|
|
172
|
+
'yi' /* 'ייִדיש', Yiddish */];
|
|
173
|
+
|
|
174
|
+
if (typeof Intl === 'undefined') {
|
|
175
|
+
globalThis.Intl = {};
|
|
176
|
+
}
|
|
177
|
+
if (typeof Intl.Locale === 'undefined') {
|
|
178
|
+
Intl.Locale = Locale;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// e.g., `i18nizeElement(document.title, {language: 'ar-AR'});`
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* @param {HTMLElement} element
|
|
185
|
+
* @param {{
|
|
186
|
+
* language?: string,
|
|
187
|
+
* avoidLangIfSet?: boolean,
|
|
188
|
+
* avoidDirIfSet?: boolean,
|
|
189
|
+
* avoidLTRByDefault?: boolean
|
|
190
|
+
* }} cfg
|
|
191
|
+
*/
|
|
192
|
+
const i18nizeElement = (element, {
|
|
193
|
+
language,
|
|
194
|
+
// These avoid setting if found to be already set with the same value
|
|
195
|
+
// on the closest ancestor
|
|
196
|
+
avoidLangIfSet = true,
|
|
197
|
+
avoidDirIfSet = true,
|
|
198
|
+
// This avoids setting LTR if there is no ancestor with `dir` (since
|
|
199
|
+
// LTR is the default)
|
|
200
|
+
avoidLTRByDefault = true
|
|
201
|
+
} = {}) => {
|
|
202
|
+
if (!element || !language) {
|
|
203
|
+
throw new TypeError('You must supply an element and language');
|
|
204
|
+
}
|
|
205
|
+
let presetLangElement;
|
|
206
|
+
|
|
207
|
+
// We set `lang` if:
|
|
208
|
+
// 1. The user is not avoiding setting `lang` when the supplied language
|
|
209
|
+
// is the same as the closest ancestor with `lang`
|
|
210
|
+
// 2. There is no ancestor with `lang`
|
|
211
|
+
// 3. The closest ancestor with `lang` has a different language from
|
|
212
|
+
// the supplied
|
|
213
|
+
if (avoidLangIfSet) {
|
|
214
|
+
presetLangElement = /** @type {HTMLElement} */
|
|
215
|
+
element.closest('[lang]');
|
|
216
|
+
}
|
|
217
|
+
if (!presetLangElement || presetLangElement.lang !== language) {
|
|
218
|
+
element.lang = language;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// See <https://github.com/tc39/ecma402/issues/205> for request for JavaScript to detect this
|
|
222
|
+
// We set `dir` if:
|
|
223
|
+
// 1. The user is not avoiding setting `dir` when the supplied
|
|
224
|
+
// language's directionality is the same as the closest ancestor
|
|
225
|
+
// with `dir`
|
|
226
|
+
// 2. There is no ancestor with `dir` (and this is either not an LTR
|
|
227
|
+
// direction or the user isn't avoiding (the default) LTR)
|
|
228
|
+
// 3. The closest ancestor with `dir` has a different `dir` from the
|
|
229
|
+
// direction of the supplied language
|
|
230
|
+
const {
|
|
231
|
+
direction: dir
|
|
232
|
+
} =
|
|
233
|
+
/**
|
|
234
|
+
* @type {Intl.Locale & {
|
|
235
|
+
* textInfo: {direction: "ltr"|"rtl"|"ttb"}
|
|
236
|
+
* }}
|
|
237
|
+
*/
|
|
238
|
+
new Intl.Locale(language).textInfo;
|
|
239
|
+
|
|
240
|
+
/** @type {HTMLElement|undefined} */
|
|
241
|
+
let presetDirElement;
|
|
242
|
+
if (avoidDirIfSet ||
|
|
243
|
+
// If avoiding the default LTR except when different (even if not
|
|
244
|
+
// avoiding an (RTL) already-set dir), we need to know if different
|
|
245
|
+
avoidLTRByDefault && dir === 'ltr') {
|
|
246
|
+
presetDirElement = /** @type {HTMLElement} */
|
|
247
|
+
element.closest('[dir]');
|
|
248
|
+
}
|
|
249
|
+
if (
|
|
250
|
+
// If the closest ancestor with `dir` is different, we need to
|
|
251
|
+
// set regardless
|
|
252
|
+
presetDirElement && presetDirElement.dir !== dir ||
|
|
253
|
+
// We set if the user is not avoiding (whether there is no ancestor
|
|
254
|
+
// with `div` or even when there is and the language has the same
|
|
255
|
+
// directionality as the closest ancestor with `dir`) OR
|
|
256
|
+
!avoidDirIfSet ||
|
|
257
|
+
// ...if there is no ancestor with `dir` AND...
|
|
258
|
+
!presetDirElement && (
|
|
259
|
+
// this is either not an LTR direction or the user isn't
|
|
260
|
+
// avoiding (the default) LTR
|
|
261
|
+
!avoidLTRByDefault || dir !== 'ltr')) {
|
|
262
|
+
element.dir = dir;
|
|
263
|
+
|
|
264
|
+
/* c8 ignore next 3 */
|
|
265
|
+
if (dir === 'ttb') {
|
|
266
|
+
element.style.writingMode = 'vertical-lr';
|
|
257
267
|
}
|
|
258
|
-
}
|
|
268
|
+
}
|
|
269
|
+
};
|
|
259
270
|
|
|
260
|
-
|
|
271
|
+
return i18nizeElement;
|
|
261
272
|
|
|
262
|
-
}))
|
|
273
|
+
}));
|
|
263
274
|
//# sourceMappingURL=i18nizeelement.umd.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"i18nizeelement.umd.js","sources":["../node_modules/rtl-detect/lib/rtl-detect.js","../node_modules/rtl-detect/index.js","../src/index.js"],"sourcesContent":["/**\n * Copyright 2015, Yahoo! Inc.\n * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.\n */\n'use strict';\n\nvar self;\nvar RtlDetectLib = self = { // eslint-disable-line consistent-this\n\n // Private functions - star\n _escapeRegExpPattern: function (str) {\n if (typeof str !== 'string') {\n return str;\n }\n return str.replace(/([\\.\\*\\+\\^\\$\\[\\]\\\\\\(\\)\\|\\{\\}\\,\\-\\:\\?])/g, '\\\\$1'); // eslint-disable-line no-useless-escape\n },\n _toLowerCase: function (str, reserveReturnValue) {\n if (typeof str !== 'string') {\n return reserveReturnValue && str;\n }\n return str.toLowerCase();\n },\n _toUpperCase: function (str, reserveReturnValue) {\n if (typeof str !== 'string') {\n return reserveReturnValue && str;\n }\n return str.toUpperCase();\n },\n _trim: function (str, delimiter, reserveReturnValue) {\n var patterns = [];\n var regexp;\n var addPatterns = function (pattern) {\n // Build trim RegExp pattern and push it to patterns array\n patterns.push('^' + pattern + '+|' + pattern + '+$');\n };\n\n // fix reserveReturnValue value\n if (typeof delimiter === 'boolean') {\n reserveReturnValue = delimiter;\n delimiter = null;\n }\n\n if (typeof str !== 'string') {\n return reserveReturnValue && str;\n }\n\n // Trim based on delimiter array values\n if (Array.isArray(delimiter)) {\n // Loop through delimiter array\n delimiter.map(function (item) {\n // Escape delimiter to be valid RegExp Pattern\n var pattern = self._escapeRegExpPattern(item);\n // Push pattern to patterns array\n addPatterns(pattern);\n });\n }\n\n // Trim based on delimiter string value\n if (typeof delimiter === 'string') {\n // Escape delimiter to be valid RegExp Pattern\n var patternDelimiter = self._escapeRegExpPattern(delimiter);\n // push pattern to patterns array\n addPatterns(patternDelimiter);\n }\n\n // If delimiter is not defined, Trim white spaces\n if (!delimiter) {\n // Push white space pattern to patterns array\n addPatterns('\\\\s');\n }\n\n // Build RegExp pattern\n var pattern = '(' + patterns.join('|') + ')';\n // Build RegExp object\n regexp = new RegExp(pattern, 'g');\n\n // trim string for all patterns\n while (str.match(regexp)) {\n str = str.replace(regexp, '');\n }\n\n // Return trim string\n return str;\n },\n\n _parseLocale : function (strLocale) {\n // parse locale regex object\n var regex = /^([a-zA-Z]*)([_\\-a-zA-Z]*)$/;\n var matches = regex.exec(strLocale); // exec regex\n var parsedLocale;\n var lang;\n var countryCode;\n\n if (!strLocale || !matches) {\n return;\n }\n\n // fix countryCode string by trimming '-' and '_'\n matches[2] = self._trim(matches[2], ['-', '_']);\n\n lang = self._toLowerCase(matches[1]);\n countryCode = self._toUpperCase(matches[2]) || countryCode;\n\n // object with lang, countryCode properties\n parsedLocale = {\n lang: lang,\n countryCode: countryCode\n };\n\n // return parsed locale object\n return parsedLocale;\n },\n // Private functions - End\n\n // Public functions - star\n isRtlLang: function (strLocale) {\n var objLocale = self._parseLocale(strLocale);\n if (!objLocale) {\n return;\n }\n // return true if the intel string lang exists in the BID RTL LANGS array else return false\n return (self._BIDI_RTL_LANGS.indexOf(objLocale.lang) >= 0);\n },\n\n getLangDir: function (strLocale) {\n // return 'rtl' if the intel string lang exists in the BID RTL LANGS array else return 'ltr'\n return self.isRtlLang(strLocale) ? 'rtl' : 'ltr';\n }\n\n // Public functions - End\n};\n\n// Const BIDI_RTL_LANGS Array\n// BIDI_RTL_LANGS ref: http://en.wikipedia.org/wiki/Right-to-left\n// Table of scripts in Unicode: https://en.wikipedia.org/wiki/Script_(Unicode)\nObject.defineProperty(self, '_BIDI_RTL_LANGS', {\n value: [\n 'ae',\t/* Avestan */\n 'ar', /* 'العربية', Arabic */\n 'arc', /* Aramaic */\n 'bcc', /* 'بلوچی مکرانی', Southern Balochi */\n 'bqi', /* 'بختياري', Bakthiari */\n 'ckb', /* 'Soranî / کوردی', Sorani */\n 'dv', /* Dhivehi */\n 'fa', /* 'فارسی', Persian */\n 'glk', /* 'گیلکی', Gilaki */\n 'he', /* 'עברית', Hebrew */\n 'ku', /* 'Kurdî / كوردی', Kurdish */\n 'mzn', /* 'مازِرونی', Mazanderani */\n 'nqo', /* N'Ko */\n 'pnb', /* 'پنجابی', Western Punjabi */\n 'ps', /* 'پښتو', Pashto, */\n 'sd', /* 'سنڌي', Sindhi */\n 'ug', /* 'Uyghurche / ئۇيغۇرچە', Uyghur */\n 'ur', /* 'اردو', Urdu */\n 'yi' /* 'ייִדיש', Yiddish */\n ],\n writable: false,\n enumerable: true,\n configurable: false\n});\n\n\nmodule.exports = RtlDetectLib;\n","/**\n * Copyright 2015, Yahoo! Inc.\n * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.\n */\n'use strict';\n\nvar rtlDetect = require('./lib/rtl-detect');\n\nmodule.exports = {\n\n isRtlLang: rtlDetect.isRtlLang,\n\n getLangDir: rtlDetect.getLangDir\n\n};\n","import rtlDetect from 'rtl-detect';\n// https://github.com/shadiabuhilal/rtl-detect/issues/3\n// e.g., `i18nizeElement(document.title, {language: 'ar-AR'});`\nconst i18nizeElement = (element, {\n language,\n // These avoid setting if found to be already set with the same value\n // on the closest ancestor\n avoidLangIfSet = true, avoidDirIfSet = true,\n // This avoids setting LTR if there is no ancestor with `dir` (since\n // LTR is the default)\n avoidLTRByDefault = true\n} = {}) => {\n if (!element || !language) {\n throw new TypeError('You must supply an element and language');\n }\n let presetLangElement;\n\n // We set `lang` if:\n // 1. The user is not avoiding setting `lang` when the supplied language\n // is the same as the closest ancestor with `lang`\n // 2. There is no ancestor with `lang`\n // 3. The closest ancestor with `lang` has a different language from\n // the supplied\n if (avoidLangIfSet) {\n presetLangElement = element.closest('[lang]');\n }\n if (!presetLangElement || presetLangElement.lang !== language) {\n element.lang = language;\n }\n\n // See <https://github.com/tc39/ecma402/issues/205> for request for JavaScript to detect this\n // We set `dir` if:\n // 1. The user is not avoiding setting `dir` when the supplied\n // language's directionality is the same as the closest ancestor\n // with `dir`\n // 2. There is no ancestor with `dir` (and this is either not an LTR\n // direction or the user isn't avoiding (the default) LTR)\n // 3. The closest ancestor with `dir` has a different `dir` from the\n // direction of the supplied language\n const dir = rtlDetect.getLangDir(language);\n let presetDirElement;\n if (avoidDirIfSet ||\n // If avoiding the default LTR except when different (even if not\n // avoiding an (RTL) already-set dir), we need to know if different\n (avoidLTRByDefault && dir === 'ltr')\n ) {\n presetDirElement = element.closest('[dir]');\n }\n if (\n // If the closest ancestor with `dir` is different, we need to\n // set regardless\n (presetDirElement && presetDirElement.dir !== dir) ||\n // We set if the user is not avoiding (whether there is no ancestor\n // with `div` or even when there is and the language has the same\n // directionality as the closest ancestor with `dir`) OR\n (!avoidDirIfSet ||\n // ...if there is no ancestor with `dir` AND...\n (!presetDirElement &&\n // this is either not an LTR direction or the user isn't\n // avoiding (the default) LTR\n (!avoidLTRByDefault || dir !== 'ltr'))\n )\n ) {\n element.dir = dir;\n\n /* istanbul ignore next */\n if (dir === 'ttb') { // Assumes https://github.com/shadiabuhilal/rtl-detect/issues/2\n element.style.writingMode = 'vertical-lr';\n }\n }\n};\nexport default i18nizeElement;\n"],"names":["self","RtlDetectLib","_escapeRegExpPattern","str","replace","_toLowerCase","reserveReturnValue","toLowerCase","_toUpperCase","toUpperCase","_trim","delimiter","patterns","regexp","addPatterns","pattern","push","Array","isArray","map","item","patternDelimiter","join","RegExp","match","_parseLocale","strLocale","regex","matches","exec","parsedLocale","lang","countryCode","isRtlLang","objLocale","_BIDI_RTL_LANGS","indexOf","getLangDir","Object","defineProperty","value","writable","enumerable","configurable","module","rtlDetect","i18nizeElement","element","language","avoidLangIfSet","avoidDirIfSet","avoidLTRByDefault","TypeError","presetLangElement","closest","dir","presetDirElement","style","writingMode"],"mappings":";;;;;;IAAA;;;;AAIA;IAEA,IAAIA,IAAJ;IACA,IAAIC,YAAY,GAAGD,IAAI,GAAG;;;MAGtBE,oBAAoB,EAAE,UAAUC,GAAV,EAAe;YAC7B,OAAOA,GAAP,KAAe,QAAnB,EAA6B;iBAClBA,GAAP;;;eAEGA,GAAG,CAACC,OAAJ,CAAY,yCAAZ,EAAuD,MAAvD,CAAP,CAJiC;OAHf;MAStBC,YAAY,EAAE,UAAUF,GAAV,EAAeG,kBAAf,EAAmC;YACzC,OAAOH,GAAP,KAAe,QAAnB,EAA6B;iBAClBG,kBAAkB,IAAIH,GAA7B;;;eAEGA,GAAG,CAACI,WAAJ,EAAP;OAbkB;MAetBC,YAAY,EAAE,UAAUL,GAAV,EAAeG,kBAAf,EAAmC;YACzC,OAAOH,GAAP,KAAe,QAAnB,EAA6B;iBAClBG,kBAAkB,IAAIH,GAA7B;;;eAEGA,GAAG,CAACM,WAA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,gBAAgB,CAACD,GAAjB,KAAyBA,GAA9C;IAEE;IACA;IACC,GAACL,aAAD;IAEE,GAACM,gBAAD;IAEC;IACC,GAACL,iBAAD,IAAsBI,GAAG,KAAK,KAHhC,CATP,EAcE;IACAR,IAAAA,OAAO,CAACQ,GAAR,GAAcA,GAAd;IAEA;;IACA,QAAIA,GAAG,KAAK,KAAZ,EAAmB;IAAE;IACnBR,MAAAA,OAAO,CAACU,KAAR,CAAcC,WAAd,GAA4B,aAA5B;IACD;IACF;IACF,CAnED;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"i18nizeelement.umd.js","sources":["../node_modules/.pnpm/intl-locale-textinfo-polyfill@1.0.1/node_modules/intl-locale-textinfo-polyfill/lib/Locale.js","../node_modules/.pnpm/intl-locale-textinfo-polyfill@1.0.1/node_modules/intl-locale-textinfo-polyfill/lib/polyfill.js","../src/index.js"],"sourcesContent":["/**\n * Copyright 2015, Yahoo! Inc.\n * Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.\n */\n\n// Private vars - star\nconst _regexEscape = /([\\.\\*\\+\\^\\$\\[\\]\\\\\\(\\)\\|\\{\\}\\,\\-\\:\\?])/g; // eslint-disable-line no-useless-escape\nconst _regexParseLocale = /^([a-zA-Z]+)([-a-zA-Z]*)$/;\n// Private vars - end\n\nclass Locale { // eslint-disable-line consistent-this\n\n constructor (locale) {\n if (typeof locale !== 'string') {\n throw new TypeError('A string argument is expected');\n }\n if (!_regexParseLocale.test(locale)) {\n throw new RangeError('A proper locale must be provided');\n }\n this._locale = locale;\n }\n\n // Private functions - star\n static _escapeRegExpPattern (str) {\n if (typeof str !== 'string') {\n return str;\n }\n return str.replace(_regexEscape, '\\\\$1');\n }\n\n static _toLowerCase (str, reserveReturnValue) {\n if (typeof str !== 'string') {\n return reserveReturnValue && str;\n }\n return str.toLowerCase();\n }\n\n static _toUpperCase (str, reserveReturnValue) {\n if (typeof str !== 'string') {\n return reserveReturnValue && str;\n }\n return str.toUpperCase();\n }\n\n static _trim (str, delimiter, reserveReturnValue) {\n const patterns = [];\n let regexp;\n const addPatterns = function (pattern) {\n // Build trim RegExp pattern and push it to patterns array\n patterns.push('^' + pattern + '+|' + pattern + '+$');\n };\n\n // fix reserveReturnValue value\n if (typeof delimiter === 'boolean') {\n reserveReturnValue = delimiter;\n delimiter = null;\n }\n\n if (typeof str !== 'string') {\n return reserveReturnValue && str;\n }\n\n // Trim based on delimiter array values\n if (Array.isArray(delimiter)) {\n // Loop through delimiter array\n delimiter.map((item) => {\n // Escape delimiter to be valid RegExp Pattern\n const pattern = this._escapeRegExpPattern(item);\n // Push pattern to patterns array\n addPatterns(pattern);\n });\n }\n\n // Trim based on delimiter string value\n if (typeof delimiter === 'string') {\n // Escape delimiter to be valid RegExp Pattern\n const patternDelimiter = this._escapeRegExpPattern(delimiter);\n // push pattern to patterns array\n addPatterns(patternDelimiter);\n }\n\n // If delimiter is not defined, Trim white spaces\n if (!delimiter) {\n // Push white space pattern to patterns array\n addPatterns('\\\\s');\n }\n\n // Build RegExp pattern\n const pattern = '(' + patterns.join('|') + ')';\n // Build RegExp object\n regexp = new RegExp(pattern, 'g');\n\n // trim string for all patterns\n while (str.match(regexp)) {\n str = str.replace(regexp, '');\n }\n\n // Return trim string\n return str;\n }\n\n static _parseLocale (strLocale) {\n var matches = _regexParseLocale.exec(strLocale); // exec regex\n let parsedLocale;\n let lang;\n let countryCode;\n\n if (!strLocale || !matches) {\n return;\n }\n\n // fix countryCode string by trimming '-' and '_'\n matches[2] = this._trim(matches[2], ['-', '_']);\n\n lang = this._toLowerCase(matches[1]);\n countryCode = this._toUpperCase(matches[2]) || countryCode;\n\n // object with lang, countryCode properties\n parsedLocale = {\n lang,\n countryCode\n };\n\n // return parsed locale object\n return parsedLocale;\n }\n // Private functions - End\n\n _isRtlLang () {\n const objLocale = this.constructor._parseLocale(this._locale);\n\n // return true if the intel string lang exists in the BID RTL LANGS array else return false\n return (this.constructor._BIDI_RTL_LANGS.indexOf(objLocale.lang) >= 0);\n }\n\n // Public functions - star\n get textInfo () {\n // return 'rtl' if the intel string lang exists in the BID RTL LANGS array else return 'ltr'\n const direction = this._isRtlLang() ? 'rtl' : 'ltr';\n return { direction };\n }\n\n // Public functions - End\n}\n\n// Why not working as static property\nLocale._BIDI_RTL_LANGS = [\n // Const BIDI_RTL_LANGS Array\n // BIDI_RTL_LANGS ref: http://en.wikipedia.org/wiki/Right-to-left\n // Table of scripts in Unicode: https://en.wikipedia.org/wiki/Script_(Unicode)\n 'ae',\t/* Avestan */\n 'ar', /* 'العربية', Arabic */\n 'arc', /* Aramaic */\n 'bcc', /* 'بلوچی مکرانی', Southern Balochi */\n 'bqi', /* 'بختياري', Bakthiari */\n 'ckb', /* 'Soranî / کوردی', Sorani */\n 'dv', /* Dhivehi */\n 'fa', /* 'فارسی', Persian */\n 'glk', /* 'گیلکی', Gilaki */\n 'he', /* 'עברית', Hebrew */\n 'ku', /* 'Kurdî / كوردی', Kurdish */\n 'mzn', /* 'مازِرونی', Mazanderani */\n 'nqo', /* N'Ko */\n 'pnb', /* 'پنجابی', Western Punjabi */\n 'prs', /* 'دری', Darī */\n 'ps', /* 'پښتو', Pashto, */\n 'sd', /* 'سنڌي', Sindhi */\n 'ug', /* 'Uyghurche / ئۇيغۇرچە', Uyghur */\n 'ur', /* 'اردو', Urdu */\n 'yi' /* 'ייִדיש', Yiddish */\n];\n\nexport default Locale;\n","import Locale from './Locale.js';\n\nif (typeof Intl === 'undefined') {\n globalThis.Intl = {};\n}\n\nif (typeof Intl.Locale === 'undefined') {\n Intl.Locale = Locale;\n}\n","import 'intl-locale-textinfo-polyfill/lib/polyfill.js';\n\n// e.g., `i18nizeElement(document.title, {language: 'ar-AR'});`\n\n/**\n * @param {HTMLElement} element\n * @param {{\n * language?: string,\n * avoidLangIfSet?: boolean,\n * avoidDirIfSet?: boolean,\n * avoidLTRByDefault?: boolean\n * }} cfg\n */\nconst i18nizeElement = (element, {\n language,\n // These avoid setting if found to be already set with the same value\n // on the closest ancestor\n avoidLangIfSet = true, avoidDirIfSet = true,\n // This avoids setting LTR if there is no ancestor with `dir` (since\n // LTR is the default)\n avoidLTRByDefault = true\n} = {}) => {\n if (!element || !language) {\n throw new TypeError('You must supply an element and language');\n }\n let presetLangElement;\n\n // We set `lang` if:\n // 1. The user is not avoiding setting `lang` when the supplied language\n // is the same as the closest ancestor with `lang`\n // 2. There is no ancestor with `lang`\n // 3. The closest ancestor with `lang` has a different language from\n // the supplied\n if (avoidLangIfSet) {\n presetLangElement = /** @type {HTMLElement} */ (\n element.closest('[lang]')\n );\n }\n if (!presetLangElement || presetLangElement.lang !== language) {\n element.lang = language;\n }\n\n // See <https://github.com/tc39/ecma402/issues/205> for request for JavaScript to detect this\n // We set `dir` if:\n // 1. The user is not avoiding setting `dir` when the supplied\n // language's directionality is the same as the closest ancestor\n // with `dir`\n // 2. There is no ancestor with `dir` (and this is either not an LTR\n // direction or the user isn't avoiding (the default) LTR)\n // 3. The closest ancestor with `dir` has a different `dir` from the\n // direction of the supplied language\n const {direction: dir} =\n /**\n * @type {Intl.Locale & {\n * textInfo: {direction: \"ltr\"|\"rtl\"|\"ttb\"}\n * }}\n */ (\n new Intl.Locale(language)\n ).textInfo;\n\n /** @type {HTMLElement|undefined} */\n let presetDirElement;\n if (avoidDirIfSet ||\n // If avoiding the default LTR except when different (even if not\n // avoiding an (RTL) already-set dir), we need to know if different\n (avoidLTRByDefault && dir === 'ltr')\n ) {\n presetDirElement = /** @type {HTMLElement} */ (\n element.closest('[dir]')\n );\n }\n if (\n // If the closest ancestor with `dir` is different, we need to\n // set regardless\n (presetDirElement && presetDirElement.dir !== dir) ||\n // We set if the user is not avoiding (whether there is no ancestor\n // with `div` or even when there is and the language has the same\n // directionality as the closest ancestor with `dir`) OR\n (!avoidDirIfSet ||\n // ...if there is no ancestor with `dir` AND...\n (!presetDirElement &&\n // this is either not an LTR direction or the user isn't\n // avoiding (the default) LTR\n (!avoidLTRByDefault || dir !== 'ltr'))\n )\n ) {\n element.dir = dir;\n\n /* c8 ignore next 3 */\n if (dir === 'ttb') {\n element.style.writingMode = 'vertical-lr';\n }\n }\n};\nexport default i18nizeElement;\n"],"names":["_regexEscape","_regexParseLocale","Locale","constructor","locale","TypeError","test","RangeError","_locale","_escapeRegExpPattern","str","replace","_toLowerCase","reserveReturnValue","toLowerCase","_toUpperCase","toUpperCase","_trim","delimiter","patterns","regexp","addPatterns","pattern","push","Array","isArray","map","item","patternDelimiter","join","RegExp","match","_parseLocale","strLocale","matches","exec","parsedLocale","lang","countryCode","_isRtlLang","objLocale","_BIDI_RTL_LANGS","indexOf","textInfo","direction","Intl","globalThis","i18nizeElement","element","language","avoidLangIfSet","avoidDirIfSet","avoidLTRByDefault","presetLangElement","closest","dir","presetDirElement","style","writingMode"],"mappings":";;;;;;EAAA;EACA;EACA;EACA;;EAEA;EACA,MAAMA,YAAY,GAAG,yCAAyC,CAAC;EAC/D,MAAMC,iBAAiB,GAAG,2BAA2B,CAAA;EACrD;;EAEA,MAAMC,MAAM,CAAC;EAAG;;IAEdC,WAAWA,CAAEC,MAAM,EAAE;EACnB,IAAA,IAAI,OAAOA,MAAM,KAAK,QAAQ,EAAE;EAC9B,MAAA,MAAM,IAAIC,SAAS,CAAC,+BAA+B,CAAC,CAAA;EACtD,KAAA;EACA,IAAA,IAAI,CAACJ,iBAAiB,CAACK,IAAI,CAACF,MAAM,CAAC,EAAE;EACnC,MAAA,MAAM,IAAIG,UAAU,CAAC,kCAAkC,CAAC,CAAA;EAC1D,KAAA;MACA,IAAI,CAACC,OAAO,GAAGJ,MAAM,CAAA;EACvB,GAAA;;EAEA;IACA,OAAOK,oBAAoBA,CAAEC,GAAG,EAAE;EAChC,IAAA,IAAI,OAAOA,GAAG,KAAK,QAAQ,EAAE;EAC3B,MAAA,OAAOA,GAAG,CAAA;EACZ,KAAA;EACA,IAAA,OAAOA,GAAG,CAACC,OAAO,CAACX,YAAY,EAAE,MAAM,CAAC,CAAA;EAC1C,GAAA;EAEA,EAA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AA,CAACJ,aAAa;EACb;EACC,EAAA,CAACK,gBAAgB;EAChB;EACA;EACC,EAAA,CAACJ,iBAAiB,IAAIG,GAAG,KAAK,KAAK,CACvC,EACH;MACAP,OAAO,CAACO,GAAG,GAAGA,GAAG,CAAA;;EAEjB;MACA,IAAIA,GAAG,KAAK,KAAK,EAAE;EACjBP,MAAAA,OAAO,CAACS,KAAK,CAACC,WAAW,GAAG,aAAa,CAAA;EAC3C,KAAA;EACF,GAAA;EACF;;;;;;;;","x_google_ignoreList":[0,1]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export default i18nizeElement;
|
|
2
|
+
/**
|
|
3
|
+
* @param {HTMLElement} element
|
|
4
|
+
* @param {{
|
|
5
|
+
* language?: string,
|
|
6
|
+
* avoidLangIfSet?: boolean,
|
|
7
|
+
* avoidDirIfSet?: boolean,
|
|
8
|
+
* avoidLTRByDefault?: boolean
|
|
9
|
+
* }} cfg
|
|
10
|
+
*/
|
|
11
|
+
declare function i18nizeElement(element: HTMLElement, { language, avoidLangIfSet, avoidDirIfSet, avoidLTRByDefault }?: {
|
|
12
|
+
language?: string;
|
|
13
|
+
avoidLangIfSet?: boolean;
|
|
14
|
+
avoidDirIfSet?: boolean;
|
|
15
|
+
avoidLTRByDefault?: boolean;
|
|
16
|
+
}): void;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";AAIA;;;;;;;;GAQG;AACH,yCARW,WAAW,mEACX;IACV,QAAY,CAAC,EAAE,MAAM,CAAC;IACtB,cAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,aAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,iBAAqB,CAAC,EAAE,OAAO,CAAA;CAC5B,QAkFH"}
|