Skip to content

Commit 8e05829

Browse files
committed
introduce convertDetectedLanguage option to address #273
1 parent c15f58d commit 8e05829

11 files changed

+178
-42
lines changed

.eslintrc

+2
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ globals:
2626
window: false
2727
navigator: false
2828
document: false
29+
describe: false
30+
it: false

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 7.0.3
2+
3+
- introduce convertDetectedLanguage option
4+
15
### 7.0.2
26

37
- simplify usage without i18next

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2022 i18next
3+
Copyright (c) 2023 i18next
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ As with all modules you can either pass the constructor function (class) to the
6666
htmlTag: document.documentElement,
6767

6868
// optional set cookie options, reference:[MDN Set-Cookie docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie)
69-
cookieOptions: { path: '/', sameSite: 'strict' }
69+
cookieOptions: { path: '/', sameSite: 'strict' },
70+
71+
// optional conversion function to use to modify the detected language code
72+
convertDetectedLanguage: 'Iso15897',
73+
convertDetectedLanguage: (lng) => lng.replace('-', '_')
7074
}
7175
```
7276

i18nextBrowserLanguageDetector.js

+40-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,39 @@
1010
}
1111
}
1212

13+
function _typeof(obj) {
14+
"@babel/helpers - typeof";
15+
16+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
17+
return typeof obj;
18+
} : function (obj) {
19+
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
20+
}, _typeof(obj);
21+
}
22+
23+
function _toPrimitive(input, hint) {
24+
if (_typeof(input) !== "object" || input === null) return input;
25+
var prim = input[Symbol.toPrimitive];
26+
if (prim !== undefined) {
27+
var res = prim.call(input, hint || "default");
28+
if (_typeof(res) !== "object") return res;
29+
throw new TypeError("@@toPrimitive must return a primitive value.");
30+
}
31+
return (hint === "string" ? String : Number)(input);
32+
}
33+
34+
function _toPropertyKey(arg) {
35+
var key = _toPrimitive(arg, "string");
36+
return _typeof(key) === "symbol" ? key : String(key);
37+
}
38+
1339
function _defineProperties(target, props) {
1440
for (var i = 0; i < props.length; i++) {
1541
var descriptor = props[i];
1642
descriptor.enumerable = descriptor.enumerable || false;
1743
descriptor.configurable = true;
1844
if ("value" in descriptor) descriptor.writable = true;
19-
Object.defineProperty(target, descriptor.key, descriptor);
45+
Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
2046
}
2147
}
2248
function _createClass(Constructor, protoProps, staticProps) {
@@ -307,9 +333,13 @@
307333
lookupSessionStorage: 'i18nextLng',
308334
// cache user language
309335
caches: ['localStorage'],
310-
excludeCacheFor: ['cimode']
336+
excludeCacheFor: ['cimode'],
311337
// cookieMinutes: 10,
312338
// cookieDomain: 'myDomain'
339+
340+
convertDetectedLanguage: function convertDetectedLanguage(l) {
341+
return l;
342+
}
313343
};
314344
}
315345
var Browser = /*#__PURE__*/function () {
@@ -329,6 +359,11 @@
329359
languageUtils: {}
330360
}; // this way the language detector can be used without i18next
331361
this.options = defaults(options, this.options || {}, getDefaults());
362+
if (typeof this.options.convertDetectedLanguage === 'string' && this.options.convertDetectedLanguage.indexOf('15897') > -1) {
363+
this.options.convertDetectedLanguage = function (l) {
364+
return l.replace('-', '_');
365+
};
366+
}
332367

333368
// backwards compatibility
334369
if (this.options.lookupFromUrlIndex) this.options.lookupFromPathIndex = this.options.lookupFromUrlIndex;
@@ -360,6 +395,9 @@
360395
if (lookup) detected = detected.concat(lookup);
361396
}
362397
});
398+
detected = detected.map(function (d) {
399+
return _this.options.convertDetectedLanguage(d);
400+
});
363401
if (this.services.languageUtils.getBestMatchFromCodes) return detected; // new i18next v19.5.0
364402
return detected.length > 0 ? detected[0] : null; // a little backward compatibility
365403
}

i18nextBrowserLanguageDetector.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ export interface DetectorOptions {
5959
* @default document.documentElement
6060
*/
6161
htmlTag?: HTMLElement | null;
62+
63+
/**
64+
* optional conversion function to use to modify the detected language code
65+
*/
66+
convertDetectedLanguage?: 'Iso15897' | ((lng: string) => string);
6267
}
6368

6469
export interface CustomDetector {

0 commit comments

Comments
 (0)