For an I18n-like component, I want to get a component content as string to have a fallback value in case my I18n service gets nothing, this value is supposed to be a fallback one.
I18n service get
method:
public get(key:string,
defaultValue?:string,
variables:Variables = {},
domain?:string):string {
for (let catalogue of this.catalogues) {
if (catalogue.getDomains().indexOf(domain) >= 0
&& catalogue.getLocale() == this.locale
&& catalogue.hasKey(key)) {
return I18n.setVariables(catalogue.get(key, domain, defaultValue).value, variables);
}
}
return defaultValue;
}
I18nComponent:
@Component({
selector: "i18n",
template: "{{text}}",
})
export class I18nComponent implements OnInit {
constructor(private i18n:I18n) {
}
@ContentChild() content:string; //Here, I want to store actual value as fallback one.
@Input('key') key:string;
@Input('domain') domain:string;
@Input('variables')
variables:Variables = [];
@Input("plural")
plural:number;
text:string;
ngOnInit():any {
console.log(this.content); //Here I get 'undefined'
if (this.plural !== undefined && this.plural != null) {
this.text = this.i18n.get(this.key, this.content, this.variables, this.domain);
} else {
this.text = this.i18n.getPlural(this.key, this.plural, this.content, this.variables, this.domain);
}
}
}
usage example:
<i18n key="FOO_KEY" domain="stackoverflow">I'm the default value !</i18n>
I know <ng-content></ng-content>
works but only on the template logic, I need a way to get child as string in typescript.
Already tried @ContentChild(String)
but I got undefined
.
Another way would be to do it like "Loading..." string you can have in the base app
component in the index, acting like a placeholder until you get everything loaded. I could do the same for I18n, let the laceholder here until I get something from service to replace it, but I don't know how to achieve this.