2016-07-14 6 views
0

나는 레일 국제화를 사용하고 내가 (https://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml#L15_ 여기에 문서에서 언급 한 바와 같이) : 개월 동안 무기 호를 입력해야 것으로 나타났습니다Rails 현지화 달 및 일 배열에 nil 항목이있는 이유는 무엇입니까? 이 같은 :

0 번째 달 같은 것은 없기 때문에
month_names: [~, January, February, March, April, May, June, July, August, September, October, November, December] 

.

왜 이것이 중요한 이유입니까? 첫 번째 요소에 대해 1 월이 반환되지 않는 이유는 무엇입니까? 이게 어떻게 작동합니까?

답변

1

자연 개월 번호 대신 0 전형적인 배열과 같은 기반 인의 기반 1을하기 때문입니다. 이를 제공하고 필요할 때마다 인덱스 계산을 수행하는 것을 잊지 않도록 월 배열은 zero 위치의 추가 요소로 정의됩니다.

가 사용되는 방법의 예를 들어 date_helper code에서보세요 :

# Looks up month names by number (1-based): 
    # 
    # month_name(1) # => "January" 
    # 
    # If the <tt>:use_month_numbers</tt> option is passed: 
    # 
    # month_name(1) # => 1 
    # 
    # If the <tt>:use_two_month_numbers</tt> option is passed: 
    # 
    # month_name(1) # => '01' 
    # 
    # If the <tt>:add_month_numbers</tt> option is passed: 
    # 
    # month_name(1) # => "1 - January" 
    # 
    # If the <tt>:month_format_string</tt> option is passed: 
    # 
    # month_name(1) # => "January (01)" 
    # 
    # depending on the format string. 
    def month_name(number) 
     if @options[:use_month_numbers] 
     number 
     elsif @options[:use_two_digit_numbers] 
     '%02d' % number 
     elsif @options[:add_month_numbers] 
     "#{number} - #{month_names[number]}" 
     elsif format_string = @options[:month_format_string] 
     format_string % {number: number, name: month_names[number]} 
     else 
     month_names[number] 
     end 
    end 
1

배열 인덱스가 올바른 달과 일치하도록하여 스텁을 앞에 붙이기를 원할 것입니다.

months[12] = December