gpt4 book ai didi

java - Android 区域设置在运行时不会(完全)改变

转载 作者:行者123 更新时间:2023-12-02 08:39:08 24 4
gpt4 key购买 nike

我正在构建一个 Android 应用程序,需要在运行时更改区域设置。我重新创建了整个 Activity ,一些绑定(bind)到我的 viewmodel 的字符串发生了变化,但 (strings.xml) 的其他字符串仍然未翻译。我在我的 Activity 中使用了以下类(class)来翻译:

    public class Language {
private static String PREFS_LANGUAGE = "LANGUAGE";

public static void setLanguage(Context context, String language, Activity activity) {
// Save selected language
persist(language);

// Update language
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = Locale.UK;
context.getResources().updateConfiguration(config, MainApplication.getContext().getResources().getDisplayMetrics());
activity.onConfigurationChanged(config);
}

private static SharedPreferences getSharedPrefs(Context context) {
return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
}

private static void persist(String language) {
SharedPreferences.Editor editor = getSharedPrefs(MainApplication.getContext()).edit();
editor.putString(PREFS_LANGUAGE, language);
editor.apply();
}
}

在我的 Activity 中:

       fun setLanguage(language: String){
Language.setLanguage(MainApplication.getContext(),language, this)
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
recreate()
}

最佳答案

我只是从基于 Java 的旧源代码中复制粘贴了此内容。

在您的应用程序类和 Mainactvity 中覆盖此设置。

  @Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleHelper.onAttach(base));
Log.e(TAG, "attachBaseContext: ");
}

How to use: Whenever you click something, invoke like this.

这里,

language is hi (Hindi) and the region is India (IN)

例如:

LocaleHelper.setLocale(this, "hi", "IN");
recreate(); //now restart.

本地助手类

 public class LocaleHelper {
private static final String TAG = "DAFT_PUNK_LH : ";


private static final String SELECTED_LANGUAGE = "en";
private static final String SELECTED_LANGUAGE_COUNTRY = "US";


public static Context onAttach(Context context) {
Log.d(TAG, "onAttach:");
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
String langCountry = getPersistedCountryData(context, Locale.getDefault().getCountry());
return setLocale(context, lang, langCountry);
}


public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}

public static String getLanguageCountry(Context context) {
return getPersistedCountryData(context, Locale.getDefault().getCountry());
}

public static Context setLocale(Context context, String language, String langCountry) {
Log.d(TAG, "setLocale: ");
persist(context, language, langCountry);

return updateResources(context, language, langCountry);


}

private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}

private static String getPersistedCountryData(Context context, String defaultLangCountry) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE_COUNTRY, defaultLangCountry);
}

private static void persist(Context context, String language, String langCountry) {
Log.d(TAG, "persist: ");
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();

editor.putString(SELECTED_LANGUAGE, language);
editor.putString(SELECTED_LANGUAGE_COUNTRY, langCountry);
editor.apply();
}


@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language, String langCountry) {
Log.d(TAG, "updateResources: ");
Locale locale = new Locale(language, langCountry);
Locale.setDefault(locale);

Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);

return context.createConfigurationContext(configuration);
}
}

关于java - Android 区域设置在运行时不会(完全)改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61496608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com