2011-06-25 69 views
1

我正在建立一個網站的軌道,我有一個日期選擇器與Rails自動生成的下拉菜單。問題是我的網站是西班牙文版本,而且這些月份的下拉菜單的價值是英文的,有沒有辦法將語言改爲西班牙語?更改日期選擇從英語到西班牙語在軌道的值

我嘗試添加代碼的某些行到config/environment.rb中,我發現here 的代碼基本上是這樣的:

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 

但什麼都沒有改變後我又發射了服務器。 我希望你能幫助我,在此先感謝。

回答

7

documentation

:use_month_names - 如果你想定製月份名稱設置爲12個月名稱的數組。注意:您也可以使用Rails的i18n功能。

所以,你可以這樣做:

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

或者,骨骼國際百分點,t()方法調用替換字符串和使用Rails的的的I18n本地化文件:

<%= 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)]} %> 

config/locales/es.yml

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

然後在config/application.rb集:

config.i18n.default_locale = :es 

賓果! :-)

+0

完美!非常感謝 – marimaf

1

在Rails 4:
(波蘭的情況:)

= 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ń"] 

這一切都

0

爲Rails 4.2.5.1或更高你可以使用T( 'locale_parameter_name'),而不是T(:locale_parameter_name)

例如:

: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')] 
1
在導軌5

>最簡單,最好的可擴展的方式

在你看來

<%= f.date_select :start_time %> 
config/locales/en.yml

補充一點:

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

剛剛更改的月份名稱任何你想要的字符串。

相關問題