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)) {

View File

@@ -927,8 +927,16 @@
<string name="DateUtils_tomorrow">Tomorrow</string>
<!-- Used in the context: Tonight at 9:00pm for example. Specifically this is after 7pm -->
<string name="DateUtils_tonight">Tonight</string>
<!-- Used when showing the time a device was linked. %1$s is replaced with the date while %2$s is replaced with the time. e.g. Jan 15 at 9:00pm -->
<string name="DateUtils_date_at">%1$s at %2$s</string>
<!-- Used when showing the time a device was linked. %1$s is replaced with the date while %2$s is replaced with the time. e.g. on Jan 15 at 9:00pm -->
<plurals name="DateUtils_date_time_at">
<item quantity="one">on %1$s at %2$s</item>
<item quantity="other">on %1$s at %2$s</item>
</plurals>
<!-- Used when showing the time a device was linked. e.g. at 9:00pm -->
<plurals name="DateUtils_time_at">
<item quantity="one">at %1$s</item>
<item quantity="other">at %1$s</item>
</plurals>
<!-- Scheduled Messages -->
<!-- Title for dialog that shows all the users scheduled messages for a chat -->
@@ -1037,7 +1045,7 @@
<!-- Dialog title shown when a device is unlinked -->
<string name="LinkDeviceFragment__device_unlinked">Device unlinked</string>
<!-- Dialog body shown when a device is unlinked where %1$s is the date and time the device was originally linked (eg Jan 15 at 9:00pm) -->
<string name="LinkDeviceFragment__the_device_that_was">The device that was linked on %1$s is no longer linked.</string>
<string name="LinkDeviceFragment__the_device_that_was">The device that was linked %1$s is no longer linked.</string>
<!-- Button to dismiss dialog -->
<string name="LinkDeviceFragment__ok">OK</string>
@@ -1088,7 +1096,7 @@
<!-- Title of notification telling users that a device was linked to their account -->
<string name="NewLinkedDeviceNotification__you_linked_new_device">You linked a new device</string>
<!-- Message body of notification telling users that a device was linked to their account. %1$s is the time it was linked -->
<string name="NewLinkedDeviceNotification__a_new_device_was_linked">A new device was linked to your account at %1$s. Tap to view.</string>
<string name="NewLinkedDeviceNotification__a_new_device_was_linked">A new device was linked to your account %1$s. Tap to view.</string>
<!-- DeviceListActivity -->
<string name="DeviceListActivity_unlink_s">Unlink \"%s\"?</string>
@@ -1684,7 +1692,7 @@
<string name="Megaphones_add_a_profile_photo">Add a profile photo</string>
<!-- Message body of megaphone telling users that a device was linked to their account. %1$s is the time it was linked -->
<string name="NewLinkedDeviceMegaphone__a_new_device_was_linked">A new device was linked to your account at %1$s.</string>
<string name="NewLinkedDeviceMegaphone__a_new_device_was_linked">A new device was linked to your account %1$s.</string>
<!-- Button shown on megaphone that will redirect to the linked devices screen -->
<string name="NewLinkedDeviceMegaphone__view_device">View device</string>
<!-- Button shown on megaphone to acknowledge and dismiss the megaphone -->