John Smith
John Smith

Reputation: 605

android - Change language AND country programmatically

I'm developing an app that allows user to change language in app's settings. But as long as some countries have 2 or more languages and some languages are used in more than 1 country, I need to use xx-XX format. For example, users from Belgium can select French or Dutch language.

I use the next code snippet to change language:

protected void setLocale() {
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    Locale locale = new Locale(dataManager.getLocale().toLowerCase());
    Locale.setDefault(locale);
    conf.locale = locale;
    res.updateConfiguration(conf, dm);
}

dataManager.getLocale() returns String like "fr-BE" or "nl-BE".

I have following resource files:

values-fr/strings.xml values-fr-rBE/strings.xml values-nl/strings.xml values-nl-rBE/strings.xml

But even if I set locale to nl-BE, it uses strings from values-nl directory, but not from values-nl-rBE.

So, my question is: "How can I make my app to use resources for certain language AND region in runtime?"

Upvotes: 1

Views: 2287

Answers (1)

Mattia Maestrini
Mattia Maestrini

Reputation: 32810

You should specify the country in the second parameter of Locale constructor.

Locale (String language, String country)
Constructs a new Locale using the specified language and country codes.

For example:

Locale locale = new Locale("nl", "BE");

Upvotes: 4

Related Questions