Per-app language preferences is available since android 13 (link) and this feature also enables to change the application language using an API.
Is there any way to change the application language preference in Ionic?
Per-app language preferences is available since android 13 (link) and this feature also enables to change the application language using an API.
Is there any way to change the application language preference in Ionic?
This may not be exactly what you are looking for, but in a few projects I have used the Capacitor Device API to getLanguageCode
and then use ngx-translate to manage the application language/translations.
That was the approach that I was going to follow. By doing this, if the user changes the applications preferred language the applicationās language is changed?
getLanguageCode returns the system locale , but does it return the applications preferred locale?
There is no way at the moment.
You could create a plugin to change the language, but it would require that you target SDK 33 and use app compat version 1.6.0-beta01.
Then your pluginās code should be something like:
Locale locale = Locale.forLanguageTag(yourLanguageTag); // yourLanguageTag should be something like "es-ES"
AppCompatDelegate.setApplicationLocales(LocaleListCompat.create(locale));
Then Devicesās plugin getLanguage and getLanguageTag will return āesā and āes-ESā respectively instead of the configured device language.
Also navigator.language
will return āes-ESā.
What about using navigator.language, similar to this:
async setDefaultHeightUnit() {
const locale = navigator.language;
if (locale === 'en-US') {
await this.preferencesService.setHeightUnit(HeightUnit.Feet);
} else {
await this.preferencesService.setHeightUnit(HeightUnit.Meters);
}
}
Does anyone have experience with that, does it reflect the device language? Could do a map that maps the code to the language.
or maybe use Device.getLanguageTag