我正在构建一个Android应用,该应用需要在运行时更改语言环境。我重新创建了整个活动,并且某些绑定到viewmodel
的字符串发生了变化,但是(strings.xml)的其他字符串仍未翻译。我在活动中使用以下课程进行翻译:
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();
}
}
以及我的活动:
fun setLanguage(language: String){
Language.setLanguage(MainApplication.getContext(),language, this)
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
recreate()
}
我只是从基于Java的旧资料中复制粘贴了此文字。
在您的Application类和Mainactvity中覆盖它。
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleHelper.onAttach(base));
Log.e(TAG, "attachBaseContext: ");
}
使用方法:每当单击某项时,都应像这样调用。
这里,
语言是您好(印地语,而该地区是印度(IN)] >>
例如:
LocaleHelper.setLocale(this,“ hi”,“ IN”);recreate(); //现在重新启动。
本地助手类
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, int i) {
Log.d(TAG, "onAttach: i " + i);
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);
}
}