简体   繁体   中英

Different date format and one locale

I use I18n.t('date.formats.default') for date formatting.

The issue is that in different countries there are different date formats , but one english locale .

For example ' %m.%d.%Y ' fo US and ' %d.%m.%Y ' for Australia

I need the ideas how to handle with it.

While you might simply use something else for date formats, the easiest drop-in solution would be to store all possible variants in the same string and on retrieval do (assuming the country code is known):

'date.formats.default': 'US[%m.%d.%Y],AU[%d.%m.%Y]'

code = 'AU'
format = I18n.t('date.formats.default')
format[/(?<=#{code}\[).*?(?=\])/] || format
#⇒ "%d.%m.%Y"

The latter || format || format is needed to support normal format, without brackets.


If you don't like regular expressions, store the JSON there, containing hash {CODE => FORMAT} , parse it and retrieve the value.

I think it is more convenient way to use different locales.

For example en-AU.yml , en-US.yml , en-CA.yml etc.? Especially i18n supports this.

Australia has different time format too.

Every time you have to take into account all these nuances for each country.

Using different locales simplifies this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM