At the moment, I am loading a JSON file in the following way:
translation.service.ts
@Injectable()
export class TranslationService {
private _messages = [];
constructor(private _http: Http) {
var observable = this._http.get("/app/i18n/localizable.it.strings").map((res: Response) => res.json());
observable.subscribe(res => {
this._messages = res;
});
}
getTranslationByKey(key: string, args?: any[]) {
return this._messages[key];
}
}
localizable.it.strings
{
"home.nav.calendar": "Calendar",
...
}
My TranslationService
is injected into my HomeComponent
, but the issue arises when trying to access these values through a pipe before they are fully loaded during page rendering.
translate.pipe.ts
@Pipe({name: 'translate'})
export class TranslatePipe implements PipeTransform {
constructor(private _translation: TranslationService) {}
transform(value: string, args: string[]) : any {
return this._translation.getTranslationByKey(value);
}
}
Is there a solution to load all values from a JSON file prior to any page being rendered?