不建议使用配置`locale`变量-Android [英] Configuration `locale` variable is deprecated - Android
问题描述
我使用以下代码在多种语言应用中手动设置默认语言:
I use this code to set the default language manually in multiple languages app:
public static void setLanguage(Context context, String languageCode){
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale; // Deprecated !!
context.getApplicationContext().getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}
所以现在我们不能通过config设置语言环境.不赞成使用语言环境,因为该变量将在API 24中删除.
so now we cant set locale by config.locale is deprecated because the variable is going to be removed in API 24.
所以我看到了替代方法是设置:
So i saw that the alternative is to set:
config.setLocales();
config.setLocales();
语言环境
已添加到API级别1
Added in API level 1
语言环境语言环境
此字段在API中已弃用 第24级.请勿直接设置或阅读.使用getLocales()和 setLocales(LocaleList).如果仅需要主要语言环境, getLocales().get(0)现在是首选访问器.
This field was deprecated in API level 24. Do not set or read this directly. Use getLocales() and setLocales(LocaleList). If only the primary locale is needed, getLocales().get(0) is now the preferred accessor.
当前用户对语言环境的偏好,与语言环境相对应 资源限定符.
Current user preference for the locale, corresponding to locale resource qualifier.
我还注意到,除了api 17及更高版本之外,还有setLocale(Locale)
I noticed also that there is setLocale(Locale)
as well but for api 17 and above
我检查了setLocales(LocalList)文档,并将其标记为灰色,好像它也已弃用!
I checked the setLocales(LocalList) documentation and it is marked in grey as if it is also deprecated !
那么对此有什么解决方案!?
so what can be solution for this !?
推荐答案
希望这会有所帮助,我还添加了吸气剂.
Hope this helps, I also added the getters.
@SuppressWarnings("deprecation")
public Locale getSystemLocaleLegacy(Configuration config){
return config.locale;
}
@TargetApi(Build.VERSION_CODES.N)
public Locale getSystemLocale(Configuration config){
return config.getLocales().get(0);
}
@SuppressWarnings("deprecation")
public void setSystemLocaleLegacy(Configuration config, Locale locale){
config.locale = locale;
}
@TargetApi(Build.VERSION_CODES.N)
public void setSystemLocale(Configuration config, Locale locale){
config.setLocale(locale);
}
public static void setLanguage(Context context, String languageCode){
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setSystemLocale(config, locale);
}else{
setSystemLocaleLegacy(config, locale);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
context.getApplicationContext().getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}
更新12/3/2016
由于现在不推荐使用context.getApplicationContext().getResources().updateConfiguration(),因此我强烈建议您签出此解决方案并采用另一种方法来覆盖Android系统配置.
UPDATE 12/3/2016
Since context.getApplicationContext().getResources().updateConfiguration() is now deprecated, I highly recommend that you check out this solution and adopt a different approach to overwriting Android system configuration.
更好的解决方案: https://stackoverflow.com/a/40704077/2199894
这篇关于不建议使用配置`locale`变量-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!