I’m working on an Ionic 4 / Angular 8 app using ngx-translate
for i18n, but I’m having a little bit of difficulty with regards to some select lists in my “Settings” page…
The page in question contains two ion-select
elements, one for language selection and another for measurement system (i.e.: “Metric” or “Imperial”). Here are a few code snippets for reference:
<ion-select interface="popover" name="uiLanguage" [(ngModel)]="uiLanguage" (ionChange)="settingChanged($event)">
<ion-select-option value="system">{{ 'SETTINGS.GENERAL.LANGUAGE.SYSTEM' | translate }}</ion-select-option>
<ion-select-option value="en">{{ 'SETTINGS.GENERAL.LANGUAGE.EN' | translate }}</ion-select-option>
<ion-select-option value="de">{{ 'SETTINGS.GENERAL.LANGUAGE.DE' | translate }}</ion-select-option>
</ion-select>
<ion-select interface="popover" name="measurementSystem" [(ngModel)]="measurementSystem" (ionChange)="settingChanged($event)">
<ion-select-option value="metric">{{ 'SETTINGS.REPORT_CONTENTS.MEASUREMENT_SYSTEM.METRIC' | translate }}</ion-select-option>
<ion-select-option value="imperial">{{ 'SETTINGS.REPORT_CONTENTS.MEASUREMENT_SYSTEM.IMPERIAL' | translate }}</ion-select-option>
</ion-select>
Note: this
settingChanged
method is used for numerous other settings, allowing me to persist the setting value internally as it changes. I just hooked in a check here so that if the language option is the one being changed, that I tell my app configuration service to change the language.
settingChanged(event) {
this.config.saveUserSetting(event.srcElement.name, this[event.srcElement.name]).then((value) => {
if (event.srcElement.name === 'uiLanguage') {
this.config.changeLanguage(value);
}
});
}
I have no issues changing the UI language, as soon as that changes, the entire UI updates and all strings are then shown in the selected language. The “Interface Language” select list option seems to take a fraction longer to update, which is a bit strange… However, the “Measurement System” select list option only updates after opening that select list.
The actual contents of both select lists don’t change, it’s literally just the translation string in each item’s label that needs to refresh. This feels like there’s something in the the ion-select
element that I need to explicitly refresh here, but I can’t work out what or how…
I’ve been searching through Google for others with the same issue, but I’ve only managed to find people trying to change the contents of a select list dynamically, or people who can’t get ngx-translate
to work at all (or properly). In my case, it is working correctly, it’s just the ion-select-option
elements within ion-select
that aren’t refreshing when I want them to.
Does anyone know how I can get around this one…?