为什么这段代码没有改变我的整个应用语言?

时间:2019-03-27 08:27:27

标签: java android android-fragments

我已经编写了以下代码来更改应用程序语言,但并没有更改整个应用程序语言:

public class SettingsFragment extends PreferenceFragment implements 
          Preference.OnPreferenceChangeListener {

    private PrefHelper prefHelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref);

        prefHelper = new PrefHelper(getActivity());

        ListPreference langPreference = (ListPreference) findPreference(prefHelper.PREF_KEY_LANG);
        langPreference.setOnPreferenceChangeListener(this);
        langPreference.setSummary(langPreference.getEntry());
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (prefHelper.PREF_KEY_LANG.equals(preference.getKey())) {
            changeLang((String) newValue);
            return true;
        }
        return true;
    }

    private void changeLang(String lang) {
        prefHelper.setLang(lang);

        WeakReference<Context> cReference = new WeakReference<>(getActivity());
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        PendingIntent intent = PendingIntent.getActivity(cReference.get(), 0,
            new Intent(new Intent(getActivity().getApplicationContext(), QuestionsActivity.class)), 0);

        AlarmManager mgr = (AlarmManager) getActivity().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, intent);

        System.exit(1);
    }
}

任何人都可以说出什么问题吗?

我创建了两个分开的eng.xml和rus.xml。

2 个答案:

答案 0 :(得分:1)

要使用所有API,请在“活动”中使用此代码

 override fun attachBaseContext(base: Context) {
    super.attachBaseContext(LocaleHelper.updateBaseContextLocale(base))
}

object LocaleHelper {
    fun updateBaseContextLocale(baseContext: Context): Context {
        val localeManager = LocaleManagerImpl(SharedPrefsStorage(baseContext))
        val locale = localeManager.locale
        Locale.setDefault(locale)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return updateResourcesLocale(baseContext, locale)
        } else {
            return updateResourcesLocaleLegacy(baseContext, locale)
        }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private fun updateResourcesLocale(context: Context, locale: Locale): Context {
        val configuration = context.resources.configuration
        configuration.setLocale(locale)
        return context.createConfigurationContext(configuration)
    }

    private fun updateResourcesLocaleLegacy(context: Context, locale: Locale): Context {
        val resources = context.resources
        val configuration = resources.configuration
        configuration.locale = locale
        resources.updateConfiguration(configuration, resources.displayMetrics)
        return context
    }

    fun updateApplicationContextLocale(applicationContext: Context,
                                       localeConfiguration: Configuration,
                                       locale: Locale) {
        applicationContext.resources.updateConfiguration(localeConfiguration,
                applicationContext.resources.displayMetrics)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            applicationContext.createConfigurationContext(localeConfiguration)
        } else {
            Locale.setDefault(locale)
            val config = applicationContext.resources.configuration
            config.locale = locale
            applicationContext.resources.updateConfiguration(config,
                    applicationContext.resources.displayMetrics)
        }
    }

    fun getCurrentLocale(resources: Resources): Locale {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            resources.configuration.locales[0]
        } else {
            resources.configuration.locale
        }    
    }
}

此外,如果要在运行时进行更改,只需在Activity中使用recreate()

这是我想出的对所有API和所有资源更改语言环境的唯一方法。如果要使其工作,您将避免使用不推荐使用的代码。

答案 1 :(得分:-1)

我正在这样使用并且工作正常

 public static void changeLocale(Resources res, String locale) {

    Configuration config;
    config = new Configuration(res.getConfiguration());

    switch (locale) {
        case "th":
            config.locale = new Locale("th");
            break;
        case "TH":
            config.locale = new Locale("th");
            break;
        default:
            config.locale = Locale.ENGLISH;
            break;
    }

    res.updateConfiguration(config, res.getDisplayMetrics());

}

然后从您的应用程序中的任何地方调用,如下所示

LanguageHelper.changeLocale(resources, "th")

然后像这样重新启动意图

Intent restart = getIntent();
finish();
startActivity(restart);