我在更改应用程序中的语言时遇到一些问题,我不知道为什么它不起作用,没有错误,请检查日志是否全部清除。我希望我的应用程序具有西班牙语和英语。我已经创建了用于西班牙文的strings.xml,并设置了LocaleHelper,还创建了包含覆盖方法“ attachBaseContext”的应用程序类,但仍然无法正常工作...
我的语言环境助手
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);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
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;
}
}
我的string.xml夹心翻译成西班牙语
<resources>
<string name="item1">Leer Mas Tarde</string>
<string name="item2">Ajustes</string>
<string name="item3">Acerca De</string>
<string name="item4">Términos y Condiciones</string>
<string name="read_later">Leer Mas Tarde</string>
<string name="delete">Borrar</string>
<string name="feedback">Realimentación</string>
<string name="change_language">Cambiar Idioma</string>
我的扩展应用程序的应用程序类
public class App extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
}
} 这是我的按钮单击,它会打开列出的警报对话框以选择要使用的语言
private void changeLanguage(){
btn_changelanguage = findViewById(R.id.changelanguage_button);
btn_changelanguage.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Select Language");
//add a list
builder.setItems(name_languages, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
LocaleHelper.setLocale(context, "en");
Toast.makeText(context, "English selected", Toast.LENGTH_SHORT).show();
recreate();
break;
case 1:
LocaleHelper.setLocale(context, "ca-rES");
Toast.makeText(context, "Spanish selected", Toast.LENGTH_SHORT).show();
recreate();
break;
}
}
}) ;
AlertDialog dialog = builder.create();
dialog.show();
}
}
);
}
其中name_language是:
String[] name_languages = {"English", "Spanish"};