我正在开发一个支持两种不同语言的应用程序 -
-
英文
-
阿拉伯语
用户可以随时切换到任何语言,并根据我正在更改应用程序语言的选择 .
下面提到的是代码片段 -
public static void setLocale(String languageCode, Context ctx) {
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= VERSION_CODES.N) {
updateResourcesLocale(ctx, locale);
} else {
updateResourcesLocaleLegacy(ctx, locale);
}
}
private static void updateResourcesLocale(Context context, Locale locale) {
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
context.getApplicationContext().createConfigurationContext(configuration);
}
private static void updateResourcesLocaleLegacy(Context context, Locale locale) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
configuration.setLayoutDirection(locale);
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
同样在应用程序的每个活动中,我都覆盖了 attachBaseContext 方法 . 我在其中使用了以下逻辑 -
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(ContextWrapper.onAttach(base, LanguageUtil.getSelectedLanguageFromSharedPreferences(base)));
}
This is the Utility class -
public class ContextWrapper {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static Context onAttach(Context context, String defaultLanguage) {
persist(context, defaultLanguage);
String lang = getPersistedData(context, defaultLanguage);
return setLocale(context, lang);
}
private 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);
configuration.setLayoutDirection(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;
}
问题是 - 如果我切换到阿拉伯语,布局方向将变为RTL,这是预期的 . 然后我'm switching my language to English again. However this time, the layout direction is not updating. It'仍然是RTL,但它应该是LTR . 此时,如果我将语言更改为阿拉伯语,布局方向将转到LTR,这也是错误的 . 我在 application tag of manifest 文件中添加了以下属性 -
android:supportsRtl="true"
另外,我已经全部替换 left alignments by start & right alignments by end.
在调试时,观察到布局方向正确保存 . 然而,它没有在UI中反映出来 .
我的 minSdkVersion 19 & targetSdkVersion 27
我陷入了困境,无法解决这个问题 . 这里有什么我想念的吗?
1 回答
最后经过批量's of trial & error, I' ve修复了这个问题,通过在所有 activities onCreate method before setContentView 中调用以下代码添加 .