I made an initializer named: 'date_formats.rb' and it looks like:
Date::DATE_FORMATS[:default] = "%B %e, %Y" # November 3, 2013
DateTime::DATE_FORMATS[:default] = "%B %e, %Y" # November 3, 2013 14:22
Time::DATE_FORMATS[:default] = "%B %d, %Y %H:%M" # November 3, 2013 14:22
So when i call somemodel.created_at
i got something like: December 16, 2014 12:37
. I love this because i dont have to struggle with strftime
anymore
The problem begins when i need this date to print in spanish. So far i have installed rails-18n gem, and because i have to translate some custom texts i have already an es.yml that looks like this:
es:
hello: "Hello world"
Client: "Cliente"
update: "Actualización"
create: "Creación"
identification_number: "Número de identificación"
created_by: "Creado por"
identification_type: "Tipo de identificación"
name: "Nombre"
observations: "Observaciones"
status: "Estado"
client_type: "Tipo de cliente"
contract_start_date: "Fecha de inicio de contrato"
contract_end_date: "Fecha de fin de contrato"
contact_name: "Nombre del contacto"
telephone: "Teléfono"
email: "Email"
address: "Dirección"
created_by_id: "Id creador"
date:
order:
- :day
- :month
- :year
abbr_day_names:
- dom
- lun
- mar
- mié
- jue
- vie
- sáb
abbr_month_names:
- ~
- ene
- feb
- mar
- abr
- may
- jun
- jul
- ago
- sep
- oct
- nov
- dic
day_names:
- domingo
- lunes
- martes
- miércoles
- jueves
- viernes
- sábado
month_names:
- ~
- enero
- febrero
- marzo
- abril
- mayo
- junio
- julio
- agosto
- septiembre
- octubre
- noviembre
- diciembre
formats:
short: "%d de %b"
default: "%d/%m/%Y"
long: "%A, %d de %B de %Y"
time:
formats:
short: "%d de %b a las %H:%M hrs"
default: "%a, %d de %b de %Y a las %H:%M:%S %Z"
long: "%A, %d de %B de %Y a las %I:%M %p"
am: "am"
pm: "pm"
It doesnt translate my dates. It only works if i call it like this l somemodel.created_at
but i dont want to call the method localize everytime i need to display a date.
So far my application.rb looks like this:
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.i18n.available_locales = ['es', :es]
config.i18n.default_locale = :es
I18n.locale = config.i18n.locale = config.i18n.default_locale
Note that i have tried a lot of workarounds to be sure it is loading the correct .yml, and i have made sure it is.
But the localize
or short l
method is the method that does the trick. You cannot get rid of it. Without that method the I18n
module is not called at all.
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.