开发者

Change date_select values from english to spanish in rails

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, 'Abri开发者_如何学编程l'    => 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! :-)


In Rails 4:
(polish case:)

= f.datetime_select :start_time, prompt: {day: 'Dzień', month: 'Miesiąc', year: 'Rok'}

pl:
date:
    order: ["year", "month", "day"]
    month_names: ["Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień"]

it's all


in rails 5 > easiest and best scalable way

in your view:

<%= f.date_select :start_time %>

in your config/locales/en.yml add this :

en:
  date:
    order: ["day", "year",  "month"]
    month_names: ["Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "August", "September", "Oktober", "November", "December"]

Just change the month names in between the strings in whatever you want.


For rails 4.2.5.1 or higher you can use t('locale_parameter_name') instead t(:locale_parameter_name)

E.g:

: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')]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