ember-intl icon indicating copy to clipboard operation
ember-intl copied to clipboard

Default formatter options

Open chrisvdp opened this issue 4 years ago • 8 comments

Hello 👋

We are currently migrating from using ember-moment usage of moment-format to the ember-intl usage of format-date. Previously we could globally set our desired date format via config.

moment: {
      outputFormat: 'lll'
}

Is there any way to set a default format, or do we have to pass the option at each invocation of the {{format-date}}?

chrisvdp avatar Nov 04 '20 17:11 chrisvdp

Great suggestion! I wanted something like this a few times myself as well!

I think we'd need to add it here:

https://github.com/ember-intl/ember-intl/blob/795239a157bef5794ae45c6c12385b7d09f11350/addon/-private/formatters/-base.ts#L78-L91

For instance, we could allow a special named format, e.g. default, that would be tried, if no / empty formatOptions are passed.

The default format would be added in the formats.js config file then:

https://github.com/ember-intl/ember-intl/blob/795239a157bef5794ae45c6c12385b7d09f11350/blueprints/ember-intl/files/app/formats.js#L1-L15

For instance:

export default { 
  time: { 
    default: {
      hour: 'numeric', 
      minute: 'numeric',
    },
    hhmmss: { 
      hour: 'numeric', 
      minute: 'numeric', 
      second: 'numeric', 
    }, 
  },
  // ...
};
default format
{{format-time someTime}} -> 23:30

explicit options (prevents falling back to default at all)
{{format-time someTime hour="numeric"}} -> 23

some named format
{{format-time someTime format="hhmmss"}} -> 23:30:15

explicitly the default format
{{format-time someTime format="default"}} -> 23:30

explicitly the default format, so you can combine it with other options
{{format-time someTime format="default" hourCycle="h12"}} -> 11:30

Mayyyyybeee we'll also wanna be able to specify a base format, that will always be applied first, even if explicit options are provided.

One issue I see with putting this into the formats.js file (which kind of is an issue of the formats.js anyway) is that the formats cannot change per locale. I could imagine that you'd for example want to use hourCycle="h12" for en-US, but stick to h24 for other locales.

What are your thoughts on this? :)

buschtoens avatar Nov 04 '20 18:11 buschtoens

We were looking for a system-wide default, so we wouldn't want to set the date format via format="default", as omitting the format argument should result in the default format anyway.

Ideally, we would want a way to set the default date format from within environment.js. We might suggest something like this:

ENV.intl.dateFormats = {
  default: 'hhmmss',
  // other known formats
};

Ultimately, we opted to create our own Helper which has the same name FormatDateHelper, thus overriding the ember-intl one.

srsgores avatar Nov 04 '20 23:11 srsgores

@srsgores

We were looking for a system-wide default, so we wouldn't want to set the date format via format="default", as omitting the format argument should result in the default format anyway.

I think you may have misread my comment. This is basically what I am suggesting.

Invoking a format helper without an explicit format param would implicitly lookup a format named default and use it, if present.

The key difference to your ENV.intl.dateFormats config is that my proposal would still place the default format(s) in the standard formats.js file alongside all other named formats.

The hbs code block in my comment attempts to showcase the different ways helpers could be called and when / how the default format kicks in.

Let me know, if this is still unclear and I'll try to explain it differently then. :)

buschtoens avatar Nov 04 '20 23:11 buschtoens

I like the idea of a default in the formats.js file. I was honestly unaware of the file until reading this comment. Our use case is currently to have one default format across all locales, but I can see how that would be useful.

chrisvdp avatar Nov 05 '20 16:11 chrisvdp

Re-reading your comment again. I think the only suggestion I would make would be to make default a top-level object with time, date, etc within it to avoid a breaking change for anyone who currently has a format called default.

chrisvdp avatar Nov 05 '20 16:11 chrisvdp

I've submitted a first draft PR for this feature: #1463

What do y'all think about this API?

https://github.com/ember-intl/ember-intl/blob/7e522d3ed97eaa224ea8ea9b15a8425db5835a4b/blueprints/ember-intl/files/app/formats.js#L1-L64

https://github.com/ember-intl/ember-intl/blob/7e522d3ed97eaa224ea8ea9b15a8425db5835a4b/tests/unit/helpers/format-options-merging-test.ts#L11-L77

buschtoens avatar Nov 05 '20 22:11 buschtoens

The gist is that base options are always applied, and the default is only applied when no format is provided?

chrisvdp avatar Nov 05 '20 22:11 chrisvdp

Exactly. base options are overridden by named format options. Named format options are overridden by known options passed in the invocation.

defaults are only applied, when neither a named format nor any known options are passed in the invocation. The defaults are applied on top of the base options.

buschtoens avatar Nov 05 '20 23:11 buschtoens

Closing this issue, as I won't be pursuing the feature at the moment and want to keep the maintenance cost low. If I understood the original issue correctly, we can create a helper that wraps {{format-date}} and applies options as needed.

ijlee2 avatar Mar 04 '24 19:03 ijlee2