10

我正在查看Google I/O Session 2012 应用程序并遇到了这个 TODO

public static String getTimeAgo(long time, Context ctx) {
    if (time < 1000000000000L) {
        // if timestamp given in seconds, convert to millis
        time *= 1000;
    }

    long now = getCurrentTime(ctx);
    if (time > now || time <= 0) {
        return null;
    }

    // TODO: localize
    final long diff = now - time;
    if (diff < MINUTE_MILLIS) {
        return "just now";
    } else if (diff < 2 * MINUTE_MILLIS) {
        return "a minute ago";
    } else if (diff < 50 * MINUTE_MILLIS) {
        return diff / MINUTE_MILLIS + " minutes ago";
    } else if (diff < 90 * MINUTE_MILLIS) {
        return "an hour ago";
    } else if (diff < 24 * HOUR_MILLIS) {
        return diff / HOUR_MILLIS + " hours ago";
    } else if (diff < 48 * HOUR_MILLIS) {
        return "yesterday";
    } else {
        return diff / DAY_MILLIS + " days ago";
    }
}

这让我想知道本地化它的步骤是什么。

4

4 回答 4

21

如果您不需要自定义字符串,您还可以使用DateUtils.getRelativeTimeSpanString,它会生成如下字符串:

  • 42 分钟前
  • 10 秒前
  • 1小时内

示例用法:

final CharSequence relativeTimeSpan = DateUtils.getRelativeTimeSpanString(time, now, 0);
于 2012-11-02T21:53:04.073 回答
10

从伊恩湖的回答:

final CharSequence relativeTimeSpan = DateUtils.getRelativeTimeSpanString(time, now, 0);

DateUtils.getRelativeTimeSpanString产生如下内容:

  • 42 分钟前
  • 10 秒前
  • 1小时内

或者,这将允许res使用给定的 Android 框架提供翻译Context,如果您想要的消息传递与您可以从框架方法中得到的不同,这将很有用。

public static String getTimeAgo(long time, Context context) {
    if (time < 1000000000000L)
        // if timestamp given in seconds, convert to millis
        time *= 1000;

    final long now = getCurrentTime(context);
    if (time > now || time <= 0) return "";


    final Resources res = context.getResources();
    final long time_difference = now - time;
    if (time_difference < _A_MINUTE)
        return res.getString(R.string.just_now);
    else if (time_difference < 50 * _A_MINUTE)
        return res.getString(R.string.time_ago,
                             res.getQuantityString(R.plurals.minutes, (int) time_difference / _A_MINUTE, time_difference / _A_MINUTE));
    else if (time_difference < 24 * _AN_HOUR)
        return res.getString(R.string.time_ago,
                             res.getQuantityString(R.plurals.hours, (int) time_difference / _AN_HOUR, time_difference / _AN_HOUR));
    else if (time_difference < 48 * _AN_HOUR)
        return res.getString(R.string.yesterday);
    else
        return res.getString(R.string.time_ago,
                             res.getQuantityString(R.plurals.days, (int) time_difference / _A_DAY, time_difference / _A_DAY));
}

我将常量定义为:

/** One second (in milliseconds) */
private static final int _A_SECOND = 1000;
/** One minute (in milliseconds) */
private static final int _A_MINUTE = 60 * _A_SECOND;
/** One hour (in milliseconds) */
private static final int _AN_HOUR = 60 * _A_MINUTE;
/** One day (in milliseconds) */
private static final int _A_DAY = 24 * _AN_HOUR;

然后剩下的工作就是复数和字符串资源的结构。

我的默认本地是 en,所以res/values/strings.xml

<!-- time ago strings -->
<string name="just_now">just now</string>
<string name="time_ago">%s ago</string>
<string name="yesterday">yesterday</string>

然后res/values/plurals.xml

<plurals name="minutes">
    <item quantity="one">a minute</item>
    <item quantity="other">%d minutes</item>
</plurals>

<plurals name="hours">
    <item quantity="one">an hour</item>
    <item quantity="other">%d hours</item>
</plurals>

<plurals name="days">
    <item quantity="one">a day</item>
    <item quantity="other">%d days</item>
</plurals>

这种方法应该允许您对语言的词汇和语法进行本地化,因为不同的语言不仅对“分钟”之类的词有不同的词,而且对于如何根据数量使词复数也有不同的规则。因此复数资源利用了 Android 框架对复数本地化的支持。剩下的就是提供翻译。

于 2012-11-02T21:04:13.597 回答
1

