在俄语下,查看通话记录,显示的时间会有问题,比如,正常情况下应该显示21分钟之前,或21小时之前,而在俄语下查看,就会发现显示不正常,会显示为1分钟之前,或是1小时之前,在其他语言下就会正常显示。通过查看源码发现 应该就是下面这句话导致此问题的发生。
resId = com.android.internal.R.plurals.abbrev_in_num_hours;
format = r.getQuantityString(resId, (int) count);
只要之歌count值为21,就会返回1。
以下为官网解释:
When the language requires special treatment of numbers like one (as with the number 1 in English and most other languages; in Russian, any number ending in 1 but not ending in 11 is in this class).
按照android官网上的解释,会看数字的最后2位,如果是11的话就是好多就显示好多,如果是X1的话,就显示为1.
比如:11就会显示为11,111就会显示会111
21则会显示为1,121也会显示为1,31也会显示1
修改思路:可以使用xliff来客制化one字符串
定位文件:\frameworks\base\core\java\android\text\format\DateUtils.java
public static CharSequence getRelativeTimeSpanString(long time, long now, long minResolution,
int flags) {
Resources r = Resources.getSystem();
boolean abbrevRelative = (flags & (FORMAT_ABBREV_RELATIVE | FORMAT_ABBREV_ALL)) != 0;
boolean past = (now >= time);
long duration = Math.abs(now - time);
int resId;
long count;
if (duration < MINUTE_IN_MILLIS && minResolution < MINUTE_IN_MILLIS) {
count = duration / SECOND_IN_MILLIS;
if (past) {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_num_seconds_ago;
} else {
resId = com.android.internal.R.plurals.num_seconds_ago;
}
} else {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_in_num_seconds;
} else {
resId = com.android.internal.R.plurals.in_num_seconds;
}
}
} else if (duration < HOUR_IN_MILLIS && minResolution < HOUR_IN_MILLIS) {
count = duration / MINUTE_IN_MILLIS;
if (past) {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_num_minutes_ago;
} else {
resId = com.android.internal.R.plurals.num_minutes_ago;
}
} else {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_in_num_minutes;
} else {
resId = com.android.internal.R.plurals.in_num_minutes;
}
}
} else if (duration < DAY_IN_MILLIS && minResolution < DAY_IN_MILLIS) {
count = duration / HOUR_IN_MILLIS;
if (past) {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_num_hours_ago;
} else {
resId = com.android.internal.R.plurals.num_hours_ago;
}
} else {
if (abbrevRelative) {
resId = com.android.internal.R.plurals.abbrev_in_num_hours;
} else {
resId = com.android.internal.R.plurals.in_num_hours;
}
}
} else if (duration < WEEK_IN_MILLIS && minResolution < WEEK_IN_MILLIS) {
return getRelativeDayString(r, time, now);
} else {
// We know that we won't be showing the time, so it is safe to pass
// in a null context.
return formatDateRange(null, time, time, flags);
}
/* modify for begin*/
String format = "";
if((int)count == 21 && isRuLanguage()){ //判断为21并且 俄语下,使用xliff来客制化one字符串 ,其他后缀为1的类似,看需求
if(duration < HOUR_IN_MILLIS){
format = r.getString(com.android.internal.R.string.num_minutes_ago,(int)count);//在xml中增加定义
}else if(duration < DAY_IN_MILLIS){
format = r.getString(com.android.internal.R.string.num_hours_ago,(int)count);
}else{
format = r.getQuantityString(resId, (int) count);
}
}else{
format = r.getQuantityString(resId, (int) count);
}
return String.format(format, count);
}
/*判断语言 */
private static boolean isRuLanguage(){
Locale locale = Resources.getSystem().getConfiguration().locale;
String language = locale.getLanguage();
if (language.endsWith("ru"))
return true;
else
return false;
}