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
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"
5 Comments
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