这个答案类似于 Dandre 的答案,除了:

  • 昨天是根据日历时间而不是秒来定义的。
    • 例如,当:
      • 发布时间为2015 年 1 月 19 日晚上 10:00:00
      • 当前时间是01:00:00 am 01/20/2015
    • 格式化日期应打印:昨天而不是3 小时前

首先,让我们编写返回格式化日期的主函数:

public String getStringFormattedDate(Context context, Date postDate){
    Calendar postCalendar = Calendar.getInstance();
    postCalendar.setTime(postDate);
    Calendar currentCalendar = Calendar.getInstance();

    long delta = Math.abs(currentCalendar.getTimeInMillis() - postCalendar .getTimeInMillis()) / 1000;

    //**yesterday**
    //This has to be checked first, otherwise, yesterday logic 
    //will be overridden by "seconds ago", "minutes ago", "hours ago"
    if(isYesterday(postCalendar , currentCalendar)){
        return context.getResources().getString(R.string.yesterday);
    }
    //seconds ago
    else if (delta < 60)
    {
        return context.getResources().getQuantityString(R.plurals.seconds, (int)delta, (int)delta);
    }
    //minutes ago
    else if (delta < 3600) // 60 * 60
    {
        delta /= 60.0; // convert seconds to minutes
        return context.getResources().getQuantityString(R.plurals.minutes, (int)delta, (int)delta);
    }
    //hours ago
    else if (delta < 86400) // 24 * 60 * 60
    {
        delta /= (60.0*60.0); // convert seconds to hours
        return context.getResources().getQuantityString(R.plurals.hours, (int)delta, (int)delta);
    }
    //older dates
    else{
       delta /= (60.0*60.0*24.0); // convert seconds to days
       return context.getResources().getString(R.string.time_ago, context.getResources().getQuantityString(R.plurals.days, (int) delta , (int) delta));
    }
}

isYesterday()检查 postCalendar 与当前日历相比是否为昨天的函数:

//return true, if postCalendar is a day behind currentCalendar
private boolean isYesterday(Calendar postCalendar, Calendar currentCalendar){
    Calendar cloneCurrentCalendar = (Calendar) currentCalendar.clone();
    resetTime(cloneCurrentCalendar);

    Calendar clonePostCalendar = (Calendar) postCalendar.clone();
    resetTime(clonePostCalendar);
    clonePostCalendar.add(Calendar.DAY_OF_MONTH, 1);

    return (cloneCurrentCalendar.compareTo(clonePostCalendar) == 0);
}

resetTime()重置日历实例仅用于比较日历日期的函数:

//set Time to 00:00:00
private void resetTime(Calendar calendar){
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
}
于 2015-10-22T17:41:14.673 回答
0

我在答案中稍微编辑了代码。我得到了“0 小时前”字符串,这是由 long 和 int 的除法引起的。此外,getCurrentTime()答案中缺少方法,因此我使用了System.getCurrentTimeInMillis(). 这是我解决0 hour ago问题的修改。

public static String getTimeAgo(long time, Context context) {
    if (time < 1000000000000L) {
        // if timestamp given in seconds, convert to millis
        time *= 1000;
    }

    final long now = System.currentTimeMillis();
    if (time > now || time <= 0) return "";

    final Resources res = context.getResources();
    final long time_difference = now - time;

    if (time_difference < C._A_MINUTE)
        return res.getString(R.string.just_now);
    else if (time_difference < 50 * C._A_MINUTE)
        return res.getString(R.string.time_ago,
            res.getQuantityString(R.plurals.minutes, (int) time_difference / C._A_MINUTE, time_difference / C._A_MINUTE));
    else if (time_difference < 24 * C._AN_HOUR)
        return res.getString(R.string.time_ago,
            res.getQuantityString(R.plurals.hours, (int) Math.ceil((double)time_difference / C._AN_HOUR), time_difference / C._AN_HOUR));
    else if (time_difference < 48 * C._AN_HOUR)
        return res.getString(R.string.yesterday);
    else
        return res.getString(R.string.time_ago,
            res.getQuantityString(R.plurals.days, (int) Math.ceil((double)time_difference / C._A_DAY), time_difference / C._A_DAY));
}

C.java是我保持常量的地方。这里是:

public class C {
    /** One second (in milliseconds) */
    public static final int _A_SECOND = 1000;
    /** One minute (in milliseconds) */
    public static final int _A_MINUTE = 60 * _A_SECOND;
    /** One hour (in milliseconds) */
    public static final int _AN_HOUR = 60 * _A_MINUTE;
    /** One day (in milliseconds) */
    public static final int _A_DAY = 24 * _AN_HOUR;

其他文件 (plurals.xmlstrings.xml) 相同。请看上面的答案。

于 2015-06-14T16:26:29.647 回答