综合编程

Change date_select values ​​from English to Spanish in…

微信扫一扫,分享到朋友圈

Change date_select values ​​from English to Spanish in…

I am building a site in rails and I have a date selector with a drop down menu that Rails generate automatically. The problem is that my site is in spanish and the values of the dropdown for the months are in English, is there a way to change the language to spanish?

I tried adding some lines of codes to the config/environment.rb that I found here
The code is basically this:

require 'date'
class Date
MONTHNAMES = [nil] + %w(Enero Febrero Marzo Abril Mayo Junio Julio Agosto Septiembre Octubre Noviembre Diciembre)
module Format
MONTHS = {
'Enero'  => 1, 'Febrero' => 2, 'Marzo'    => 3, 'Abril'    => 4,
'Mayo'      => 5, 'Junio'     => 6, 'Julio'     => 7, 'Agosto' => 8,
'Septiembre'=> 9, 'Octubre'  =>10, 'Noviembre' =>11, 'Diciembre'=>12
}
end
end

But nothing changed after I fired up the server again. I hope you can help me, thanks in advance.

From the documentation
:

:use_month_names - Set to an array with 12 month names if you want to customize month names. Note: You can also use Rails’ i18n functionality for this.

So you can either do this:

<%= f.date_select :date, {:use_month_names => ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre','Diciembre']} %>

Or, for bones internationalisation points, replace the strings with t()
method calls and use Rails’s I18n localisation files:

<%= f.date_select :date, {:use_month_names => [t(:jan), t(:feb), t(:mar), t(:apr), t(:may), t(:jun), t(:jul), t(:aug), t(:sep), t(:oct), t(:nov), t(:dec)]} %>

In config/locales/es.yml

es:
jan: "Enero"
feb: "Febrero"
...

And then in config/application.rb
set:

config.i18n.default_locale = :es

Bingo! :-)

Why Should You Attend Agility This Year?

上一篇

三箭齐发,爱奇艺“宅家云综”打出组合拳

下一篇

你也可能喜欢

评论已经被关闭。

插入图片

热门栏目

Change date&lowbar;select values &ZeroWidthSpace;&ZeroWidthSpace;from English to Spanish in…

长按储存图像,分享给朋友