Skip to content

Add administrate default format#2781

Open
goosys wants to merge 1 commit intothoughtbot:mainfrom
goosys:add-administrate_default-format
Open

Add administrate default format#2781
goosys wants to merge 1 commit intothoughtbot:mainfrom
goosys:add-administrate_default-format

Conversation

@goosys
Copy link
Copy Markdown
Contributor

@goosys goosys commented Feb 19, 2025

  • refs Refactor Time-related Fields ahead of upcoming improvements #2702
  • The only way to change the time format was to modify the Rails default setting. However, changing it would also affect other parts of the application outside the admin panel. Therefore, I added a dedicated format specifically for Administrate.
    • administrate_date_default administrate_datetime_default administrate_time_default
  • Currently, the Time format is fixed as "%I:%M%p", but in Japanese, we would prefer the order "%p %I:%M". I believe other languages may also have their own preferred orders. I have made it possible to change this for each language using administrate_time_default .

@goosys
Copy link
Copy Markdown
Contributor Author

goosys commented Feb 19, 2025

At this point, there is a backward compatibility issue.

Since administrate_*_default does not exist in everyone's Rails setup, some initial configuration is required to start using this feature. I feel bad about forcing this initial setup on everyone, so I’d like to discuss if there’s a better way.

  1. Write the configuration values for each language in all config/locales/administrate.*.yml files.
  2. Create config/locales/date.yml and include only the default (en) settings.
  3. Use I18n.localize with default: [:"time.formats.default"] to enable fallback. ref.

Option 1 is very tedious. Option 3 would likely work, but it introduces unnecessary processing if the initial configuration is already set up, so I’d prefer to avoid it if possible.
What do you think?

@goosys goosys force-pushed the add-administrate_default-format branch from 2995e3d to 6d0935d Compare February 19, 2025 12:56
@nickcharlton nickcharlton added the feature new functionality that’s not yet implemented label Mar 20, 2025
@nickcharlton nickcharlton added this to the Post v1 milestone Mar 20, 2025
@pablobm
Copy link
Copy Markdown
Collaborator

pablobm commented Feb 20, 2026

I think we can start looking into merging this. I have a couple of questions.

First: should the format keys live under administrate instead of the root translations file? So administrate.date.formats.default instead of date.formats.administrate_date_default.

Although I see that this will cause spec/i18n_spec.rb to fail until the key is present in all our translation files... not sure what to do about that, but I think that's an issue with how we handle translations, instead of this specific PR.

Second: of the options you give, my preferred is option 3, as it offers easy back-compatibility. When you say it involves unnecessary processing, do you mean in terms of computing performance? If that's so, I think it's not a concern here, as it would be negligible.

But perhaps both problems can be solved by sourcing the format strings from somewhere. Is there's a project out there (with a compatible license) where we can respectfully "steal" them from?

@goosys
Copy link
Copy Markdown
Contributor Author

goosys commented Mar 2, 2026

@pablobm

First: ...

Yes.

date.formats.administrate_date_default is the correct place for it.

This is because the format is used by I18n.l, not I18n.t. Since I18n.l looks up formats under date.formats, the translation needs to be defined there.

@goosys
Copy link
Copy Markdown
Contributor Author

goosys commented Mar 2, 2026

do you mean in terms of computing performance?

Yes

If that's so, I think it's not a concern here, as it would be negligible.

OK

Is there's a project out there (with a compatible license) where we can respectfully "steal" them from?

It looks like we could fall back to defaults provided by rails-i18n for date and datetime.

For time, rails-i18n does not seem to provide a default format without a date component. However, it should be possible to generate one mechanically from ruby-cldr.

https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/de.yml#L227
https://github.com/ruby-i18n/ruby-cldr


Would it be okay if I proceed with the remaining work as follows?

  1. Do not add translations for date.formats.administrate_*_default in this PR.
  2. Add the default option to I18n.l so that administrate_*_default can be used when available.

@goosys goosys force-pushed the add-administrate_default-format branch from 6d0935d to c43637a Compare March 2, 2026 13:24
end

context "with `prefix` option" do
context "with `format` option" do
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is not meant to cover the prefix option, so I changed it to use format instead.

end

def fallback_format
I18n.t("date.formats.default", default: "%Y-%m-%d")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In environments where rails-i18n is installed, formats.default should always be present, but I've added an additional fallback just in case.

@goosys
Copy link
Copy Markdown
Contributor Author

goosys commented Mar 2, 2026

@pablobm
I've rebased and made some additional changes. Please take another look.

For the fallback format, I set a simple format that doesn't rely on things like %a: date.abbr_day_names. This is only intended to prevent errors on the screen, and in normal cases it shouldn't be used if rails-i18n is installed. If the format here feels off, I'm happy to adjust it.

@goosys goosys changed the title WIP: Add administrate default format Add administrate default format Mar 3, 2026
@goosys goosys force-pushed the add-administrate_default-format branch from c43637a to 3af6d05 Compare March 3, 2026 04:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature new functionality that’s not yet implemented

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants