From 876a250c78fdf68937c5edffa2d776e1857ed3b2 Mon Sep 17 00:00:00 2001 From: nakamura Date: Tue, 18 Feb 2025 06:20:44 +0000 Subject: [PATCH] Ensure we localise time formatting This ensures consistency across public methods for `DateTime`, `Date`, and `Time` which all had slight differences. `Field::Time#time` now always goes through `I18n.localize`, previously if the `format` option wasn't specified, it bypassed `translate_localization_format` and so AM/PM was not localized. There's now an example on `Customer` so that we can see the behaviour. --- lib/administrate/field/time.rb | 9 +++--- .../app/dashboards/customer_dashboard.rb | 2 ++ spec/example_app/db/seeds.rb | 1 + spec/lib/fields/time_spec.rb | 30 +++++++++++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/administrate/field/time.rb b/lib/administrate/field/time.rb index 628987d110..ff461df09e 100644 --- a/lib/administrate/field/time.rb +++ b/lib/administrate/field/time.rb @@ -4,15 +4,16 @@ module Administrate module Field class Time < Base def time - return I18n.localize(data, format: format) if options[:format] - - data.strftime("%I:%M%p") + I18n.localize( + data, + format: format + ) end private def format - options[:format] + options.fetch(:format, "%I:%M%p") end end end diff --git a/spec/example_app/app/dashboards/customer_dashboard.rb b/spec/example_app/app/dashboards/customer_dashboard.rb index f52506b9d6..18f310604c 100644 --- a/spec/example_app/app/dashboards/customer_dashboard.rb +++ b/spec/example_app/app/dashboards/customer_dashboard.rb @@ -17,6 +17,7 @@ class CustomerDashboard < Administrate::BaseDashboard searchable_fields: ["name"], include_blank: true ), + example_time: Field::Time, password: Field::Password } @@ -28,6 +29,7 @@ class CustomerDashboard < Administrate::BaseDashboard :email_subscriber, :kind, :territory, + :example_time, :password ].freeze diff --git a/spec/example_app/db/seeds.rb b/spec/example_app/db/seeds.rb index 599021f9b4..eeef441109 100644 --- a/spec/example_app/db/seeds.rb +++ b/spec/example_app/db/seeds.rb @@ -32,6 +32,7 @@ name: name, email: Faker::Internet.email(name: name), territory: countries.sample, + example_time: Faker::Time.between(from: "00:00:00", to: "23:59:59"), password: Faker::Internet.password } end diff --git a/spec/lib/fields/time_spec.rb b/spec/lib/fields/time_spec.rb index bce4994696..6519e6b5f8 100644 --- a/spec/lib/fields/time_spec.rb +++ b/spec/lib/fields/time_spec.rb @@ -38,4 +38,34 @@ expect(field.time).to eq("04:38PM") end end + + it "formats the time with localized AM/PM markers" do + time = DateTime.new(2021, 3, 26, 16, 38) + formats = { + time: { + am: "午前", + pm: "午後" + } + } + + field = Administrate::Field::Time.new(:time, time, :index) + + I18n.with_locale(:ja) do + with_translations(:ja, formats) do + expect(field.time).to eq("04:38午後") + end + end + end + + it "returns a missing translation message if the translation is not available" do + time = DateTime.new(2021, 3, 26, 16, 38) + field = Administrate::Field::Time.new(:time, time, :index) + formats = {} + + I18n.with_locale(:ja) do + with_translations(:ja, formats) do + expect(field.time).to eq("Translation missing: ja.time.pm") + end + end + end end