无法使用createConfigurationContext()更改应用程序的语言

时间:2018-04-11 07:44:41

标签: android

我正在尝试使用以下代码在运行时更改我的应用语言

private static Context updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources res = context.getResources();
    Configuration config = new Configuration(res.getConfiguration());
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
        config.setLocale(locale);
        LocaleList.setDefault(new LocaleList(locale));

        context = context.createConfigurationContext(config);
    }
    else if (Build.VERSION.SDK_INT >= 17) {
        config.setLocale(locale);
        context = context.createConfigurationContext(config);
    } else {
        config.locale = locale;
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
    return context;
}

我覆盖ApplicationActivity' s attachBaseContext()

protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleManager.setLocale(base));
}

奇怪的是,当我在一个单独的项目上执行这些步骤时,语言会发生变化,但我的应用程序的情况似乎无能为力。

如果区域设置在活动的上下文对象上发生更改,我已经记录了它似乎正确但仍然在获取字符串资源时它们无法正确选择。

任何帮助都将不胜感激。

修改

事实证明,问题在于我有一条线' resConfigs" en"'在build.gradle中阻止了语言的改变。

1 个答案:

答案 0 :(得分:-1)

选择Langauge活动

private LinearLayout llHindi, llEnglish;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_change_language);
    resetTitles();
    llHindi = (LinearLayout) findViewById(R.id.llHindi);
    llEnglish = (LinearLayout) findViewById(R.id.llEnglish);


    llHindi.setOnClickListener(this);
    llEnglish.setOnClickListener(this);
}



private boolean setNewLocale(String language, boolean restartProcess) {
    LocaleManager.setNewLocale(this, language);

    Intent i = new Intent(this, MainActivity.class);
    startActivity(i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));

    if (restartProcess) {
        System.exit(0);
    } else {
    }
    return true;
}



@Override
public void onClick(View v) {
    int id = v.getId();

    if (id == R.id.llEnglish) {
        setNewLocale(LANGUAGE_ENGLISH, true);
    }

    if (id == R.id.llHindi) {
        setNewLocale(LANGUAGE_HINDI, true);
    }
}



@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleManager.setLocale(base));
}



private void resetTitles() {
    try {
        ActivityInfo info = getPackageManager().getActivityInfo(getComponentName(), GET_META_DATA);
        if (info.labelRes != 0) {
            setTitle(info.labelRes);
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
}

LocaleManager

public class LocaleManager {

public static final  String LANGUAGE_ENGLISH   = "en";
public static final  String LANGUAGE_HINDI = "hi";
private static final String LANGUAGE_KEY       = "language_key";

public static Context setLocale(Context c) {
    return updateResources(c, getLanguage(c));
}

public static Context setNewLocale(Context c, String language) {
    persistLanguage(c, language);
    return updateResources(c, language);
}

public static String getLanguage(Context c) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
    return prefs.getString(LANGUAGE_KEY, LANGUAGE_ENGLISH);
}

@SuppressLint("ApplySharedPref")
private static void persistLanguage(Context c, String language) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
    // use commit() instead of apply(), because sometimes we kill the application process immediately
    // which will prevent apply() to finish
    prefs.edit().putString(LANGUAGE_KEY, language).commit();
}

private static Context updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources res = context.getResources();
    Configuration config = new Configuration(res.getConfiguration());
    if (Build.VERSION.SDK_INT >= 17) {
        config.setLocale(locale);
        context = context.createConfigurationContext(config);
    } else {
        config.locale = locale;
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
    return context;
}

public static Locale getLocale(Resources res) {
    Configuration config = res.getConfiguration();
    return Build.VERSION.SDK_INT >= 24 ? config.getLocales().get(0) : config.locale;
}

全局类范围应用程序

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleManager.setLocale(base));
    //Log.d(TAG, "attachBaseContext");
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    LocaleManager.setLocale(this);
    //Log.d(TAG, "onConfigurationChanged: " + newConfig.locale.getLanguage());
}

这将起作用,您需要为不同的语言

创建不同的string.xml文件