迁移到 Androidx 后更改区域设置不起作用 [英] Change Locale not work after migrate to Androidx
问题描述
我有一个支持多语言的旧项目.我想升级支持库和目标平台,在迁移到 Androidx
之前一切正常,但现在更改语言不起作用!
我使用此代码更改应用程序的默认语言环境
private static Context updateResources(Context context, String language){Locale locale = new Locale(language);Locale.setDefault(locale);配置配置 = context.getResources().getConfiguration();configuration.setLocale(locale);返回 context.createConfigurationContext(configuration);}
并通过像这样覆盖 attachBaseContext
来在每个 Activity 上调用此方法:
@Overrideprotected void attachBaseContext(Context newBase){SharedPreferences 首选项 = PreferenceManager.getDefaultSharedPreferences(context);字符串语言=preferences.getString(SELECTED_LANGUAGE, "fa");super.attachBaseContext(updateResources(newBase, language));}
我尝试了其他方法来获取字符串,但我注意到 getActivity().getBaseContext().getString
有效而 getActivity().getString
无效.即使下面的代码也不起作用,并且总是在默认资源 string.xml 中显示 app_name
vlaue.
我在 https://github.com/Freydoonk/LanguageTest
还有 getActivity()..getResources().getIdentifier
不起作用,总是返回 0!
最后,我在我的应用中找到了问题.将项目迁移到 Androidx
时,我的项目的依赖项更改如下:
依赖项{实现文件树(包括:['*.jar'],目录:'libs')实现 'androidx.appcompat:appcompat:1.1.0-alpha03'实现 'androidx.constraintlayout:constraintlayout:1.1.3'实现 'com.google.android.material:material:1.1.0-alpha04'androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'}
正如所见,当我将其更改为最新的稳定版本 1.0 时,
,我的问题解决了,更改语言正常工作.androidx.appcompat:appcompat
的版本是 1.1.0-alpha03
.2
我在 Maven 存储库中找到了 appcompat
库的最新稳定版本.我还将其他库更改为最新的稳定版本.
现在我的应用依赖部分如下所示:
依赖项{实现文件树(包括:['*.jar'],目录:'libs')实现 'androidx.appcompat:appcompat:1.0.2'实现 'androidx.constraintlayout:constraintlayout:1.1.3'实现 'com.google.android.material:material:1.0.0'androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'}
I have an old project that supports multi-languages. I want to upgrade support library and target platform, Before migrating to Androidx
everything works fine but now change language not work!
I use this code to change default locale of App
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);
}
And call this method on each activity by override attachBaseContext
like this:
@Override
protected void attachBaseContext(Context newBase)
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String language = preferences.getString(SELECTED_LANGUAGE, "fa");
super.attachBaseContext(updateResources(newBase, language));
}
I try other method to get string and I noticed that getActivity().getBaseContext().getString
work and getActivity().getString
not work. Even the following code does not work and always show app_name
vlaue in default resource string.xml.
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"/>
I share a sample code in https://github.com/Freydoonk/LanguageTest
Also getActivity()..getResources().getIdentifier
not work and always return 0!
Finally, I find the problem in my app. When migrating the project to Androidx
dependencies of my project changed like this:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0-alpha03'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0-alpha04'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'
}
As it is seen, version of androidx.appcompat:appcompat
is 1.1.0-alpha03
when I changed it to the latest stable version, 1.0.2
, my problem is resolved and the change language working properly.
I find the latest stable version of appcompat
library in Maven Repository. I also change other libraries to the latest stable version.
Now my app dependencies section is like bellow:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
这篇关于迁移到 Androidx 后更改区域设置不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!