twitter / twitter-cldr-rb

Ruby implementation of the ICU (International Components for Unicode) that uses the Common Locale Data Repository to format dates, plurals, and more.
Apache License 2.0
669 stars 93 forks source link

Convert CLDR Date/Time pattern to Ruby-compatible pattern #271

Closed adamdebono closed 7 months ago

adamdebono commented 7 months ago

I'm wondering if there is an easy way to pass the CLDR library one of its datetime patterns, and have it return a string that could be passed to strftime. For example:

TwitterCldr::Formatters::DateTimeFormatter.pattern_to_ruby('yMd') # => '%m/%d/%y'

I currently have a set of date formats across various locale YAML files that I am using with I18n, as is standard in Rails apps and libraries. Essentially what I would like to be able to do is automatically generate these files instead of needing to manually maintain them.

I had a quick look through the source code and there doesn't appear to be anything like this currently, but I'm happy to be proven wrong. Based on this spec I assume there could be some sort of mapping created, e.g. { 'MM' => '%m', ... }, that could then be used in a find-and-replace method.

camertron commented 7 months ago

Hey @adamdebono, thanks for asking about this 😄

Unfortunately the library doesn't have this functionality right now. Any attempt at adding it would have to be best-effort too, since CLDR offers some formatting options that Ruby does not. You could probably get ~90% of the way there. Ruby's strftime also doesn't have the ability to produce i18n-aware strings, i.e. it won't know to emit "lunes" in Spanish instead of "Monday."

If you're interested in implementing this, it's probably best to distribute it in a separate gem.

adamdebono commented 7 months ago

Yep I thought that might be the case, thanks for the response.

While strftime won't produce the locale-aware strings, I'm pretty i18n-rails will? Which for my purposes is fine.

Since it's pretty specific for my project I might implement this internally for now. If I can get it working well enough it could be extracted out into a gem.