9

I'm doing an app with multilanguage. I understand the concept of string.xml for having the app language set to what is default for the phone. Is there some way of overriding this? For instance if I have a spanish phone I use in Spain but I still want to have the app in english ?

2 Answers 2

18

You can change the language (locale) of your app programatically with a simple function. You only have to implement a method similar to the one you have below which receives a string as a parameter (like 'en' for English, 'es' for Spanish), where you can configure the locale for your app and refresh your current activity to reflect the changes. The locale you applied will not be changed until you manually change it again.

public void setLocale(String lang) { 
    myLocale = new Locale(lang); 
    Resources res = getResources(); 
    DisplayMetrics dm = res.getDisplayMetrics(); 
    Configuration conf = res.getConfiguration(); 
    conf.locale = myLocale; 
    res.updateConfiguration(conf, dm); 
    Intent refresh = new Intent(this, AndroidLocalize.class); 
    startActivity(refresh); 
    finish();
} 

Make sure you import the following packages:

import java.util.Locale; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.util.DisplayMetrics; 
import android.content.res.Resources;
import android.content.res.Configuration;

Add also in manifest inside your activity tag android:configChanges="locale|orientation"

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

5 Comments

simple and fast @Andreu... +1 A mes... no hi ha molta gent de Barcelona per aqui ;)
Looks almost too good to be true. I'll test it when I get the opportunity and if it works as you say, it's definitively worth +50!
Okay then, let me know when you check it :)
God damn, it worked like a clockwork! I'm truly deeply impressed. I usually learn programming the hard way (trial and error for a week). But this I wouldn't even be close to find out for myself in a week. Since it is restarting the app I have to save my other settings to a temporary file before restarting and then on restarting look if there is a temporary file saved and if so read it, set my settings and delete the temporary settings file. But I am really amazed, you saved me a lot of time and frustration with this. Spot on Andreu!
This feedback is the best reward for me. Thank you :)
2

EDIT as correctly mentioned this is not what the OP asked, still I'll leave it here just in case someone winds up here trying to solve what I answered.

From Android Developers page:

How to Create Alternative Resources

A large part of localizing an application is providing alternative text for different languages. In some cases you will also provide alternative graphics, sounds, layouts, and other locale-specific resources.

An application can specify many res// directories, each with different qualifiers. To create an alternative resource for a different locale, you use a qualifier that specifies a language or a language-region combination. (The name of a resource directory must conform to the naming scheme described in Providing Alternative Resources, or else it will not compile.)

Example:

Suppose that your application's default language is English. Suppose also that you want to localize all the text in your application to French, and most of the text in your application (everything except the application's title) to Japanese. In this case, you could create three alternative strings.xml files, each stored in a locale-specific resource directory:

res/values/strings.xml
Contains English text for all the strings that the application uses, including text for a string named title.
res/values-fr/strings.xml
Contain French text for all the strings, including title.
res/values-ja/strings.xml
Contain Japanese text for all the strings except title.

If your Java code refers to R.string.title, here is what will happen at runtime:

If the device is set to any language other than French, Android will load title from the res/values/strings.xml file.
If the device is set to French, Android will load title from the res/values-fr/strings.xml file.

Notice that if the device is set to Japanese, Android will look for title in the res/values-ja/strings.xml file. But because no such string is included in that file, Android will fall back to the default, and will load title in English from the res/values/strings.xml file.

here's also the original link for reference: http://developer.android.com/guide/topics/resources/localization.html

4 Comments

I think the OP is not asking about that, I think he ask how to change the selected locale programatically (Seems that he already knows that he can set different strings.xml files)
Yes, this is what I meant by "I understand the concept of string.xml for having the app language set to what is default for the phone". But still thank you for the official description. I never red this but I learnt it the hard way :) So it's a +1 ;-)
By the way, I've been looking around but I can't find it. Anyone got a link to somewhere where all the official language codes are listed (en-english, hi-hindi, ja-japaneese, fr-french and so on)?
Anyone? I found this list at link, but it can't possibly be the complete list ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.