|
19 | 19 | Object.defineProperty(target, descriptor.key, descriptor); |
20 | 20 | } |
21 | 21 | } |
22 | | - |
23 | 22 | function _createClass(Constructor, protoProps, staticProps) { |
24 | 23 | if (protoProps) _defineProperties(Constructor.prototype, protoProps); |
25 | 24 | if (staticProps) _defineProperties(Constructor, staticProps); |
|
45 | 44 |
|
46 | 45 | // eslint-disable-next-line no-control-regex |
47 | 46 | var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/; |
48 | | - |
49 | 47 | var serializeCookie = function serializeCookie(name, val, options) { |
50 | 48 | var opt = options || {}; |
51 | 49 | opt.path = opt.path || '/'; |
52 | 50 | var value = encodeURIComponent(val); |
53 | 51 | var str = "".concat(name, "=").concat(value); |
54 | | - |
55 | 52 | if (opt.maxAge > 0) { |
56 | 53 | var maxAge = opt.maxAge - 0; |
57 | 54 | if (Number.isNaN(maxAge)) throw new Error('maxAge should be a Number'); |
58 | 55 | str += "; Max-Age=".concat(Math.floor(maxAge)); |
59 | 56 | } |
60 | | - |
61 | 57 | if (opt.domain) { |
62 | 58 | if (!fieldContentRegExp.test(opt.domain)) { |
63 | 59 | throw new TypeError('option domain is invalid'); |
64 | 60 | } |
65 | | - |
66 | 61 | str += "; Domain=".concat(opt.domain); |
67 | 62 | } |
68 | | - |
69 | 63 | if (opt.path) { |
70 | 64 | if (!fieldContentRegExp.test(opt.path)) { |
71 | 65 | throw new TypeError('option path is invalid'); |
72 | 66 | } |
73 | | - |
74 | 67 | str += "; Path=".concat(opt.path); |
75 | 68 | } |
76 | | - |
77 | 69 | if (opt.expires) { |
78 | 70 | if (typeof opt.expires.toUTCString !== 'function') { |
79 | 71 | throw new TypeError('option expires is invalid'); |
80 | 72 | } |
81 | | - |
82 | 73 | str += "; Expires=".concat(opt.expires.toUTCString()); |
83 | 74 | } |
84 | | - |
85 | 75 | if (opt.httpOnly) str += '; HttpOnly'; |
86 | 76 | if (opt.secure) str += '; Secure'; |
87 | | - |
88 | 77 | if (opt.sameSite) { |
89 | 78 | var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite; |
90 | | - |
91 | 79 | switch (sameSite) { |
92 | 80 | case true: |
93 | 81 | str += '; SameSite=Strict'; |
94 | 82 | break; |
95 | | - |
96 | 83 | case 'lax': |
97 | 84 | str += '; SameSite=Lax'; |
98 | 85 | break; |
99 | | - |
100 | 86 | case 'strict': |
101 | 87 | str += '; SameSite=Strict'; |
102 | 88 | break; |
103 | | - |
104 | 89 | case 'none': |
105 | 90 | str += '; SameSite=None'; |
106 | 91 | break; |
107 | | - |
108 | 92 | default: |
109 | 93 | throw new TypeError('option sameSite is invalid'); |
110 | 94 | } |
111 | 95 | } |
112 | | - |
113 | 96 | return str; |
114 | 97 | }; |
115 | | - |
116 | 98 | var cookie = { |
117 | 99 | create: function create(name, value, minutes, domain) { |
118 | 100 | var cookieOptions = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { |
119 | 101 | path: '/', |
120 | 102 | sameSite: 'strict' |
121 | 103 | }; |
122 | | - |
123 | 104 | if (minutes) { |
124 | 105 | cookieOptions.expires = new Date(); |
125 | 106 | cookieOptions.expires.setTime(cookieOptions.expires.getTime() + minutes * 60 * 1000); |
126 | 107 | } |
127 | | - |
128 | 108 | if (domain) cookieOptions.domain = domain; |
129 | 109 | document.cookie = serializeCookie(name, encodeURIComponent(value), cookieOptions); |
130 | 110 | }, |
131 | 111 | read: function read(name) { |
132 | 112 | var nameEQ = "".concat(name, "="); |
133 | 113 | var ca = document.cookie.split(';'); |
134 | | - |
135 | 114 | for (var i = 0; i < ca.length; i++) { |
136 | 115 | var c = ca[i]; |
137 | | - |
138 | 116 | while (c.charAt(0) === ' ') { |
139 | 117 | c = c.substring(1, c.length); |
140 | 118 | } |
141 | | - |
142 | 119 | if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length); |
143 | 120 | } |
144 | | - |
145 | 121 | return null; |
146 | 122 | }, |
147 | 123 | remove: function remove(name) { |
|
152 | 128 | name: 'cookie', |
153 | 129 | lookup: function lookup(options) { |
154 | 130 | var found; |
155 | | - |
156 | 131 | if (options.lookupCookie && typeof document !== 'undefined') { |
157 | 132 | var c = cookie.read(options.lookupCookie); |
158 | 133 | if (c) found = c; |
159 | 134 | } |
160 | | - |
161 | 135 | return found; |
162 | 136 | }, |
163 | 137 | cacheUserLanguage: function cacheUserLanguage(lng, options) { |
|
171 | 145 | name: 'querystring', |
172 | 146 | lookup: function lookup(options) { |
173 | 147 | var found; |
174 | | - |
175 | 148 | if (typeof window !== 'undefined') { |
176 | 149 | var search = window.location.search; |
177 | | - |
178 | 150 | if (!window.location.search && window.location.hash && window.location.hash.indexOf('?') > -1) { |
179 | 151 | search = window.location.hash.substring(window.location.hash.indexOf('?')); |
180 | 152 | } |
181 | | - |
182 | 153 | var query = search.substring(1); |
183 | 154 | var params = query.split('&'); |
184 | | - |
185 | 155 | for (var i = 0; i < params.length; i++) { |
186 | 156 | var pos = params[i].indexOf('='); |
187 | | - |
188 | 157 | if (pos > 0) { |
189 | 158 | var key = params[i].substring(0, pos); |
190 | | - |
191 | 159 | if (key === options.lookupQuerystring) { |
192 | 160 | found = params[i].substring(pos + 1); |
193 | 161 | } |
194 | 162 | } |
195 | 163 | } |
196 | 164 | } |
197 | | - |
198 | 165 | return found; |
199 | 166 | } |
200 | 167 | }; |
201 | 168 |
|
202 | 169 | var hasLocalStorageSupport = null; |
203 | | - |
204 | 170 | var localStorageAvailable = function localStorageAvailable() { |
205 | 171 | if (hasLocalStorageSupport !== null) return hasLocalStorageSupport; |
206 | | - |
207 | 172 | try { |
208 | 173 | hasLocalStorageSupport = window !== 'undefined' && window.localStorage !== null; |
209 | 174 | var testKey = 'i18next.translate.boo'; |
|
212 | 177 | } catch (e) { |
213 | 178 | hasLocalStorageSupport = false; |
214 | 179 | } |
215 | | - |
216 | 180 | return hasLocalStorageSupport; |
217 | 181 | }; |
218 | | - |
219 | 182 | var localStorage = { |
220 | 183 | name: 'localStorage', |
221 | 184 | lookup: function lookup(options) { |
222 | 185 | var found; |
223 | | - |
224 | 186 | if (options.lookupLocalStorage && localStorageAvailable()) { |
225 | 187 | var lng = window.localStorage.getItem(options.lookupLocalStorage); |
226 | 188 | if (lng) found = lng; |
227 | 189 | } |
228 | | - |
229 | 190 | return found; |
230 | 191 | }, |
231 | 192 | cacheUserLanguage: function cacheUserLanguage(lng, options) { |
|
236 | 197 | }; |
237 | 198 |
|
238 | 199 | var hasSessionStorageSupport = null; |
239 | | - |
240 | 200 | var sessionStorageAvailable = function sessionStorageAvailable() { |
241 | 201 | if (hasSessionStorageSupport !== null) return hasSessionStorageSupport; |
242 | | - |
243 | 202 | try { |
244 | 203 | hasSessionStorageSupport = window !== 'undefined' && window.sessionStorage !== null; |
245 | 204 | var testKey = 'i18next.translate.boo'; |
|
248 | 207 | } catch (e) { |
249 | 208 | hasSessionStorageSupport = false; |
250 | 209 | } |
251 | | - |
252 | 210 | return hasSessionStorageSupport; |
253 | 211 | }; |
254 | | - |
255 | 212 | var sessionStorage = { |
256 | 213 | name: 'sessionStorage', |
257 | 214 | lookup: function lookup(options) { |
258 | 215 | var found; |
259 | | - |
260 | 216 | if (options.lookupSessionStorage && sessionStorageAvailable()) { |
261 | 217 | var lng = window.sessionStorage.getItem(options.lookupSessionStorage); |
262 | 218 | if (lng) found = lng; |
263 | 219 | } |
264 | | - |
265 | 220 | return found; |
266 | 221 | }, |
267 | 222 | cacheUserLanguage: function cacheUserLanguage(lng, options) { |
|
275 | 230 | name: 'navigator', |
276 | 231 | lookup: function lookup(options) { |
277 | 232 | var found = []; |
278 | | - |
279 | 233 | if (typeof navigator !== 'undefined') { |
280 | 234 | if (navigator.languages) { |
281 | 235 | // chrome only; not an array, so can't use .push.apply instead of iterating |
282 | 236 | for (var i = 0; i < navigator.languages.length; i++) { |
283 | 237 | found.push(navigator.languages[i]); |
284 | 238 | } |
285 | 239 | } |
286 | | - |
287 | 240 | if (navigator.userLanguage) { |
288 | 241 | found.push(navigator.userLanguage); |
289 | 242 | } |
290 | | - |
291 | 243 | if (navigator.language) { |
292 | 244 | found.push(navigator.language); |
293 | 245 | } |
294 | 246 | } |
295 | | - |
296 | 247 | return found.length > 0 ? found : undefined; |
297 | 248 | } |
298 | 249 | }; |
|
302 | 253 | lookup: function lookup(options) { |
303 | 254 | var found; |
304 | 255 | var htmlTag = options.htmlTag || (typeof document !== 'undefined' ? document.documentElement : null); |
305 | | - |
306 | 256 | if (htmlTag && typeof htmlTag.getAttribute === 'function') { |
307 | 257 | found = htmlTag.getAttribute('lang'); |
308 | 258 | } |
309 | | - |
310 | 259 | return found; |
311 | 260 | } |
312 | 261 | }; |
|
315 | 264 | name: 'path', |
316 | 265 | lookup: function lookup(options) { |
317 | 266 | var found; |
318 | | - |
319 | 267 | if (typeof window !== 'undefined') { |
320 | 268 | var language = window.location.pathname.match(/\/([a-zA-Z-]*)/g); |
321 | | - |
322 | 269 | if (language instanceof Array) { |
323 | 270 | if (typeof options.lookupFromPathIndex === 'number') { |
324 | 271 | if (typeof language[options.lookupFromPathIndex] !== 'string') { |
325 | 272 | return undefined; |
326 | 273 | } |
327 | | - |
328 | 274 | found = language[options.lookupFromPathIndex].replace('/', ''); |
329 | 275 | } else { |
330 | 276 | found = language[0].replace('/', ''); |
331 | 277 | } |
332 | 278 | } |
333 | 279 | } |
334 | | - |
335 | 280 | return found; |
336 | 281 | } |
337 | 282 | }; |
|
340 | 285 | name: 'subdomain', |
341 | 286 | lookup: function lookup(options) { |
342 | 287 | // If given get the subdomain index else 1 |
343 | | - var lookupFromSubdomainIndex = typeof options.lookupFromSubdomainIndex === 'number' ? options.lookupFromSubdomainIndex + 1 : 1; // get all matches if window.location. is existing |
| 288 | + var lookupFromSubdomainIndex = typeof options.lookupFromSubdomainIndex === 'number' ? options.lookupFromSubdomainIndex + 1 : 1; |
| 289 | + // get all matches if window.location. is existing |
344 | 290 | // first item of match is the match itself and the second is the first group macht which sould be the first subdomain match |
345 | 291 | // is the hostname no public domain get the or option of localhost |
| 292 | + var language = typeof window !== 'undefined' && window.location && window.location.hostname && window.location.hostname.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i); |
346 | 293 |
|
347 | | - var language = typeof window !== 'undefined' && window.location && window.location.hostname && window.location.hostname.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i); // if there is no match (null) return undefined |
348 | | - |
349 | | - if (!language) return undefined; // return the given group match |
350 | | - |
| 294 | + // if there is no match (null) return undefined |
| 295 | + if (!language) return undefined; |
| 296 | + // return the given group match |
351 | 297 | return language[lookupFromSubdomainIndex]; |
352 | 298 | } |
353 | 299 | }; |
|
361 | 307 | lookupSessionStorage: 'i18nextLng', |
362 | 308 | // cache user language |
363 | 309 | caches: ['localStorage'], |
364 | | - excludeCacheFor: ['cimode'] // cookieMinutes: 10, |
| 310 | + excludeCacheFor: ['cimode'] |
| 311 | + // cookieMinutes: 10, |
365 | 312 | // cookieDomain: 'myDomain' |
366 | | - |
367 | 313 | }; |
368 | 314 | } |
369 | | - |
370 | 315 | var Browser = /*#__PURE__*/function () { |
371 | 316 | function Browser(services) { |
372 | 317 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; |
373 | | - |
374 | 318 | _classCallCheck(this, Browser); |
375 | | - |
376 | 319 | this.type = 'languageDetector'; |
377 | 320 | this.detectors = {}; |
378 | 321 | this.init(services, options); |
379 | 322 | } |
380 | | - |
381 | 323 | _createClass(Browser, [{ |
382 | 324 | key: "init", |
383 | 325 | value: function init(services) { |
384 | 326 | var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; |
385 | 327 | var i18nOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; |
386 | 328 | this.services = services; |
387 | | - this.options = defaults(options, this.options || {}, getDefaults()); // backwards compatibility |
| 329 | + this.options = defaults(options, this.options || {}, getDefaults()); |
388 | 330 |
|
| 331 | + // backwards compatibility |
389 | 332 | if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex; |
390 | 333 | this.i18nOptions = i18nOptions; |
391 | 334 | this.addDetector(cookie$1); |
|
406 | 349 | key: "detect", |
407 | 350 | value: function detect(detectionOrder) { |
408 | 351 | var _this = this; |
409 | | - |
410 | 352 | if (!detectionOrder) detectionOrder = this.options.order; |
411 | 353 | var detected = []; |
412 | 354 | detectionOrder.forEach(function (detectorName) { |
413 | 355 | if (_this.detectors[detectorName]) { |
414 | 356 | var lookup = _this.detectors[detectorName].lookup(_this.options); |
415 | | - |
416 | 357 | if (lookup && typeof lookup === 'string') lookup = [lookup]; |
417 | 358 | if (lookup) detected = detected.concat(lookup); |
418 | 359 | } |
419 | 360 | }); |
420 | 361 | if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0 |
421 | | - |
422 | 362 | return detected.length > 0 ? detected[0] : null; // a little backward compatibility |
423 | 363 | } |
424 | 364 | }, { |
425 | 365 | key: "cacheUserLanguage", |
426 | 366 | value: function cacheUserLanguage(lng, caches) { |
427 | 367 | var _this2 = this; |
428 | | - |
429 | 368 | if (!caches) caches = this.options.caches; |
430 | 369 | if (!caches) return; |
431 | 370 | if (this.options.excludeCacheFor && this.options.excludeCacheFor.indexOf(lng) > -1) return; |
|
434 | 373 | }); |
435 | 374 | } |
436 | 375 | }]); |
437 | | - |
438 | 376 | return Browser; |
439 | 377 | }(); |
440 | | - |
441 | 378 | Browser.type = 'languageDetector'; |
442 | 379 |
|
443 | 380 | return Browser; |
|
0 commit comments