So, dont know if anyone can help me. I have the below line of code in html :
<ion-item class="item-style">
<ion-label>{{ 'SettingsPage.ChangeLanguage' | translate }}</ion-label>
<ion-select [(ngModel)]="language" (ionChange)="onLanguageChange();" cancelText={{cancelText}} okText={{okText}}>
<ion-select-option value="en">{{ 'SettingsPage.English' | translate }}</ion-select-option>
<ion-select-option value="mt">{{ 'SettingsPage.Maltese' | translate }}</ion-select-option>
</ion-select>
</ion-item>
and the following methods in the .ts :
onLanguageChange() {
this.translate.use(this.language);
console.log(this.language);
this.globalVariableService.languageChanged = true;
this.globalVariableService.setCurrentLanguage(this.language);
this.storage.set('Language', this.language).then(() => {
this.sectorDataServiceProvider.populateSectorData().then(data => {
console.log('this.sectorInfo ', this.sectorDataServiceProvider.sectorInfo);
});
});
this.setOkAndCancelText();
}
setOkAndCancelText() {
if (this.language === 'en') {
this.cancelText = 'Cancel';
this.okText = 'OK';
} else if (this.language === 'mt') {
this.cancelText = 'Le';
this.okText = 'Iva';
}
}
I wish to remove the s setOkAndCancelText() which is being used to fill the cancelText={{cancelText}} parameter when the language changes within the app, and have something similar to:
<p [innerHTML]="'TermsOfUsePage.Header' | translate"></p>
Any ideas how i can make this possible please?
Instead of reliaing on libraries, you can create your custom service and add your local strings i.e key and value as following:
Note that it is flexible, you can go deeper in objects. Now simply create a pipe for this service:
Now simply call it in your HTML:
You can could make the the language parameter dynamic as well and in pipe, give it a default value. anyway here is the way to call it for your other language:
Try this stackblitz example
Translate Service
Translate Pipe
We have done it in following ways:
Here we have kept our en.json file under i18n folder where all key values of text are there.
After this you can use it in html template:
Here cancel.text is a key in en.json file.