Skip to content

Commit 02ccf08

Browse files
committed
tsc test interop vs non-interop
1 parent d0d5cf3 commit 02ccf08

File tree

6 files changed

+169
-18
lines changed

6 files changed

+169
-18
lines changed

.prettierrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"bracketSpacing": true,
3+
"jsxBracketSameLine": false,
4+
"printWidth": 100,
5+
"semi": true,
6+
"singleQuote": true,
7+
"trailingComma": "all"
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import LngDetector, { CustomDetector, DetectorOptions } from 'i18next-browser-languagedetector';
2+
import * as i18next from 'i18next';
3+
4+
/**
5+
* NOTE: only the imports should differ in these `usage*.ts` files
6+
*/
7+
8+
const options: DetectorOptions = {
9+
// order and from where user language should be detected
10+
order: ['querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag'],
11+
12+
// keys or params to lookup language from
13+
lookupQuerystring: 'lng',
14+
lookupCookie: 'i18next',
15+
lookupLocalStorage: 'i18nextLng',
16+
17+
// cache user language on
18+
caches: ['localStorage', 'cookie'],
19+
excludeCacheFor: ['cimode'], // languages to not persist (cookie, localStorage)
20+
21+
// optional expire and domain for set cookie
22+
cookieMinutes: 10,
23+
cookieDomain: 'myDomain',
24+
25+
// optional htmlTag with lang attribute, the default is:
26+
htmlTag: document.documentElement,
27+
};
28+
29+
const customDetector: CustomDetector = {
30+
name: 'myDetectorsName',
31+
32+
lookup(options: DetectorOptions) {
33+
// options -> are passed in options
34+
return 'en';
35+
},
36+
37+
cacheUserLanguage(lng: string, options: DetectorOptions) {
38+
// options -> are passed in options
39+
// lng -> current language, will be called after init and on changeLanguage
40+
// store it
41+
},
42+
};
43+
44+
const customDetector2: CustomDetector = {
45+
name: 'myDetectorsName',
46+
lookup(options: DetectorOptions) {
47+
return undefined;
48+
},
49+
};
50+
51+
const lngDetector = new LngDetector(null, options);
52+
53+
lngDetector.init(options);
54+
lngDetector.addDetector(customDetector);
55+
56+
// instance based
57+
i18next.use(lngDetector).init({});
58+
59+
// class based
60+
i18next.use(LngDetector).init({});
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,60 @@
1-
import LngDetector, {
2-
CustomDetector,
3-
DetectorOptions
4-
} from "i18next-browser-languagedetector";
1+
import LngDetector, { CustomDetector, DetectorOptions } from 'i18next-browser-languagedetector';
2+
import i18next from 'i18next';
3+
4+
/**
5+
* NOTE: only the imports should differ in these `usage*.ts` files
6+
*/
57

68
const options: DetectorOptions = {
79
// order and from where user language should be detected
8-
order: ["querystring", "cookie", "localStorage", "navigator", "htmlTag"],
10+
order: ['querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag'],
911

1012
// keys or params to lookup language from
11-
lookupQuerystring: "lng",
12-
lookupCookie: "i18next",
13-
lookupLocalStorage: "i18nextLng",
13+
lookupQuerystring: 'lng',
14+
lookupCookie: 'i18next',
15+
lookupLocalStorage: 'i18nextLng',
1416

1517
// cache user language on
16-
caches: ["localStorage", "cookie"],
17-
excludeCacheFor: ["cimode"], // languages to not persist (cookie, localStorage)
18+
caches: ['localStorage', 'cookie'],
19+
excludeCacheFor: ['cimode'], // languages to not persist (cookie, localStorage)
1820

1921
// optional expire and domain for set cookie
2022
cookieMinutes: 10,
21-
cookieDomain: "myDomain",
23+
cookieDomain: 'myDomain',
2224

2325
// optional htmlTag with lang attribute, the default is:
24-
htmlTag: document.documentElement
26+
htmlTag: document.documentElement,
2527
};
2628

