迁移到Androidx后更改语言环境不起作用 [英] Change Locale not work after migrate to Androidx

查看:590
本文介绍了迁移到Androidx后更改语言环境不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个支持多语言的旧项目.我想升级支持库和目标平台,在迁移到Androidx之前一切正常,但是现在更改语言不起作用!

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!

我使用此代码来更改App的默认语言环境

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);
}

并通过覆盖attachBaseContext在每个活动上调用此方法,如下所示:

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));
}

我尝试其他方法来获取字符串,但我注意到‍‍ getActivity().getBaseContext().getString有效,而getActivity().getString不起作用.即使以下代码也不起作用,并且始终在默认资源string.xml中显示app_name vlaue.

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"/>

我在 https://github.com/Freydoonk/LanguageTest

getActivity()..getResources().getIdentifier不起作用,总是返回0!

Also getActivity()..getResources().getIdentifier not work and always return 0!

推荐答案

最后,我在我的应用程序中发现了问题.将项目迁移到我的项目的Androidx依赖项时,更改如下:

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'
} 

如所见,当我将androidx.appcompat:appcompat的版本更改为最新的稳定版本1.0.2时,其版本为1.1.0-alpha03,我的问题已解决且更改语言正常工作.

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.

我在 Maven存储库中找到了appcompat库的最新稳定版本.我还将其他库更改为最新的稳定版本.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