Change the string language in an Android action

I am making an application with a multilingual language. I understand the concept of string.xml so that the application language is set to what is by default used for the phone. Is there a way to override this? For example, if I have a Spanish phone that I use in Spain, but I still want to have an application in English?

+4
source share
2 answers

You can change the language (locale) of the application programmatically using a simple function. You only need to implement a method similar to the one you have below, which receives a string as a parameter (for example, "en" for English, "es" for Spanish), where you can configure the locale for your application and update the current activities to reflect change. The language you have applied will not be changed until you manually change it.

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 a manifest inside your activity tag android:configChanges="locale|orientation"

+11
source

, , , , , - , , .

Android:

. , , .

res//, . , , . ( , " " , .)

:

, . , , , (, ) . strings.xml, :

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.

Java R.string.title, :

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.

, , Android title res/values-ja/strings.xml. , Android , res/values ​​/strings.xml.

: http://developer.android.com/guide/topics/resources/localization.html

0

All Articles