Eri*_*ric 12 time android locale date relative-date
我发现使用android.text.format.DateUtils返回"昨天"或"2小时前"等值的相对API非常好 - 但我的应用程序并不支持Android所做的每一种语言.所以,我默认为英语,但对于我不支持的每种语言,相对字符串显示在设备的设置中.
例如,像:
Last attempt: hace 11 minutos.
对于我不支持的任何语言,我想将API调用默认为英语.但是,我没有看到任何地方为API调用设置Locale - 我希望我只是在某处丢失它.
有没有办法为API调用设置Locale,忽略设备设置?
Mar*_*ues 12
这适用于Android 7
void forceLocale(Locale locale) {
Configuration conf = getBaseContext().getResources().getConfiguration();
updateConfiguration(conf, locale);
getBaseContext().getResources().updateConfiguration(conf, getResources().getDisplayMetrics());
Configuration systemConf = Resources.getSystem().getConfiguration();
updateConfiguration(systemConf, locale);
Resources.getSystem().updateConfiguration(conf, getResources().getDisplayMetrics());
Locale.setDefault(locale);
}
void updateConfiguration(Configuration conf, Locale locale) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
conf.setLocale(locale);
}else {
//noinspection deprecation
conf.locale = locale;
}
}
Run Code Online (Sandbox Code Playgroud)
根据DateUtils类的源代码,它使用两者Resource.getSystem()和Locale.getDefault()格式化日期和时间的方法.您可以更改默认Locale使用Locale.setDefault()方法,但我认为不可能更改方法的返回值Resource.getSystem().您可以尝试将默认语言环境更改为,Locale.US但在我看来,在这种情况下结果会更糟.
| 归档时间: |
|
| 查看次数: |
4134 次 |
| 最近记录: |