diff --git a/apps/transport/lib/transport_web/templates/resource/gtfs_details.html.heex b/apps/transport/lib/transport_web/templates/resource/gtfs_details.html.heex index 77afc22f78..4d8c9c88aa 100644 --- a/apps/transport/lib/transport_web/templates/resource/gtfs_details.html.heex +++ b/apps/transport/lib/transport_web/templates/resource/gtfs_details.html.heex @@ -1,6 +1,7 @@ <% is_gtfs_flex = not is_nil(@resource_history) and DB.ResourceHistory.gtfs_flex?(@resource_history) associated_geojson = get_associated_geojson(@related_files) -associated_netex = get_associated_netex(@related_files) %> +associated_netex = get_associated_netex(@related_files) +locale = get_session(@conn, :locale) %>
@@ -30,7 +31,7 @@ associated_netex = get_associated_netex(@related_files) %> ) %> <%= unless is_nil(associated_geojson) or is_nil(associated_geojson.resource_history_last_up_to_date_at) do %> <%= dgettext("validations", "This GeoJSON was up-to-date with the GTFS resource %{hours} ago.", - hours: hours_ago(associated_geojson.resource_history_last_up_to_date_at) + hours: hours_ago(associated_geojson.resource_history_last_up_to_date_at, locale) ) %> <% end %>
@@ -69,7 +70,7 @@ associated_netex = get_associated_netex(@related_files) %> <%= unless is_nil(associated_geojson.resource_history_last_up_to_date_at) do %>
<%= dgettext("validations", "Visualization up-to-date %{hours} ago.", - hours: hours_ago(associated_geojson.resource_history_last_up_to_date_at) + hours: hours_ago(associated_geojson.resource_history_last_up_to_date_at, locale) ) %>
<% end %> @@ -118,7 +119,7 @@ associated_netex = get_associated_netex(@related_files) %> date: DateTimeDisplay.format_datetime_to_paris( @validation.validation_timestamp, - get_session(@conn, :locale) + locale ), validator_url: gtfs_validator_url() ) diff --git a/apps/transport/lib/transport_web/views/resource_view.ex b/apps/transport/lib/transport_web/views/resource_view.ex index 99d77602b9..b425dd98dd 100644 --- a/apps/transport/lib/transport_web/views/resource_view.ex +++ b/apps/transport/lib/transport_web/views/resource_view.ex @@ -81,33 +81,36 @@ defmodule TransportWeb.ResourceView do def max_display_errors, do: 50 - def hours_ago(utcdatetime) do - DateTime.utc_now() |> DateTime.diff(utcdatetime) |> seconds_to_hours_minutes() + def hours_ago(utcdatetime, locale) do + DateTime.utc_now() |> DateTime.diff(utcdatetime) |> seconds_to_hours_minutes(locale) end @doc """ Converts seconds to a string showing hours and minutes. Also work for negative input, even if not intended to use it that way. - iex> seconds_to_hours_minutes(3661) - "1 h 1 min" - iex> seconds_to_hours_minutes(60) - "1 min" - iex> seconds_to_hours_minutes(30) - "0 min" - iex> seconds_to_hours_minutes(-3661) - "-1 h 1 min" + iex> seconds_to_hours_minutes(3661, :en) + "1 hour and 1 minute" + iex> seconds_to_hours_minutes(60, :en) + "1 minute" + iex> seconds_to_hours_minutes(30, :en) + "0 minute" + iex> seconds_to_hours_minutes(-3661, :en) + "-1 hour and 1 minute" """ - @spec seconds_to_hours_minutes(integer()) :: binary() - def seconds_to_hours_minutes(seconds) do - hours = div(seconds, 3600) + @spec seconds_to_hours_minutes(integer(), atom() | Cldr.LanguageTag.t()) :: binary() + def seconds_to_hours_minutes(seconds, locale \\ :en) do + duration = strip_seconds(seconds) - case hours do - 0 -> "#{div(seconds, 60)} min" - hours -> "#{hours} h #{seconds |> rem(3600) |> div(60) |> abs()} min" + cond do + duration == 0 -> "0 minute" + duration < 0 -> "-#{Shared.DateTimeDisplay.format_duration(-duration, locale)}" + true -> Shared.DateTimeDisplay.format_duration(duration, locale) end end + def strip_seconds(seconds), do: div(seconds, 60) * 60 + def download_availability_class(ratio) when ratio >= 0 and ratio <= 100 do cond do ratio == 100 -> "download_availability_100"