Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

android - How can I change language of my application?

In my application I have a option of language selection.

There are three languages: English, German & Spanish. When I select an option, the entire application language should be changed.

How can I make this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do you mean that you want to use another language than the default language in the phone? I have that in one application, and this is what I had to do.

Add this to your activity declaration in the AndroidManifest.xml

<activity
    android:name=".ui.SomeActivity"
    android:configChanges="locale"
    :
    :
</activity>

And then invoke a method like this from onCreate in your activity:

public static void setLanguage(Context context, String languageToLoad) {
    Log.d(TAG, "setting language");
    Locale locale = new Locale(languageToLoad); //e.g "sv"
    Locale systemLocale = SystemLocale.getInstance().getCurrentLocale(context);
    if (systemLocale != null && systemLocale.equals(locale)) {
       Log.d(TAG, "Already correct language set");
       return;
    }
    Locale.setDefault(locale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = locale;
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    Log.d(TAG, "Language set");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...