UNPKG

2.45 kBTypeScriptView Raw
1import * as i18next from "i18next";
2
3type LoadPathOption =
4 | string
5 | ((lngs: string[], namespaces: string[]) => string);
6
7interface BackendOptions {
8 /**
9 * path where resources get loaded from, or a function
10 * returning a path:
11 * function(lngs, namespaces) { return customPath; }
12 * the returned path will interpolate lng, ns if provided like giving a static path
13 */
14 loadPath?: LoadPathOption;
15 /**
16 * path to post missing resources
17 */
18 addPath?: string;
19 /**
20 * your backend server supports multiLoading
21 * locales/resources.json?lng=de+en&ns=ns1+ns2
22 * set loadPath: '/locales/resources.json?lng={{lng}}&ns={{ns}}' to adapt to multiLoading
23 */
24 allowMultiLoading?: boolean;
25 /**
26 * parse data after it has been fetched
27 * in example use https://www.npmjs.com/package/json5
28 * here it removes the letter a from the json (bad idea)
29 */
30 parse?(data: string): string;
31 /**
32 * allow cross domain requests
33 */
34 crossDomain?: boolean;
35 /**
36 * allow credentials on cross domain requests
37 */
38 withCredentials?: boolean;
39 /**
40 * define a custom xhr function
41 * can be used to support XDomainRequest in IE 8 and 9
42 */
43 ajax?(
44 url: string,
45 options: BackendOptions,
46 callback: AjaxRequestCallback,
47 data: {} | string,
48 cache: boolean
49 ): void;
50 /**
51 * adds parameters to resource URL. 'example.com' -> 'example.com?v=1.3.5'
52 */
53 queryStringParams?: { [key: string]: string };
54
55 /**
56 * @see https://github.com/i18next/i18next-xhr-backend/blob/281c7e235e1157b33122adacef1957252e5700f1/src/ajax.js#L52
57 */
58 customHeaders?: { [key: string]: string };
59}
60
61type AjaxRequestCallback = (response: string, x: XMLHttpRequest) => void;
62
63// type LoadCallback = (error: any, result: string | false) => void;
64
65export default class I18NextXhrBackend
66 implements i18next.BackendModule<BackendOptions> {
67 constructor(services?: any, options?: BackendOptions);
68 init(services?: any, options?: BackendOptions): void;
69 readMulti(
70 languages: string[],
71 namespaces: string[],
72 callback: i18next.ReadCallback
73 ): void;
74 read(
75 language: string,
76 namespace: string,
77 callback: i18next.ReadCallback
78 ): void;
79 loadUrl(url: string, callback: i18next.ReadCallback): void;
80 create(
81 languages: string | string[],
82 namespace: string,
83 key: string,
84 fallbackValue: string
85 ): void;
86 type: "backend";
87 services: any;
88 options: BackendOptions;
89}