简体   繁体   English

在android应用中更改语言时,应用状态会发生变化

[英]App State Changes when changing language within the android app

We are using a total of 6 languages inside our android app, which user can choose from. 我们在android应用程序中总共使用6种语言,用户可以选择。 We are setting the locale programmatically. 我们正在以编程方式设置语言环境。 The languages are English, Hindi, Tamil, Telegu, Kannada, Malayalam, Marathi. 语言是英语,印地语,泰米尔语,泰勒古语,卡纳达语,马拉雅拉姆语,马拉地语。

Using the code below we change the app's language. 使用下面的代码,我们可以更改应用的语言。

locale = new Locale(lang);
Locale.setDefault(locale);
Configuration conf = new Configuration(config);
conf.locale = locale;

getApplicationContext().getResources().updateConfiguration(conf,getBaseContext().getResources().getDisplayMetrics());

When we change the language between Hindi and English app is able to get the data from shared preferences, but for the other languages, it returns null for the same query. 当我们在北印度语和英语之间更改语言时,应用程序能够从共享首选项中获取数据,但对于其他语言,对于同一查询,它返回null。 Please help 请帮忙

Update 1 : Following is the code to retrieve data from shared preference 更新1:以下是从共享首选项中检索数据的代码

  SharedPreferences sharedPrefs = context.getSharedPreferences(context.getString(R.string.shared_prefs_key), Context.MODE_PRIVATE);
  String value = sharedPrefs.getString(key, null);

Since you are using string value to get the data from sharedpreferences R.string.shared_prefs_key that is why the issues is occurring because the key would be changing in different language string. 由于您使用字符串值从sharedpreferences R.string.shared_prefs_key中获取数据,因此这就是为什么发生此问题的原因,因为密钥会以不同的语言字符串更改。 And since the data was stored during different key, it gives null for other language keys. 并且由于数据是在不同的键期间存储的,因此对于其他语言键,它会提供空值。

Change that to a constant value like below: 将其更改为恒定值,如下所示:

SharedPreferences sharedPrefs = context.getSharedPreferences("<your key>", Context.MODE_PRIVATE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当用户在我的应用程序中更改语言时 android:label 的文本没有改变? - Why does android:label's text not changing when user changes language within my app? Android-在应用程序内部更改语言环境语言 - Android - Changing Locale language within the app itself 使用字符串资源以编程方式在Android应用程序中更改语言? - Programmatically changing the language within the Android app using string resources? 应用Android Studio中的语言未更改 - Language not changing in app Android Studio 如果android设备上的方向发生变化,则我的应用语言默认情况下也会发生变化。 帮我 - If Orientation changes on android device my app language is changing by default language. Help me Android - 更改所有 Android 版本的应用程序语言 - Android - Changing app language for all versions of Android 当状态在Android中的TextView中更改时更改textColor - Changing the textColor when the state changes in TextView in Android Android 应用程序的语言在屏幕旋转后发生变化 - Language of Android app changes after screen rotation 当用户更改手机语言时,我的android应用应如何更改其语言? - How should my android app change its language, when user changes mobile phone language? 在 Android 中以编程方式更改应用程序语言的问题 - Problem with changing app language programmatically in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM