|
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 | + */ |
5 | 7 |
|
6 | 8 | const options: DetectorOptions = {
|
7 | 9 | // order and from where user language should be detected
|
8 |
| - order: ["querystring", "cookie", "localStorage", "navigator", "htmlTag"], |
| 10 | + order: ['querystring', 'cookie', 'localStorage', 'navigator', 'htmlTag'], |
9 | 11 |
|
10 | 12 | // 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', |
14 | 16 |
|
15 | 17 | // 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) |
18 | 20 |
|
19 | 21 | // optional expire and domain for set cookie
|
20 | 22 | cookieMinutes: 10,
|
21 |
| - cookieDomain: "myDomain", |
| 23 | + cookieDomain: 'myDomain', |
22 | 24 |
|
23 | 25 | // optional htmlTag with lang attribute, the default is:
|
24 |
| - htmlTag: document.documentElement |
| 26 | + htmlTag: document.documentElement, |
25 | 27 | };
|
26 | 28 |
|
27 | 29 | const customDetector: CustomDetector = {
|
28 |
| - name: "myDetectorsName", |
| 30 | + name: 'myDetectorsName', |
29 | 31 |
|
30 | 32 | lookup(options: DetectorOptions) {
|
31 | 33 | // options -> are passed in options
|
32 |
| - return "en"; |
| 34 | + return 'en'; |
33 | 35 | },
|
34 | 36 |
|
35 | 37 | cacheUserLanguage(lng: string, options: DetectorOptions) {
|
36 | 38 | // options -> are passed in options
|
37 | 39 | // lng -> current language, will be called after init and on changeLanguage
|
38 | 40 | // store it
|
39 |
| - } |
| 41 | + }, |
40 | 42 | };
|
41 | 43 |
|
42 | 44 | const customDetector2: CustomDetector = {
|
43 |
| - name: "myDetectorsName", |
| 45 | + name: 'myDetectorsName', |
44 | 46 | lookup(options: DetectorOptions) {
|
45 | 47 | return undefined;
|
46 |
| - } |
| 48 | + }, |
47 | 49 | };
|
48 | 50 |
|
49 | 51 | const lngDetector = new LngDetector(null, options);
|
50 | 52 |
|
51 | 53 | lngDetector.init(options);
|
52 | 54 | lngDetector.addDetector(customDetector);
|
| 55 | + |
| 56 | +// instance based |
| 57 | +i18next.use(lngDetector).init({}); |
| 58 | + |
| 59 | +// class based |
| 60 | +i18next.use(LngDetector).init({}); |
0 commit comments