Change app language programmatically in Android

前端 未结 30 3749
面向向阳花
面向向阳花 2020-11-21 04:34

Is it possible to change the language of an app programmatically while still using Android resources?

If not, is it possible to request a resource in an specific lan

30条回答
  •  没有蜡笔的小新
    2020-11-21 05:20

    I know it's late to answer but i found this article here . Which explains the whole process very well and provides you a well structured code.

    Locale Helper class:

    import android.annotation.TargetApi;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.res.Configuration;
    import android.content.res.Resources;
    import android.os.Build;
    import android.preference.PreferenceManager;
    
    import java.util.Locale;
    
    /**
     * This class is used to change your application locale and persist this change for the next time
     * that your app is going to be used.
     * 

    * You can also change the locale of your application on the fly by using the setLocale method. *

    * Created by gunhansancar on 07/10/15. */ public class LocaleHelper { private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language"; public static Context onAttach(Context context) { String lang = getPersistedData(context, Locale.getDefault().getLanguage()); return setLocale(context, lang); } public static Context onAttach(Context context, String defaultLanguage) { String lang = getPersistedData(context, defaultLanguage); return setLocale(context, lang); } public static String getLanguage(Context context) { return getPersistedData(context, Locale.getDefault().getLanguage()); } public static Context setLocale(Context context, String language) { persist(context, language); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return updateResources(context, language); } return updateResourcesLegacy(context, language); } private static String getPersistedData(Context context, String defaultLanguage) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); return preferences.getString(SELECTED_LANGUAGE, defaultLanguage); } private static void persist(Context context, String language) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = preferences.edit(); editor.putString(SELECTED_LANGUAGE, language); editor.apply(); } @TargetApi(Build.VERSION_CODES.N) private static Context updateResources(Context context, String language) { Locale locale = new Locale(language); Locale.setDefault(locale); Configuration configuration = context.getResources().getConfiguration(); configuration.setLocale(locale); configuration.setLayoutDirection(locale); return context.createConfigurationContext(configuration); } @SuppressWarnings("deprecation") private static Context updateResourcesLegacy(Context context, String language) { Locale locale = new Locale(language); Locale.setDefault(locale); Resources resources = context.getResources(); Configuration configuration = resources.getConfiguration(); configuration.locale = locale; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { configuration.setLayoutDirection(locale); } resources.updateConfiguration(configuration, resources.getDisplayMetrics()); return context; } }

    You need to override attachBaseContext and call LocaleHelper.onAttach() to initialize the locale settings in your application.

    import android.app.Application;
    import android.content.Context;
    
    import com.gunhansancar.changelanguageexample.helper.LocaleHelper;
    
    public class MainApplication extends Application {
        @Override
        protected void attachBaseContext(Context base) {
            super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
        }
    }
    

    All you have to do is to add

    LocaleHelper.onCreate(this, "en");
    

    wherever you want to change the locale.

提交回复
热议问题