UNPKG

6.92 kBTypeScriptView Raw
1import { EventEmitter } from "@angular/core";
2import { Http } from "@angular/http";
3import { Observable } from "rxjs/Observable";
4import "rxjs/add/observable/of";
5import "rxjs/add/operator/share";
6import "rxjs/add/operator/map";
7import "rxjs/add/operator/merge";
8import "rxjs/add/operator/toArray";
9import "rxjs/add/operator/take";
10import { TranslateParser } from "./translate.parser";
11export interface TranslationChangeEvent {
12 translations: any;
13 lang: string;
14}
15export interface LangChangeEvent {
16 lang: string;
17 translations: any;
18}
19export interface DefaultLangChangeEvent {
20 lang: string;
21 translations: any;
22}
23export interface MissingTranslationHandlerParams {
24 /**
25 * the key that's missing in translation files
26 *
27 * @type {string}
28 */
29 key: string;
30 /**
31 * an instance of the service that was unable to translate the key.
32 *
33 * @type {TranslateService}
34 */
35 translateService: TranslateService;
36 /**
37 * interpolation params that were passed along for translating the given key.
38 *
39 * @type {Object}
40 */
41 interpolateParams?: Object;
42}
43export declare abstract class MissingTranslationHandler {
44 /**
45 * A function that handles missing translations.
46 *
47 * @abstract
48 * @param {MissingTranslationHandlerParams} params context for resolving a missing translation
49 * @returns {any} a value or an observable
50 * If it returns a value, then this value is used.
51 * If it return an observable, the value returned by this observable will be used (except if the method was "instant").
52 * If it doesn't return then the key will be used as a value
53 */
54 abstract handle(params: MissingTranslationHandlerParams): any;
55}
56export declare abstract class TranslateLoader {
57 abstract getTranslation(lang: string): Observable<any>;
58}
59export declare class TranslateStaticLoader implements TranslateLoader {
60 private http;
61 private prefix;
62 private suffix;
63 constructor(http: Http, prefix?: string, suffix?: string);
64 /**
65 * Gets the translations from the server
66 * @param lang
67 * @returns {any}
68 */
69 getTranslation(lang: string): Observable<any>;
70}
71export declare class TranslateService {
72 currentLoader: TranslateLoader;
73 parser: TranslateParser;
74 private missingTranslationHandler;
75 /**
76 * The lang currently used
77 */
78 currentLang: string;
79 /**
80 * An EventEmitter to listen to translation change events
81 * onTranslationChange.subscribe((params: TranslationChangeEvent) => {
82 * // do something
83 * });
84 * @type {EventEmitter<TranslationChangeEvent>}
85 */
86 onTranslationChange: EventEmitter<TranslationChangeEvent>;
87 /**
88 * An EventEmitter to listen to lang change events
89 * onLangChange.subscribe((params: LangChangeEvent) => {
90 * // do something
91 * });
92 * @type {EventEmitter<LangChangeEvent>}
93 */
94 onLangChange: EventEmitter<LangChangeEvent>;
95 /**
96 * An EventEmitter to listen to default lang change events
97 * onDefaultLangChange.subscribe((params: DefaultLangChangeEvent) => {
98 * // do something
99 * });
100 * @type {EventEmitter<DefaultLangChangeEvent>}
101 */
102 onDefaultLangChange: EventEmitter<DefaultLangChangeEvent>;
103 private pending;
104 private translations;
105 private defaultLang;
106 private langs;
107 /**
108 *
109 * @param currentLoader An instance of the loader currently used
110 * @param parser An instance of the parser currently used
111 * @param missingTranslationHandler A handler for missing translations.
112 */
113 constructor(currentLoader: TranslateLoader, parser: TranslateParser, missingTranslationHandler: MissingTranslationHandler);
114 /**
115 * Sets the default language to use as a fallback
116 * @param lang
117 */
118 setDefaultLang(lang: string): void;
119 /**
120 * Gets the default language used
121 * @returns string
122 */
123 getDefaultLang(): string;
124 /**
125 * Changes the lang currently used
126 * @param lang
127 * @returns {Observable<*>}
128 */
129 use(lang: string): Observable<any>;
130 /**
131 * Retrieves the given translations
132 * @param lang
133 * @returns {Observable<*>}
134 */
135 private retrieveTranslations(lang);
136 /**
137 * Gets an object of translations for a given language with the current loader
138 * @param lang
139 * @returns {Observable<*>}
140 */
141 getTranslation(lang: string): Observable<any>;
142 /**
143 * Manually sets an object of translations for a given language
144 * @param lang
145 * @param translations
146 * @param shouldMerge
147 */
148 setTranslation(lang: string, translations: Object, shouldMerge?: boolean): void;
149 /**
150 * Returns an array of currently available langs
151 * @returns {any}
152 */
153 getLangs(): Array<string>;
154 /**
155 * @param langs
156 * Add available langs
157 */
158 addLangs(langs: Array<string>): void;
159 /**
160 * Update the list of available langs
161 */
162 private updateLangs();
163 /**
164 * Returns the parsed result of the translations
165 * @param translations
166 * @param key
167 * @param interpolateParams
168 * @returns {any}
169 */
170 getParsedResult(translations: any, key: any, interpolateParams?: Object): any;
171 /**
172 * Gets the translated value of a key (or an array of keys)
173 * @param key
174 * @param interpolateParams
175 * @returns {any} the translated key, or an object of translated keys
176 */
177 get(key: string | Array<string>, interpolateParams?: Object): Observable<string | any>;
178 /**
179 * Returns a translation instantly from the internal state of loaded translation.
180 * All rules regarding the current language, the preferred language of even fallback languages will be used except any promise handling.
181 * @param key
182 * @param interpolateParams
183 * @returns {string}
184 */
185 instant(key: string | Array<string>, interpolateParams?: Object): string | any;
186 /**
187 * Sets the translated value of a key
188 * @param key
189 * @param value
190 * @param lang
191 */
192 set(key: string, value: string, lang?: string): void;
193 /**
194 * Changes the current lang
195 * @param lang
196 */
197 private changeLang(lang);
198 /**
199 * Changes the default lang
200 * @param lang
201 */
202 private changeDefaultLang(lang);
203 /**
204 * Allows to reload the lang file from the file
205 * @param lang
206 * @returns {Observable<any>}
207 */
208 reloadLang(lang: string): Observable<any>;
209 /**
210 * Deletes inner translation
211 * @param lang
212 */
213 resetLang(lang: string): void;
214 /**
215 * Returns the language code name from the browser, e.g. "de"
216 *
217 * @returns string
218 */
219 getBrowserLang(): string;
220 /**
221 * Returns the culture language code name from the browser, e.g. "de-DE"
222 *
223 * @returns string
224 */
225 getBrowserCultureLang(): string;
226}