I have a pipe that resolves message key and returns message accordingly to locale.
But i want angular to wait untill my service will finish it's loading messages, and only after that continue rendering the main page. Where my localization pipe is used. How to get it work?
Service:
@Injectable()
export class I18nService implements OnInit {
private cachedMessages: {string: string};
activeLocale:I18Enum = null;
constructor(private http: Http) {
this.reloadLocale(I18Enum.ru);
}
ngOnInit(): void {
this.reloadLocale(I18Enum.ru);
}
getMessage(key: string) {
if (this.cachedMessages) {
return this.cachedMessages[key];
}
}
reloadLocale(locale: I18Enum) {
this.activeLocale = locale;
this.http.get(basePath + i18nPath + I18Enum[locale]).subscribe(
res => {
this.cachedMessages = res.json();
}
);
}
}
And pipe:
@Pipe({name: 'i18nPipe'})
export class I18nPipe implements PipeTransform {
constructor(private i18Service: I18nService) {
}
transform(value: any, args: any): any {
return this.i18Service.getMessage(value);
}
}