2729
const customDetector: CustomDetector = {
28-
name: "myDetectorsName",
30+
name: 'myDetectorsName',
2931

3032
lookup(options: DetectorOptions) {
3133
// options -> are passed in options
32-
return "en";
34+
return 'en';
3335
},
3436

3537
cacheUserLanguage(lng: string, options: DetectorOptions) {
3638
// options -> are passed in options
3739
// lng -> current language, will be called after init and on changeLanguage
3840
// store it
39-
}
41+
},
4042
};
4143

4244
const customDetector2: CustomDetector = {
43-
name: "myDetectorsName",
45+
name: 'myDetectorsName',
4446
lookup(options: DetectorOptions) {
4547
return undefined;
46-
}
48+
},
4749
};
4850

4951
const lngDetector = new LngDetector(null, options);
5052

5153
lngDetector.init(options);
5254
lngDetector.addDetector(customDetector);
55+
56+
// instance based
57+
i18next.use(lngDetector).init({});
58+
59+
// class based
60+
i18next.use(LngDetector).init({});

test/typescript/usageLegacy.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import LngDetector, { CustomDetector, DetectorOptions } from 'i18next-browser-languagedetector';
2+
import * as i18next from 'i18next';
3+
4+
/**
5+
* NOTE: only the imports should differ in these `usage*.ts` files
6+
*/
7+
8+
const options: DetectorOptions = {
9+
// order and from where user language should be detected
10+
order: ['querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag'],
11+
12+
// keys or params to lookup language from
13+
lookupQuerystring: 'lng',
14+
lookupCookie: 'i18next',
15+
lookupLocalStorage: 'i18nextLng',
16+
17+
// cache user language on
18+
caches: ['localStorage', 'cookie'],
19+
excludeCacheFor: ['cimode'], // languages to not persist (cookie, localStorage)
20+
21+
// optional expire and domain for set cookie
22+
cookieMinutes: 10,
23+
cookieDomain: 'myDomain',
24+
25+
// optional htmlTag with lang attribute, the default is:
26+
htmlTag: document.documentElement,
27+
};
28+
29+
const customDetector: CustomDetector = {
30+
name: 'myDetectorsName',
31+
32+
lookup(options: DetectorOptions) {
33+
// options -> are passed in options
34+
return 'en';
35+
},
36+
37+
cacheUserLanguage(lng: string, options: DetectorOptions) {
38+
// options -> are passed in options
39+
// lng -> current language, will be called after init and on changeLanguage
40+
// store it
41+
},
42+
};
43+
44+
const customDetector2: CustomDetector = {
45+
name: 'myDetectorsName',
46+
lookup(options: DetectorOptions) {
47+
return undefined;
48+
},
49+
};
50+
51+
const lngDetector = new LngDetector(null, options);
52+
53+
lngDetector.init(options);
54+
lngDetector.addDetector(customDetector);
55+
56+
// instance based
57+
i18next.use(lngDetector).init({});
58+
59+
// class based
60+
i18next.use(LngDetector).init({});

tsconfig.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
"strict": true,
1010
"noEmit": true,
1111
"baseUrl": ".",
12+
13+
"esModuleInterop": true,
14+
"allowSyntheticDefaultImports": true,
15+
1216
"paths": { "i18next-browser-languagedetector": ["./index.d.ts"] }
1317
},
14-
"include": ["./indext.d.ts", "./test/**/*.ts*"]
18+
"include": ["./indext.d.ts", "./test/**/*.ts*"],
19+
"exclude": ["test/typescript/nonEsModuleInterop/**/*.ts"]
1520
}

tsconfig.nonEsModuleInterop.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
// typescript defaults to these
5+
"esModuleInterop": false,
6+
"allowSyntheticDefaultImports": false
7+
},
8+
"include": ["./test/typescript/nonEsModuleInterop/**/*.ts"],
9+
"exclude": []
10+
}

0 commit comments

Comments
 (0)