2018-11-08 15:34:43 +01:00
|
|
|
import { TranslateDefaultParser, TranslateStore } from '@ngx-translate/core';
|
|
|
|
import { ConfigService } from '../services/config.service';
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
2019-02-01 09:51:18 +01:00
|
|
|
export interface CustomTranslation {
|
2018-11-08 15:34:43 +01:00
|
|
|
original: string;
|
|
|
|
translation: string;
|
|
|
|
}
|
|
|
|
|
2019-02-01 09:51:18 +01:00
|
|
|
export type CustomTranslations = CustomTranslation[];
|
2018-11-08 15:34:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom translate parser. Intercepts and use custom translations from the configservice.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class OpenSlidesTranslateParser extends TranslateDefaultParser {
|
|
|
|
/**
|
|
|
|
* Saves the custom translations retrieved from the config service
|
|
|
|
*/
|
|
|
|
private customTranslations: CustomTranslations = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribes to the config services and watches for updated custom translations.
|
|
|
|
*
|
|
|
|
* @param config
|
|
|
|
* @param translateStore
|
|
|
|
*/
|
|
|
|
public constructor(config: ConfigService, private translateStore: TranslateStore) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
config.get<CustomTranslations>('translations').subscribe(ct => {
|
|
|
|
if (!ct) {
|
|
|
|
ct = [];
|
|
|
|
}
|
|
|
|
this.customTranslations = ct;
|
|
|
|
|
|
|
|
// trigger reload of all languages. This does not hurt performance,
|
|
|
|
// in fact the directives and pipes just listen to the selected language.
|
|
|
|
this.translateStore.langs.forEach(lang => {
|
|
|
|
this.translateStore.onTranslationChange.emit({
|
|
|
|
lang: lang,
|
|
|
|
translations: this.translateStore.translations[lang]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Here, we actually intercept getting translations. This method is called from the
|
|
|
|
* TranslateService trying to retrieve a translation to the key.
|
|
|
|
*
|
|
|
|
* Here, the translation is searched and then overwritten by our custom translations, if
|
|
|
|
* the value exist.
|
|
|
|
*
|
|
|
|
* @param target The translation dict
|
|
|
|
* @param key The key to find the translation
|
|
|
|
*/
|
|
|
|
public getValue(target: any, key: string): any {
|
|
|
|
const translation = super.getValue(target, key);
|
|
|
|
const customTranslation = this.customTranslations.find(c => c.original === translation);
|
|
|
|
if (customTranslation) {
|
|
|
|
return customTranslation.translation;
|
|
|
|
} else {
|
|
|
|
return translation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|