Change application language in android (Per-app language preferences)

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.

1 Like

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