How To Set Default Language for Android app?
Asked Answered
H

4

11

I've created an app in two languages. The second one (english), is used when user's default system language is english. If it's not, then the first one is used.

I want to set the second language (that's english) as a DEFAULT language, which means that when user opens my app and his system language is not the first one, nor English, the English language will appear as a default one.

I tried:

    Locale locale = new Locale("en_US");
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    context.getApplicationContext.getResources().updateConfiguration(config, null);

But got "context cannot be resolved" error everytime.. Is this piece of code right or..?

Harappa answered 23/5, 2014 at 21:35 Comment(2)
You shouldn't need to do that. Whatever resources you put in a "base" folder (e.g. values vs values-es) will be used when the language does not match any of the provided alternatives.Heiser
it's giving error because you have not defined the context variable, so at your first activity you can write as bellow before setContentView(), Locale locale = new Locale("en_US"); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getApplicationContext().getResources().updateConfiguration(config, null); this works for me.Huth
H
15

Okay,

to make everything clear, I realized res/values is a DEFAULT directory and the others are just "in case of language". So everything I had to do was to switch the english to /res/values and the other language goes to res/values-es

Harappa answered 23/5, 2014 at 21:49 Comment(0)
P
0

You should define all languages you support using res folders, i.e res/values, res/values-en, res/values-fr. The system will take care of everything else, you don't need any code.

Parnassus answered 23/5, 2014 at 21:43 Comment(0)
R
0

If you are in an activity you can do:

this.getApplicationContext().getResources().updateConfiguration(config, null);

...to fix your error. Otherwise you need to pass in the context.

Make sure you add the parenthesis at the end of getApplicationContext(). You didn't do so in your code.

Rechabite answered 23/5, 2014 at 21:44 Comment(0)
C
-1

I've been in the same situation, my app was first created in portuguese (BR) so we went global and I had as second language En-Us, so my solution was creating a new language ( clicking in translation Editor + Brazil )... so I have my default language (Portuguese) second (English) third (Portuguese)

then I replaced the resource/ values to the english strings setting as default...

Caravaggio answered 24/8, 2020 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.