Merge pull request #17705 from GiedriusGrabauskas/i18next-browser-languagedetector

Updated I18next-browser-languagedetector to @2.0
This commit is contained in:
Ron Buckton
2017-07-06 16:21:58 -07:00
committed by GitHub
8 changed files with 255 additions and 94 deletions

View File

@@ -1,31 +1,40 @@
import * as i18next from 'i18next';
import LngDetector from 'i18next-browser-languagedetector';
import * as i18next from "i18next";
import * as LngDetector from "i18next-browser-languagedetector";
var options = {
const options: LngDetector.DetectorOptions = {
// order and from where user language should be detected
order: ['querystring', 'cookie', 'localStorage', 'navigator'],
order: ["querystring", "cookie", "localStorage", "navigator", "htmlTag"],
// keys or params to lookup language from
lookupQuerystring: 'lng',
lookupCookie: 'i18next',
lookupLocalStorage: 'i18nextLng',
lookupQuerystring: "lng",
lookupCookie: "i18next",
lookupLocalStorage: "i18nextLng",
// cache user language on
caches: ['localStorage', 'cookie'],
caches: ["localStorage", "cookie"],
excludeCacheFor: ["cimode"], // languages to not persist (cookie, localStorage)
// optional expire and domain for set cookie
cookieMinutes: 10,
cookieDomain: 'myDomain'
};
var myDetector = {
name: 'myDetectorsName',
cookieDomain: "myDomain",
lookup(options: Object) {
// optional htmlTag with lang attribute, the default is:
htmlTag: document.documentElement
};
i18next.use(LngDetector).init({
detection: options
});
const customDetector: LngDetector.CustomDetector = {
name: "myDetectorsName",
lookup(options: LngDetector.DetectorOptions) {
// options -> are passed in options
return 'en';
return "en";
},
cacheUserLanguage(lng: string, options: Object) {
cacheUserLanguage(lng: string, options: LngDetector.DetectorOptions) {
// options -> are passed in options
// lng -> current language, will be called after init and on changeLanguage
@@ -33,10 +42,20 @@ var myDetector = {
}
};
i18next.use(LngDetector).init({
detection: options
});
const customDetector2: LngDetector.CustomDetector = {
name: "myDetectorsName",
lookup(options: LngDetector.DetectorOptions) {
return undefined;
}
};
const lngDetector = new LngDetector(null, options);
lngDetector.init(options);
lngDetector.addDetector(myDetector);
lngDetector.addDetector(customDetector);
i18next
.use(lngDetector)
.init({
detection: options
});

View File

@@ -1,86 +1,64 @@
// Type definitions for i18next-browser-languagedetector 0.0.14
// Type definitions for i18next-browser-languagedetector 2.0
// Project: http://i18next.com/
// Definitions by: Cyril Schumacher <https://github.com/cyrilschumacher>
// Definitions by: Cyril Schumacher <https://github.com/cyrilschumacher>, Giedrius Grabauskas <https://github.com/GiedriusGrabauskas>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="i18next"/>
/// <reference types="express" />
declare namespace I18next {
interface I18nextStatic extends i18nextBrowserLanguageDetector.I18nextStatic { }
interface I18nextOptions extends i18nextBrowserLanguageDetector.I18nextOptions { }
}
declare namespace i18nextBrowserLanguageDetector {
/**
* @summary Interface for Language detector options.
* @interface
*/
interface LanguageDetectorOptions {
caches?: Array<string>|boolean;
cookieDomain?: string;
cookieExpirationDate?: Date;
lookupCookie?: string;
lookupFromPathIndex?: number;
interface DetectorOptions {
/**
* order and from where user language should be detected
*/
order?: Array<"querystring" | "cookie" | "localStorage" | "navigator" | "htmlTag" | string>;
/**
* keys or params to lookup language from
*/
lookupQuerystring?: string;
lookupSession?: string;
order?: Array<string>;
lookupCookie?: string;
lookupLocalStorage?: string;
/**
* cache user language on
*/
caches?: string[];
/**
* languages to not persist (cookie, localStorage)
*/
excludeCacheFor?: string[];
/**
* optional expire and domain for set cookie
* @default 10
*/
cookieMinutes?: number;
cookieDomain?: string;
/**
* optional htmlTag with lang attribute
* @default document.documentElement
*/
htmlTag?: HTMLElement;
}
/**
* @summary Interface for custom detector.
* @interface
*/
interface CustomDetector {
name: string;
//todo: Checks paramters type.
cacheUserLanguage: (lng: string, options: Object) => void;
lookup: (options: Object) => string;
}
/**
* @summary i18next options.
* @interface
*/
interface I18nextOptions {
detection?: LanguageDetectorOptions;
}
/**
* @summary i18next interface.
* @interface
*/
interface I18nextStatic {
use(module: LngDetector): I18nextStatic;
}
/**
* @summary i18next language detection.
* @class
*/
class LngDetector {
/**
* @summary Constructor.
* @constructor
*/
constructor(services?: any, options?: LanguageDetectorOptions);
/**
* @summary Adds detector.
* @param {CustomDetector} detector The custom detector.
*/
addDetector(detector: CustomDetector): LngDetector;
/**
* @summary Initializes detector.
* @param {LanguageDetectorOptions} options The options.
*/
init(options?: LanguageDetectorOptions): void;
cacheUserLanguage?(lng: string, options: DetectorOptions): void;
lookup(options: DetectorOptions): string | undefined;
}
}
declare module "i18next-browser-languagedetector" {
declare class i18nextBrowserLanguageDetector {
constructor(services?: any, options?: i18nextBrowserLanguageDetector.DetectorOptions);
/**
* Adds detector.
*/
addDetector(detector: i18nextBrowserLanguageDetector.CustomDetector): i18nextBrowserLanguageDetector;
export default i18nextBrowserLanguageDetector.LngDetector;
/**
* Initializes detector.
*/
init(options?: i18nextBrowserLanguageDetector.DetectorOptions): void;
}
export = i18nextBrowserLanguageDetector;

View File

@@ -2,11 +2,12 @@
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
@@ -19,4 +20,4 @@
"index.d.ts",
"i18next-browser-languagedetector-tests.ts"
]
}
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@@ -0,0 +1,42 @@
import * as i18next from 'i18next';
import LngDetector from 'i18next-browser-languagedetector';
const options = {
// order and from where user language should be detected
order: ['querystring', 'cookie', 'localStorage', 'navigator'],
// keys or params to lookup language from
lookupQuerystring: 'lng',
lookupCookie: 'i18next',
lookupLocalStorage: 'i18nextLng',
// cache user language on
caches: ['localStorage', 'cookie'],
// optional expire and domain for set cookie
cookieMinutes: 10,
cookieDomain: 'myDomain'
};
const myDetector = {
name: 'myDetectorsName',
lookup(options: {}) {
// options -> are passed in options
return 'en';
},
cacheUserLanguage(lng: string, options: {}) {
// options -> are passed in options
// lng -> current language, will be called after init and on changeLanguage
// store it
}
};
i18next.use(LngDetector).init({
detection: options
});
const lngDetector = new LngDetector(null, options);
lngDetector.init(options);
lngDetector.addDetector(myDetector);

View File

@@ -0,0 +1,82 @@
// Type definitions for i18next-browser-languagedetector 0.0
// Project: http://i18next.com/
// Definitions by: Cyril Schumacher <https://github.com/cyrilschumacher>, Giedrius Grabauskas <https://github.com/GiedriusGrabauskas>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import * as i18next from "i18next";
declare namespace I18next {
interface I18nextStatic extends i18nextBrowserLanguageDetector.I18nextStatic { }
interface I18nextOptions extends i18nextBrowserLanguageDetector.I18nextOptions { }
}
declare namespace i18nextBrowserLanguageDetector {
/**
* @summary Interface for Language detector options.
* @interface
*/
interface LanguageDetectorOptions {
caches?: string[] | boolean;
cookieDomain?: string;
cookieExpirationDate?: Date;
lookupCookie?: string;
lookupFromPathIndex?: number;
lookupQuerystring?: string;
lookupSession?: string;
order?: string[];
}
/**
* @summary Interface for custom detector.
* @interface
*/
interface CustomDetector {
name: string;
// todo: Checks paramters type.
cacheUserLanguage(lng: string, options: {}): void;
lookup(options: {}): string;
}
/**
* @summary i18next options.
* @interface
*/
interface I18nextOptions {
detection?: LanguageDetectorOptions;
}
/**
* @summary i18next interface.
* @interface
*/
interface I18nextStatic {
use(module: LngDetector): I18nextStatic;
}
/**
* @summary i18next language detection.
* @class
*/
class LngDetector {
/**
* @summary Constructor.
* @constructor
*/
constructor(services?: any, options?: LanguageDetectorOptions);
/**
* @summary Adds detector.
* @param {CustomDetector} detector The custom detector.
*/
addDetector(detector: CustomDetector): LngDetector;
/**
* @summary Initializes detector.
* @param {LanguageDetectorOptions} options The options.
*/
init(options?: LanguageDetectorOptions): void;
}
}
export default i18nextBrowserLanguageDetector.LngDetector;

View File

@@ -0,0 +1,27 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../../",
"typeRoots": [
"../../"
],
"paths": {
"i18next-browser-languagedetector": [
"i18next-browser-languagedetector/v0"
]
},
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"i18next-browser-languagedetector-tests.ts"
]
}

View File

@@ -0,0 +1,11 @@
{
"extends": "dtslint/dt.json",
"rules": {
"interface-name": [
false
],
"no-empty-interface": [
false
]
}
}