无法使用Oreo android更改我的应用程序的语言

时间:2018-04-06 17:48:55

标签: android android-studio error-handling locale android-8.0-oreo

(编辑)

我使用了一个区域设置更改解决方案来更改我的应用程序的语言,但它在oreo中不起作用。它完全适用于我的三星S4,但不适用于我的S9。

所以我正在进行这样的语言环境更改:

   public void initAppLanguages(Context context, String lang){
        PreferenceUtil.setSelectedLanguageId(lang);
        LocaleUtils.setLocale(context, lang );
        MyApplication.reouvrir=1;
        Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
        this.finishAffinity();
        finish();
        startActivity(i);


    }

我的LocaleUtils类:

public class LocaleUtils {

    @Retention(RetentionPolicy.SOURCE)
    @StringDef({ENGLISH, FRENCH, SPANISH})
    public @interface LocaleDef {
        String[] SUPPORTED_LOCALES = {ENGLISH, FRENCH, SPANISH};
    }

    public static final String ENGLISH = "en";
    public static final String FRENCH = "fr";
    public static final String SPANISH = "es";


    public static void initialize(Context context) {
        setLocale(context, ENGLISH);
    }

    public static void initialize(Context context, @LocaleDef String defaultLanguage) {
        setLocale(context, defaultLanguage);
    }


    public static boolean setLocale(Context context, @LocaleDef String language) {
        return updateResources(context, language);
    }

    private static boolean updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        context.createConfigurationContext(configuration);
        configuration.locale = locale;
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        return true;
    }
}

我的PreferenceUtil类:

public class PreferenceUtil {
    private static SharedPreferences getDefaultSharedPreference(Context context) {
        if (PreferenceManager.getDefaultSharedPreferences(MyApplication.getInstance().getApplicationContext()) != null)
            return PreferenceManager.getDefaultSharedPreferences(MyApplication.getInstance().getApplicationContext());
        else
            return null;
    }

    public static void setSelectedLanguageId(String id){
        final SharedPreferences prefs = getDefaultSharedPreference(MyApplication.getInstance().getApplicationContext());
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("app_language_id", id);
        editor.apply();
    }

    public static String getSelectedLanguageId(){
        return getDefaultSharedPreference(MyApplication.getInstance().getApplicationContext())
                .getString("app_language_id", "en");
    }
}

这样的子活动中的语言环境会发生变化:

MyApplication.initAppLanguage(mContext);

我做错了什么?为什么它不在奥利奥工作?

2 个答案:

答案 0 :(得分:2)

请按照以下步骤操作:

注意:我假设您在values文件夹中为不同的英语,西班牙语和法语创建了不同的字符串文件。 (即每个values-en,values-es,res目录中的values-fr文件夹中的strings.xml文件)

添加此文件: LocaleHelper.java

package com.test;

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.os.LocaleList;
import android.preference.PreferenceManager;
import java.util.Locale;

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

    Configuration configuration = context.getResources().getConfiguration();
    LocaleList localeList = new LocaleList(locale);
    localeList.setDefault(localeList);
    configuration.setLocales(localeList);

    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;

    resources.updateConfiguration(configuration, resources.getDisplayMetrics());

    return context;
}
}

添加文件: LanguageUtil.java

package com.test;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.Log;

import java.util.Locale;

public class LanguageUtil {
public static void changeLanguageType(Context context, Locale localelanguage) {
    Log.i("=======", "context = " + context);
    Resources resources = context.getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    Configuration config = resources.getConfiguration();
    // Application user selects language
    // reference https://developer.android.com/reference/android/content/res/Configuration.html
    if (VersionUtils.isAfter24()) {
        config.setLocale(localelanguage);
    } else {
        config.locale = localelanguage;
        resources.updateConfiguration(config, dm);
    }
}

public static Locale getLanguageType(Context context) {
    Log.i("=======", "context = " + context);
    Resources resources = context.getResources();
    Configuration config = resources.getConfiguration();
    // Application user selects language
    if (VersionUtils.isAfter24()) {
        return config.getLocales().get(0);
    } else {
        return config.locale;
    }
}
}

添加文件: MyContextWrapper.java

package com.test;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;

import java.util.Locale;

public class MyContextWrapper extends ContextWrapper {

public MyContextWrapper(Context base) {
    super(base);
}

@TargetApi(Build.VERSION_CODES.N)
public static ContextWrapper wrap(Context context, Locale newLocale) {
    Resources res = context.getResources();
    Configuration configuration = res.getConfiguration();

    if (VersionUtils.isAfter24()) {
        configuration.setLocale(newLocale);

        LocaleList localeList = new LocaleList(newLocale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);

        context = context.createConfigurationContext(configuration);

    } else if (VersionUtils.isAfter17()) {
        configuration.setLocale(newLocale);
        context = context.createConfigurationContext(configuration);

    } else {
        configuration.locale = newLocale;
        res.updateConfiguration(configuration, res.getDisplayMetrics());
    }

    return new ContextWrapper(context);
}
}

