Pluralize time strings.

This commit is contained in:
Michelle Tang
2025-01-24 10:46:46 -05:00
committed by Greyson Parrelli
parent 7542614580
commit 83aee4a084
4 changed files with 45 additions and 10 deletions

View File

@@ -74,7 +74,7 @@ class NewLinkedDeviceNotificationJob private constructor(
val builder = NotificationCompat.Builder(context, NotificationChannels.getInstance().NEW_LINKED_DEVICE)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getString(R.string.NewLinkedDeviceNotification__you_linked_new_device))
.setContentText(context.getString(R.string.NewLinkedDeviceNotification__a_new_device_was_linked, DateUtils.getOnlyTimeString(context, data.deviceCreatedAt)))
.setContentText(context.getString(R.string.NewLinkedDeviceNotification__a_new_device_was_linked, DateUtils.getOnlyTimeAtString(context, data.deviceCreatedAt)))
.setContentIntent(pendingIntent)
ServiceUtil.getNotificationManager(context).notify(NotificationIds.NEW_LINKED_DEVICE, builder.build())

View File

@@ -299,7 +299,7 @@ public final class Megaphones {
}
private static @NonNull Megaphone buildNewLinkedDeviceMegaphone(@NonNull Context context) {
String createdAt = DateUtils.getOnlyTimeString(context, SignalStore.misc().getNewLinkedDeviceCreatedTime());
String createdAt = DateUtils.getOnlyTimeAtString(context, SignalStore.misc().getNewLinkedDeviceCreatedTime());
return new Megaphone.Builder(Event.NEW_LINKED_DEVICE, Megaphone.Style.BASIC)
.setTitle(R.string.NewLinkedDeviceNotification__you_linked_new_device)
.setBody(context.getString(R.string.NewLinkedDeviceMegaphone__a_new_device_was_linked, createdAt))

View File

@@ -26,6 +26,7 @@ import org.thoughtcrime.securesms.conversation.v2.computed.FormattedDate
import java.text.DateFormatSymbols
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale
import java.util.concurrent.TimeUnit
@@ -182,14 +183,30 @@ object DateUtils : android.text.format.DateUtils() {
}
/**
* Formats the timestamp as a date, without the year, followed by the time
* eg. Jan 15 at 9:00pm
* Given a timestamp, formats as "at time".
* Pluralization allows for Romance languages to be translated correctly
* eg. at 7:23pm, at 13:20
*/
@JvmStatic
fun getOnlyTimeAtString(context: Context, timestamp: Long): String {
val time = timestamp.toLocalTime().formatHours(context)
val hour = getHour(context, timestamp)
return context.resources.getQuantityString(R.plurals.DateUtils_time_at, hour, time)
}
/**
* Formats the timestamp as a date, without the year, followed by the time.
* Pluralization allows for Romance languages to be translated correctly
* eg. on Jan 15 at 9:00pm
*/
@JvmStatic
fun getDateTimeString(context: Context, locale: Locale, timestamp: Long): String {
val date = timestamp.toDateString("MMM d", locale)
val time = timestamp.toLocalTime().formatHours(context)
return context.getString(R.string.DateUtils_date_at, date, time)
val hour = getHour(context, timestamp)
return context.resources.getQuantityString(R.plurals.DateUtils_date_time_at, hour, date, time)
}
/**
@@ -363,6 +380,16 @@ object DateUtils : android.text.format.DateUtils() {
return isToday(time + TimeUnit.DAYS.toMillis(1))
}
private fun getHour(context: Context, timestamp: Long): Int {
val cal = Calendar.getInstance(Locale.getDefault())
cal.timeInMillis = timestamp
return if (context.is24HourFormat()) {
cal[Calendar.HOUR_OF_DAY]
} else {
cal[Calendar.HOUR]
}
}
private fun Context.is24HourFormat(): Boolean {
is24HourDateCache?.let {
if (it.lastUpdated.isWithin(10.seconds)) {