5

I have to make my Android application in 3 languages ie German, English and dutch. i have made three folders in my android application names values-de and values-nl within the res directory. now when the user selects a specific language i perform the following code:

Resources res = getResources(); Configuration newConfig = new Configuration(res.getConfiguration()); newConfig.locale = Locale.ENGLISH; res.updateConfiguration(newConfig, null); 

all the strings in the different values folders have the same name, i.e a string with name add_site in the values folder has the same name in the values-de folder but with a different value. My application is not loading the German value when i set the locale to German? what could be the problem?

thank you for you help.

3
  • It is my understanding that locales are automatically handled by Android according to system preferences. Is there any reason you need the users to be able to set the locale from within your application? Commented Mar 18, 2011 at 6:02
  • yes. this is because just to view my application the user will have to change the system configration which will result in all application being set in that language....which i believe is unnecessary. although now when i change the system configration the code is working fine, meaning the german text is being shown when i have set the system locale to german... i want to achieve the same from within the application Commented Mar 18, 2011 at 6:07
  • To some extend that is a nice idea as a feature but keep in mind that it is against how all other applications on Android including the stock applications work. They all automatically detect your locale and load the correct resources on the fly without user intervention. Unless there is a really good reason I would not break this default behaviour. Commented Mar 18, 2011 at 15:38

2 Answers 2

6

Try putting this in onCreate() just after the call to super.onCreate:

Locale locale = new Locale("de"); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); 

EDIT: Here's another approach. It seems to be very flexible, but there seems to be some disagreement in the comments whether it works on all Android versions.

Sign up to request clarification or add additional context in comments.

1 Comment

i want to change the locale only when the user wants. ie there is a languages.java file where via a checkbox, i make the user select the language and there is another activity sitelisting.java where i want to see the changes....
0

To set the Configuration locale is not enough in case you also support a language which has a different layout direction LTR or RTL.

 Resources res = getResources(); Configuration newConfig = new Configuration( res.getConfiguration() ); Locale locale = new Locale( appLanguage ); newConfig.locale = locale; newConfig.setLayoutDirection( locale ); res.updateConfiguration( newConfig, null ); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.