Localize month names - Calendar Railscasts #213 - ruby-on-rails

i'm try tutorial calendar from railscasts episode #213.
i have add es.yml but not worked.
i try localize month names with replace word on en.yml such as
en:
date:
month_names: [~, Enero, Febrero, Marzo, Abril, Mayo, Junio, Julio, Agosto, Septiembre, Octubre, Noviembre, Diciembre]
abbr_month_names: [~, Ene, Feb, Mar, Abr, May, Jun, Jul, Ago, Sep, Oct, Nov, Dic]
not working too
on html.erb
<h2 id="month"><%= #date.strftime("%B %Y") %></h2>
i want change this
anyone help me?
thank's

You should use the localize method of I18n (shortened as l):
<h2 id="month"><%= l(#date) %></h2>
Then you can set different formats on your own:
http://guides.rubyonrails.org/i18n.html#adding-date-time-formats
# config/locales/es.yml
es:
date:
formats:
short: "%B %Y"
default: "%D %m, %Y"
And use it like this:
<h2 id="month"><%= l(#date, format: :short) %></h2>

just want to clarify that if you use with active record, just simply convert the string datetime value to date object as example below.
en:
date:
formats:
default: "%Y-%m-%d"
short: "%b %d"
long: "%B %d, %Y"
enter code here
<%= l(post.the_created_at.to_date, format: :long) %>

Related

How to use locale structure for I18n.localize?

The provided locale structure in the I18n gem can look like the following:
de:
date:
abbr_day_names:
- So
- Mo
- Di
- Mi
- Do
- Fr
- Sa
But trying to output the day as described in the guides doesn't work, it seems like it looks for a format: in the locale aswell?
I18n.locale = :de
l(Date.current, format: :abbr_day_names)
"I18n::MissingTranslationData: translation missing: de.date.formats.abbr_day_names"
This is how you should do (french used):
date:
abbr_day_names: [Dim, Lun, Mar, Mer, Jeu, Ven, Sam]
abbr_month_names: [~, Jan, Fév, Mar, Avr, Mai, Jun, Jul, Août, Sep, Oct, Nov, Déc]
day_names: [Dimanche, Lundi, Mardi, Mercredi, Jeudi, Vendredi, Samedi]
formats:
day_month: "%b %d"
default: "%Y-%m-%d"
hour: "%H:%M"
long: "%A %d %B %Y"
long_month: "%d %B %Y"
month_abbr: "%d %b %Y"
So in date.abbr_day_names you define the abbreviated day names, same for date.abbr_months_names. Then you can set a custom format located in date.formats.name_of_your_format
In your view, you would use it this way:
l(Date.current, format: :long)
# OR
l(Date.current, format: :month_abbr)
# etc.
It works the same with datetime.formats and time.formats.
Here is an example of a common en-US.yml file for date/time formats: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en-US.yml
I can't find the full documentation about every wildcards usable in the i18n localization system. If somebody knows where to get it, your input will be greatly appreciated!

Rails month localize helper does not work

I want to make the translation into Russian of months.
config/locales/ru.yml
ru:
date:
formats:
default: "%d-%m-%Y"
short: "%b %d"
long: "%B %d, %Y"
day_names: [Воскресенье, Понедельник, Вторник, Среда, Четверг, Пятница, Суббота]
abbr_day_names: [Вос, Пон, Вт, Ср, Чет, Пят, Суб]
month_names: [~, Январь, Февраль, Март, Апрель, Маи, Июнь, Июль, Август, Сентябрь, Октябрь, Ноябрь, Декабрь]
abbr_month_names: [~, Янв, Фев, Мар, Апр, Маи, Июн, Июл, Авг, Сен, Окт, Ноя, Дек]
time:
formats:
default: "%d-%m-%Y %H:%M:%S"
short: "%d %B %H:%M"
long: "%B %d, %Y %H:%M"
In the view:
time now: <%= l Time.now, :format => :short %>
Return:
time now: 31 l 13:13
Current locale is "ru".
<%= I18n.locale %>
Return:
ru
Also checked the localization of days by '%a', does not work either. Why localization of months and days of not working?
Found duplicate field date in the file location. Removed, the problem persists.

Formatting Date Output Hash

The output from my hash is giving me the following date time:
16 May 11 13:12:14 +0000
How do I go about formatting this into something sensible?
Thanks
I'll assume you are wanting this output in a view... First, you can create some formats:
# /config/locales/en.yml:
en:
date:
formats:
full: "%b %d, %Y"
time:
formats:
full: "%B %d, %Y at %I:%M%p"
Then you can display a datetime in a view using the l method with specified format:
<%= l #something.updated_at, :format => :full %>
This would display something like the following:
May 16, 2011 at 01:12pm
More on the i18n formats
List of available directives

AM/PM not uppercase when using I18n.l with %p in Rails

I'm having an issue which I can't seem to figure out. I'm trying to format a date using a custom format I've defined in my en.yml file:
en:
hello: "Hello world"
time:
formats:
history_table: "%m/%d/%Y %I:%M:%S %p %Z"
This is being called using the 'l' helper:
l version.created_at, :format => :history_table
For some reason this is displaying the AM/PM in lowercase, instead of in uppercase as should be the case with %p.
I've played around in the console a bit, and it seems like it's a difference in behavior between the localization function and strftime:
ruby-1.9.2-p180 :043 > I18n.l user.updated_at, :format => "%m/%d/%Y %I:%M:%S %p %Z"
=> "03/23/2011 01:52:16 am UTC"
ruby-1.9.2-p180 :044 > user.updated_at.strftime("%m/%d/%Y %I:%M:%S %p %Z")
=> "03/23/2011 01:52:16 AM UTC"
Am I doing something wrong? Is this a bug? Any guidance is greatly appreciated as my forehead is sore from banging it against the wall.
Edit:
This has been solved(ish).
Looking at the default activesupport localization, there isn't any differentiation between %p and %P.
https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml
I over-rode the localization in my local en.yml file to use uppercase. I would really have liked to see Rails support both options however.
Looking in the source for active support, I found the following under english localization:
time:
formats:
default: "%a, %d %b %Y %H:%M:%S %z"
short: "%d %b %H:%M"
long: "%B %d, %Y %H:%M"
am: "am"
pm: "pm"
In other words, there is no distinction between %P and %p built-in to localization as there is in strftime. This unfortunately means that in individual custom formats it is not possible to choose between upper and lower case representations, but it is possible to define which you would like globally by over-riding the default formats in your own en.yml file. For example, here's my updated en.yml file that now causes the output of upper-case AM/PM.
en:
hello: "Hello world"
time:
formats:
history_table: "%m/%d/%Y %I:%M:%S %p %Z"
am: "AM"
pm: "PM"

Rails: How to make Date strftime aware of the default locale?

I have my default locale set in the environment.rb as de (German).
I also see all the error messages in German, so the locale is picked up by the server. But when I try to print date with strftime like following:
some_date.strftime('%B, %y')
It prints in English (January, 11), and not the expected German (Januar, 11).
How can I print the date according to the default locale?
Use the l (alias for localize) method instead of raw strftime, like this:
l(date, format: '%B %d, in the year %Y')
See here for more information.
You can also define 'named' formats, a couple of them (short, long) are already predefined.
you can also make it shorter:
l(some_date, :format => '%d %B %Y')
In es.yml put:
es:
date:
formats:
default: "%d / %m / %Y"
In index.html.erb put:
<%= l somemodel.datefield %>

Resources