How can I change language of my application?

后端 未结 2 1142
暗喜
暗喜 2020-12-05 06:28

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 la

2条回答
  •  庸人自扰
    2020-12-05 06:50

    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

    
    

    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");
    }
    

提交回复
热议问题