2019-07-26 11:46:59 +02:00
|
|
|
import { Inject, Injectable } from '@angular/core';
|
|
|
|
|
2018-11-08 15:34:43 +01:00
|
|
|
import {
|
2019-07-26 11:46:59 +02:00
|
|
|
MissingTranslationHandler,
|
2018-11-08 15:34:43 +01:00
|
|
|
TranslateCompiler,
|
2019-07-26 11:46:59 +02:00
|
|
|
TranslateLoader,
|
2018-11-08 15:34:43 +01:00
|
|
|
TranslateParser,
|
2019-07-26 11:46:59 +02:00
|
|
|
TranslateService,
|
|
|
|
TranslateStore,
|
2018-11-08 15:34:43 +01:00
|
|
|
USE_DEFAULT_LANG,
|
|
|
|
USE_STORE
|
|
|
|
} from '@ngx-translate/core';
|
|
|
|
import { Observable, of } from 'rxjs';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom translate service. Wraps the get, stream and instant method not to throw an error, if null or undefined
|
|
|
|
* is passed as keys to them. This happens, if yet not resolved properties should be translated in the templates.
|
|
|
|
* Returns empty strings instead.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class OpenSlidesTranslateService extends TranslateService {
|
|
|
|
/**
|
|
|
|
* See the ngx-translate TranslateService for docs.
|
|
|
|
*
|
|
|
|
* @param store
|
|
|
|
* @param currentLoader
|
|
|
|
* @param compiler
|
|
|
|
* @param parser
|
|
|
|
* @param missingTranslationHandler
|
|
|
|
* @param useDefaultLang
|
|
|
|
* @param isolate
|
|
|
|
*/
|
|
|
|
public constructor(
|
|
|
|
store: TranslateStore,
|
|
|
|
currentLoader: TranslateLoader,
|
|
|
|
compiler: TranslateCompiler,
|
|
|
|
parser: TranslateParser,
|
|
|
|
missingTranslationHandler: MissingTranslationHandler,
|
|
|
|
@Inject(USE_DEFAULT_LANG) useDefaultLang: boolean = true,
|
|
|
|
@Inject(USE_STORE) isolate: boolean = false
|
|
|
|
) {
|
2020-04-01 12:46:06 +02:00
|
|
|
super(store, currentLoader, compiler, parser, missingTranslationHandler, useDefaultLang, isolate, true, 'en');
|
2018-11-08 15:34:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the original get function and returns an empty string instead of throwing an error.
|
|
|
|
*
|
|
|
|
* @override
|
|
|
|
*/
|
2019-02-14 13:17:59 +01:00
|
|
|
public get(key: string | string[], interpolateParams?: Object): Observable<string | any> {
|
2018-11-08 15:34:43 +01:00
|
|
|
try {
|
|
|
|
return super.get(key, interpolateParams);
|
|
|
|
} catch {
|
|
|
|
return of('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the original key function and returns an empty string instead of throwing an error.
|
|
|
|
*
|
|
|
|
* @override
|
|
|
|
*/
|
2019-02-14 13:17:59 +01:00
|
|
|
public stream(key: string | string[], interpolateParams?: Object): Observable<string | any> {
|
2018-11-08 15:34:43 +01:00
|
|
|
try {
|
|
|
|
return super.stream(key, interpolateParams);
|
|
|
|
} catch {
|
|
|
|
return of('');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses the original instant function and returns an empty string instead of throwing an error.
|
|
|
|
*
|
|
|
|
* @override
|
|
|
|
*/
|
2019-02-14 13:17:59 +01:00
|
|
|
public instant(key: string | string[], interpolateParams?: Object): string | any {
|
2018-11-08 15:34:43 +01:00
|
|
|
try {
|
|
|
|
return super.instant(key, interpolateParams);
|
|
|
|
} catch {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|