Change app language programmatically in Android

前端 未结 30 3053
面向向阳花
面向向阳花 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:18

    I finally figured out how to setup it to work on both =N android versions.

    Extend AppCompatActivity with your own abstract class, like:

    abstract class MLAppCompatActivity : AppCompatActivity() {
      override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(LocaleHelper.wrap(newBase))
      }
    
      override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            LocaleHelper.wrap(this)
        }
      }
    }
    

    attachBaseContext is called on Android >=N versions and on this way activity will use the correct context. On Android

    class TermsActivity : MLAppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_terms)
      }
    }
    

    And finally the LocaleHelper is like this:

    import android.annotation.TargetApi;
    import android.content.Context;
    import android.content.ContextWrapper;
    import android.content.SharedPreferences;
    import android.content.res.Configuration;
    import android.content.res.Resources;
    import android.os.Build;
    import android.util.DisplayMetrics;
    
    import com.at_zone.constants.SharedPreferencesKeys;
    
    import java.util.Locale;
    
    public class LocaleHelper extends ContextWrapper {
    
        public LocaleHelper(Context base) {
            super(base);
        }
    
        public static Context wrap(Context context) {
            SharedPreferences sharedPreferences = context.getSharedPreferences(
                    SharedPreferencesKeys.SHARED_PREFERENCES, Context.MODE_PRIVATE
            );
            String language = sharedPreferences.getString(SharedPreferencesKeys.CURRENT_LANGUAGE, "default");
            if (!language.equals("default")) {
                Configuration config = context.getResources().getConfiguration();
                if (!language.equals("")) {
                    Locale locale = new Locale(language);
                    Locale.setDefault(locale);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        setSystemLocale(config, locale);
                    } else {
                        setSystemLocaleLegacy(context, config, locale);
                    }
                    config.setLayoutDirection(locale);
                    context = context.createConfigurationContext(config);
                }
                return new LocaleHelper(context);
            }
            return context;
        }
    
        public static String getSystemLanguage(Context context) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                return getSystemLocale(context).getLanguage().toLowerCase();
            } else {
                return getSystemLocaleLegacy(context).getLanguage().toLowerCase();
            }
        }
    
        public static Locale getSystemLocaleLegacy(Context context) {
            Configuration config = context.getResources().getConfiguration();
            return config.locale;
        }
    
        @TargetApi(Build.VERSION_CODES.N)
        public static Locale getSystemLocale(Context context) {
            return context.getResources().getConfiguration().getLocales().get(0);
        }
    
        public static void setSystemLocaleLegacy(Context context, Configuration config, Locale locale) {
            config.locale = locale;
            Resources res = context.getResources();
            DisplayMetrics dm = res.getDisplayMetrics();
            res.updateConfiguration(config, dm);
        }
    
        @TargetApi(Build.VERSION_CODES.N)
        public static void setSystemLocale(Configuration config, Locale locale) {
            config.setLocale(locale);
        }
    
    }
    

提交回复
热议问题