添加文件: VersionUtils.java

package com.test;

import android.os.Build;
import android.support.annotation.RequiresApi;

public final class VersionUtils {

@RequiresApi(Build.VERSION_CODES.N_MR1)
public static boolean isAfter25() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1;
}

@RequiresApi(Build.VERSION_CODES.N)
public static boolean isAfter24() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
}

public static boolean isAfter23() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}

public static boolean isAfter22() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1;
}

public static boolean isAfter21() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
}

public static boolean isAfter20() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH;
}

public static boolean isAfter19() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
}

public static boolean isAfter18() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
}

public static boolean isAfter17() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1;
}

public static boolean isAfter16() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
}

public static boolean isAfter14() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
}

public static boolean isAfter13() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2;
}

public static boolean isAfter11() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
}

public static boolean isAfter9() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
}

public static boolean isAfter8() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
}

public static boolean isAfter5() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR;
}
}

现在创建Prefs文件: Prefs.kt (这是Kotlin类文件) 注意:您可以使用自己的首选项创建和重新生成代码。

package com.test

import android.content.Context
import android.content.SharedPreferences
import android.content.SharedPreferences.Editor

class Prefs private constructor(private val context: Context) {
private val prefranceName = "testPrefs"

companion object {

    private fun getPrefs(context: Context): SharedPreferences {
        return context.getSharedPreferences(context.packageName, 0)
    }

    fun clearDefaultShared(key: String, context: Context) {
        val e = getPrefs(context).edit()
        e.remove(key)
        e.apply()
    }

    fun setString(key: String, sunDefult: String, context: Context) {
        val e = getPrefs(context).edit()
        e.putString(key, sunDefult)
        e.apply()
    }

    fun getString(key: String, sunDefult: String, context: Context): String? {
        return getPrefs(context).getString(key, sunDefult)
    }

    fun clearAll(context: Context) {
        val editor = getPrefs(context).edit()
        editor.clear()
        editor.commit()
    }
}
}

现在创建一个扩展Application类的TestApp类。 这将用于保存应用重新启动时要应用的上一个选定语言。 注意:请不要忘记将此应用类名“TestApp”添加到标记中的AndroidManifest.xml。

添加文件: TestApp.kt

package com.test

import android.app.Application
import com.test.LanguageUtil
import com.test.Prefs
import java.util.*

class TestApp : Application() {

override fun onCreate() {
    super.onCreate()
    dbAdapter = DBAdapter(applicationContext)
    dbAdapter.openDataBase()

    // Set Language Because Local will not be saved every time the app restarts after it is completely logged out
    var languageType = Prefs.getString("languageType", "en", this@TestApp).toString()


    if (languageType == "fr") {
        LanguageUtil.changeLanguageType(this, Locale.FRENCH)
    } else if (languageType == "es") {
        LanguageUtil.changeLanguageType(this, Locale("es",""))
    } else {
        LanguageUtil.changeLanguageType(this, Locale.ENGLISH)
    }

}
}

现在,在每个活动中,添加以下代码以应用更改后的语言。

这是Kotlin代码:

override fun attachBaseContext(newBase: Context) {
    var languageType = Prefs.getString("languageType", "en", newBase).toString()
    super.attachBaseContext(MyContextWrapper.wrap(newBase, Locale(languageType, "")))
}

选择其他语言时,请使用以下代码:

LanguageUtil.changeLanguageType(this@MainActivity, Locale.ENGLISH) // for englsih
LanguageUtil.changeLanguageType(this@MainActivity, Locale("es","")) // for spanish
LanguageUtil.changeLanguageType(this@MainActivity, Locale.FRENCH)// for french

之后也将其存储在共享偏好中。

请记住:如果您更改当前活动中的语言,并且您将看不到其中的语言更改,因为您需要重新启动活动以查看更改,但在此当前活动之后打开活动,您将看到更改。

如果在同一个活动中调用recreate(),它将使屏幕闪烁,因此不要使用recreate()函数。

我有一个解决方案(不是正确的解决方案,但我可以说它是补丁,它是完美的)。 要显示当前活动的更改,您需要在默认strings.xml中创建所有英语,西班牙语和法语字符串,并在用户选择其他语言时将其设置为当前活动中的运行时。

答案 1 :(得分:0)

设置新区域设置后开始您的活动。这对我有用。

private void restartActivity() {
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}

您可以看到完整的答案here