From d55181ccd0ec1f92e9f0d9c57fd3f85fa3f1fb69 Mon Sep 17 00:00:00 2001 From: Vavassor Date: Fri, 14 Jul 2017 19:45:26 -0400 Subject: [PATCH] Adds ability to localize timestamps. --- .../tusky/adapter/StatusViewHolder.java | 2 +- .../keylesspalace/tusky/util/DateUtils.java | 44 ++++++++++++++----- app/src/main/res/values/strings.xml | 14 +++++- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java index c532dcbd0..af5833b12 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/StatusViewHolder.java @@ -169,7 +169,7 @@ public class StatusViewHolder extends RecyclerView.ViewHolder { if (createdAt != null) { long then = createdAt.getTime(); long now = new Date().getTime(); - readout = DateUtils.getRelativeTimeSpanString(then, now); + readout = DateUtils.getRelativeTimeSpanString(sinceCreated.getContext(), then, now); readoutAloud = android.text.format.DateUtils.getRelativeTimeSpanString(then, now, android.text.format.DateUtils.SECOND_IN_MILLIS, android.text.format.DateUtils.FORMAT_ABBREV_RELATIVE); diff --git a/app/src/main/java/com/keylesspalace/tusky/util/DateUtils.java b/app/src/main/java/com/keylesspalace/tusky/util/DateUtils.java index 7142b5fd2..beb58ef9b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/DateUtils.java +++ b/app/src/main/java/com/keylesspalace/tusky/util/DateUtils.java @@ -15,38 +15,60 @@ package com.keylesspalace.tusky.util; -import java.text.NumberFormat; +import android.content.Context; + +import com.keylesspalace.tusky.R; public class DateUtils { /* This is a rough duplicate of android.text.format.DateUtils.getRelativeTimeSpanString, * but even with the FORMAT_ABBREV_RELATIVE flag it wasn't abbreviating enough. */ - public static String getRelativeTimeSpanString(long then, long now) { + public static String getRelativeTimeSpanString(Context context, long then, long now) { final long MINUTE = 60; final long HOUR = 60 * MINUTE; final long DAY = 24 * HOUR; final long YEAR = 365 * DAY; long span = (now - then) / 1000; - String prefix = ""; + boolean future = false; if (span < 0) { - prefix = "in "; + future = true; span = -span; } - String unit; + String format; if (span < MINUTE) { - unit = "s"; + if (future) { + format = context.getString(R.string.abbreviated_in_seconds); + } else { + format = context.getString(R.string.abbreviated_seconds_ago); + } } else if (span < HOUR) { span /= MINUTE; - unit = "m"; + if (future) { + format = context.getString(R.string.abbreviated_in_minutes); + } else { + format = context.getString(R.string.abbreviated_minutes_ago); + } } else if (span < DAY) { span /= HOUR; - unit = "h"; + if (future) { + format = context.getString(R.string.abbreviated_in_hours); + } else { + format = context.getString(R.string.abbreviated_hours_ago); + } } else if (span < YEAR) { span /= DAY; - unit = "d"; + if (future) { + format = context.getString(R.string.abbreviated_in_days); + } else { + format = context.getString(R.string.abbreviated_days_ago); + } } else { span /= YEAR; - unit = "y"; + if (future) { + format = context.getString(R.string.abbreviated_in_years); + } else { + format = context.getString(R.string.abbreviated_years_ago); + } } - return prefix + NumberFormat.getIntegerInstance().format(span) + unit; + return String.format(format, span); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d1942c695..68bf67826 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -197,6 +197,18 @@ %1$s %2$s no content - Toot saved ! + Toot saved! + + + in %dy + in %dd + in %dh + in %dm + in %ds + %dy + %dd + %dh + %dm + %ds