Полагаю, вы можете создать Subject в TranslationService и подписаться в директиве, например,
export class TranslationService {
private subject = new Subject<any>();
public observable=this.subject.asObservable();
loadTranslations(lang: string) {
...
this.subject.next(true)
}
...
}
export class I18nDirective implements OnInit {
@Input() i18n: string;
constructor(private el: ElementRef,
private translationService: TranslationService) {}
ngOnInit() {
this.translationService.observable.pipe(
startWith(null)
).subscribe(()=>{
this.el.nativeElement.textContent = this.translationService.get(this.i18n);
})
}
}
Другой вариант - создать метод setValue в вашей директиве
setValue()
{
this.el.nativeElement.textContent = this.translationService.get(this.i18n);
}
И в вашем компоненте получите директиву, используя ViewChildren
@ViewChildren(I18nDirective) labels: QueryList<I18nDirective>;
при изменении идиомы
changeIdioma() {
this.translationService.loadTranslations(this.idioma).subscribe(()=>{
this.labels && this.labels.forEach(x => x.setValue());
})
}
Вы можете увидеть в stackblitz (*)
( *) Я поставил два пути, просто разомкнуть строки