change interface of i18next as v8.4.2

This commit is contained in:
Alen
2017-06-27 17:06:15 +08:00
committed by Alen Liang
parent ec7dd9ed30
commit bcfc7d0934
2 changed files with 53 additions and 38 deletions

View File

@@ -6,13 +6,14 @@ i18n.init({
en: {
translation: {
helloWorld: 'Hello, world!',
helloWorldInterpolated: 'Hello, {{name}}!'
helloWorldInterpolated: 'Hello, {{name}}!',
uppercaseFormatted: '{{text^uppercase}} just uppercased',
}
},
ru: {
translation: {
helloWorld: 'Привет, мир!',
helloWorldInterpolated: 'Привет, {{name}}!'
helloWorldInterpolated: 'Привет, {{name}}!',
}
}
},
@@ -31,23 +32,29 @@ i18n.init({
contextSeparator: '_',
saveMissing: true,
saveMissingTo: 'all',
missingKeyHandler: (lng:string, ns:string, key:string, fallbackValue:string) => {
missingKeyHandler: (lng: string, ns: string, key: string, fallbackValue: string) => {
console.log('Lng: ' + lng + ', ns: ' + ns + ', key' + key + ', fallbackValue: ' + fallbackValue);
},
parseMissingKeyHandler: (key:string) => {
parseMissingKeyHandler: (key: string) => {
console.log(key);
},
appendNamespaceToMissingKey: true,
returnNull: false,
returnEmptyString: false,
returnObjects: false,
returnedObjectHandler: (key:string, value:string, options:any) => {
returnedObjectHandler: (key: string, value: string, options: any) => {
},
joinArrays: '\n',
overloadTranslationOptionHandler: (args:any[]) => {
overloadTranslationOptionHandler: (args: any[]) => {
return <i18n.TranslationOptions>{}
},
interpolation: <i18n.InterpolationOptions>{},
interpolation: {
format: function(value, format, lng) {
if (format === 'uppercase') return value.toUpperCase();
return value;
},
formatSeparator: '^',
},
detection: null,
backend: null,
cache: null,
@@ -77,6 +84,10 @@ i18n.t('helloWorldInterpolated', <i18n.TranslationOptions> {
name: "world"
});
i18n.t('uppercaseFormatted', <i18n.TranslationOptions> {
text: 'can you hear me',
});
i18n.t('helloSingleFallbackLng', <i18n.TranslationOptions> {
fallbackLng: 'en'
});

View File

@@ -1,11 +1,10 @@
// Type definitions for i18next v2.3.5
// Type definitions for i18next v8.4.2
// Project: http://i18next.com
// Definitions by: Michael Ledin <https://github.com/mxl>, Budi Irawan <https://github.com/deerawan>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Sources: https://github.com/i18next/i18next/
declare namespace i18n {
interface ResourceStore {
[language: string]: ResourceStoreLanguage;
@@ -25,7 +24,12 @@ declare namespace i18n {
type FallbackLng = string | string[] | FallbackLngObjList;
type FormatFunction = (value: any, format: string, lng: string) => string;
interface InterpolationOptions {
format?: FormatFunction;
formatSeparator?: string;
escape?: (str: string) => string;
escapeValue?: boolean;
prefix?: string;
suffix?: string;
@@ -99,40 +103,28 @@ declare namespace i18n {
}
type TranslationFunction = (key: string, options?: TranslationOptions) => string;
type LoadCallback = (error: any, t: TranslationFunction) => void;
interface I18n {
//constructor(options?: Options, callback?: (err: any, t: TranslationFunction) => void);
init(options?: Options&ReactOptions, callback?: (err: any, t: TranslationFunction) => void): I18n;
loadResources(callback?: (err: any) => void): void;
language: string;
languages: string[];
// api
init(options?: Options & ReactOptions, callback?: LoadCallback): I18n;
use(module: any): I18n;
changeLanguage(lng: string, callback?: (err: any, t: TranslationFunction) => void): void;
getFixedT(lng?: string, ns?: string | string[]): TranslationFunction;
t(key: string, options?: TranslationOptions): string | any | Array<any>;
t(key: string, options?: TranslationOptions): string | any | any[];
exists(key: string, options?: TranslationOptions): boolean;
getFixedT(lng?: string, ns?: string | string[]): TranslationFunction;
changeLanguage(lng: string, callback?: LoadCallback): void;
language: string;
languages: string[];
loadNamespaces(ns: string[], callback?: LoadCallback): void;
loadLanguages(lngs: string[], callback?: LoadCallback): void;
reloadResources(lng?: string[], ns?: string[]): void;
setDefaultNamespace(ns: string): void;
loadNamespaces(ns: string[], callback?: () => void): void;
loadLanguages(lngs: string[], callback?: () => void): void;
dir(lng?: string): string;
createInstance(options?: Options, callback?: (err: any, t: TranslationFunction) => void): I18n;
cloneInstance(options?: Options, callback?: (err: any, t: TranslationFunction) => void): I18n;
format: FormatFunction; // introduced in v8.4.0;
// instance creation
createInstance(options?: Options, callback?: LoadCallback): I18n;
cloneInstance(options?: Options, callback?: LoadCallback): I18n;
// events
on(event: string, listener: () => void): void;
on(initialized: 'initialized', listener: (options: i18n.Options) => void): void;
on(loaded: 'loaded', listener: (loaded: any) => void): void;
@@ -141,8 +133,20 @@ declare namespace i18n {
on(added: 'added', listener: (lng: string, ns: string) => void): void;
on(removed: 'removed', listener: (lng: string, ns: string) => void): void;
on(languageChanged: 'languageChanged', listener: (lng: string) => void): void;
off(event: string, listener: () => void): void;
// resource handling
getResource(lng: string, ns: string, key: string, options?: {
keySeparator?: string,
}): ResourceStore;
addResource(lng: string, ns: string, key: string, value: string, options?: {
keySeparator?: string,
silent?: boolean,
}): void;
addResources(lng: string, ns: string, resources: ResourceStore): void;
addResourceBundle(lng: string, ns: string, resources: ResourceStore, deep: boolean, overwrite: boolean): void;
hasResourceBundle(lng: string, ns: string): boolean;
getResourceBundle(lng: string, ns: string): ResourceStore;
removeResourceBundle(lng: string, ns: string): void;
options: Options;
}