Skip to content

Commit 8842c45

Browse files
Add types
1 parent b0d87a4 commit 8842c45

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

index.d.ts

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
declare namespace i18nextBrowserLanguageDetector {
2+
interface DetectorOptions {
3+
/**
4+
* order and from where user language should be detected
5+
*/
6+
order?: Array<"querystring" | "cookie" | "localStorage" | "navigator" | "htmlTag" | string>;
7+
8+
/**
9+
* keys or params to lookup language from
10+
*/
11+
lookupQuerystring?: string;
12+
lookupCookie?: string;
13+
lookupLocalStorage?: string;
14+
15+
/**
16+
* cache user language on
17+
*/
18+
caches?: string[];
19+
20+
/**
21+
* languages to not persist (cookie, localStorage)
22+
*/
23+
excludeCacheFor?: string[];
24+
25+
/**
26+
* optional expire and domain for set cookie
27+
* @default 10
28+
*/
29+
cookieMinutes?: number;
30+
cookieDomain?: string;
31+
32+
/**
33+
* optional htmlTag with lang attribute
34+
* @default document.documentElement
35+
*/
36+
htmlTag?: HTMLElement | null;
37+
}
38+
39+
interface CustomDetector {
40+
name: string;
41+
cacheUserLanguage?(lng: string, options: DetectorOptions): void;
42+
lookup(options: DetectorOptions): string | undefined;
43+
}
44+
}
45+
46+
declare class i18nextBrowserLanguageDetector {
47+
constructor(services?: any, options?: i18nextBrowserLanguageDetector.DetectorOptions);
48+
/**
49+
* Adds detector.
50+
*/
51+
addDetector(detector: i18nextBrowserLanguageDetector.CustomDetector): i18nextBrowserLanguageDetector;
52+
53+
/**
54+
* Initializes detector.
55+
*/
56+
init(services?: any, options?: i18nextBrowserLanguageDetector.DetectorOptions): void;
57+
58+
detect(detectionOrder?: i18nextBrowserLanguageDetector.DetectorOptions): string | undefined;
59+
60+
cacheUserLanguage(lng: string, caches?: string[]): void;
61+
62+
type: "languageDetector";
63+
detectors: { [key: string]: any };
64+
services: any;
65+
i18nOptions: any;
66+
}
67+
68+
export = i18nextBrowserLanguageDetector;

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "2.2.4",
44
"description": "language detector used in browser environment for i18next",
55
"main": "./index.js",
6+
"types": "./index.d.ts",
67
"jsnext:main": "dist/es/index.js",
78
"keywords": [
89
"i18next",
@@ -23,6 +24,7 @@
2324
"babel-plugin-transform-es2015-classes": "6.22.0",
2425
"babel-preset-es2015": "6.22.0",
2526
"babel-preset-stage-0": "6.22.0",
27+
"dtslint": "^0.4.2",
2628
"eslint": "2.8.0",
2729
"eslint-config-airbnb": "7.0.0",
2830
"mkdirp": "0.5.1",
@@ -31,9 +33,14 @@
3133
"rollup-plugin-babel": "2.4.0",
3234
"rollup-plugin-node-resolve": "1.7.1",
3335
"rollup-plugin-uglify": "0.2.0",
36+
"tslint": "^5.12.1",
37+
"typescript": "^3.3.1",
3438
"yargs": "4.6.0"
3539
},
3640
"scripts": {
41+
"pretest": "npm run test:typescript",
42+
"test": "echo 'TODO'",
43+
"test:typescript": "tslint --project tsconfig.json",
3744
"clean": "rimraf dist && mkdirp dist",
3845
"copy": "cp ./dist/umd/i18nextBrowserLanguageDetector.min.js ./i18nextBrowserLanguageDetector.min.js && cp ./dist/umd/i18nextBrowserLanguageDetector.js ./i18nextBrowserLanguageDetector.js",
3946
"build:es": "BABEL_ENV=jsnext babel src --out-dir dist/es",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as LngDetector from "i18next-browser-languagedetector";
2+
3+
const options: LngDetector.DetectorOptions = {
4+
// order and from where user language should be detected
5+
order: ["querystring", "cookie", "localStorage", "navigator", "htmlTag"],
6+
7+
// keys or params to lookup language from
8+
lookupQuerystring: "lng",
9+
lookupCookie: "i18next",
10+
lookupLocalStorage: "i18nextLng",
11+
12+
// cache user language on
13+
caches: ["localStorage", "cookie"],
14+
excludeCacheFor: ["cimode"], // languages to not persist (cookie, localStorage)
15+
16+
// optional expire and domain for set cookie
17+
cookieMinutes: 10,
18+
cookieDomain: "myDomain",
19+
20+
// optional htmlTag with lang attribute, the default is:
21+
htmlTag: document.documentElement
22+
};
23+
24+
const customDetector: LngDetector.CustomDetector = {
25+
name: "myDetectorsName",
26+
27+
lookup(options: LngDetector.DetectorOptions) {
28+
// options -> are passed in options
29+
return "en";
30+
},
31+
32+
cacheUserLanguage(lng: string, options: LngDetector.DetectorOptions) {
33+
// options -> are passed in options
34+
// lng -> current language, will be called after init and on changeLanguage
35+
36+
// store it
37+
}
38+
};
39+
40+
const customDetector2: LngDetector.CustomDetector = {
41+
name: "myDetectorsName",
42+
lookup(options: LngDetector.DetectorOptions) {
43+
return undefined;
44+
}
45+
};
46+
47+
const lngDetector = new LngDetector(null, options);
48+
49+
lngDetector.init(options);
50+
lngDetector.addDetector(customDetector);

tsconfig.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es5",
5+
"lib": ["es6", "dom"],
6+
"jsx": "react",
7+
"moduleResolution": "node",
8+
"forceConsistentCasingInFileNames": true,
9+
"strict": true,
10+
"noEmit": true,
11+
"baseUrl": ".",
12+
"paths": { "i18next-browser-languagedetector": ["./index.d.ts"] }
13+
},
14+
"include": ["./indext.d.ts", "./test/**/*.ts*"]
15+
}

tslint.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"defaultSeverity": "error",
3+
"extends": "dtslint/dtslint.json",
4+
"rules": {
5+
"semicolon": false
6+
}
7+
}

0 commit comments

Comments
 (0)