如何在 Android 上创建本地化的时间前字符串 [英] How to create a localized time ago String on Android

查看:18
本文介绍了如何在 Android 上创建本地化的时间前字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看 Google I/O Session 2012 app 并遇到了这个 TODO

I was reviewing the Google I/O Session 2012 app and came across this 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";
    }
}

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

Which made me wonder what would be the steps to localizing it.

推荐答案

来自 Ian Lake 的回答:

From Ian Lake's answer:

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

DateUtils.getRelativeTimeSpanString 产生如下内容:

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

或者,这将允许使用给定的 Context 通过 Android res 框架提供翻译,如果您想要的消息与您可以从框架方法.

Alternatively, this would allow translations to be provided through the Android res framework using the given Context, useful if your desired messaging difference from what you can get out of the framework method.

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;

那么剩下的工作就是复数结构和字符串资源了.

Then the rest of the work is in the structure of the plurals and string resources.

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

My default local is en, so 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 框架对复数本地化的支持.剩下的就是提供翻译.

This approach should allow you to localize on the vocabulary and the grammar of the language, as different languages not only have different words for words like "minute", but they also have different rules about how to pluralize words based on the quantity. So the plurals resource utilizes the Android frameworks support of localizing on pluralizations. All that is left is to provide the translations.

这篇关于如何在 Android 上创建本地化的时间前字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