Ruby on Rails I18n - Localization of Dates Works I

2019-09-12 23:45发布

问题:

I am using Ruby on Rails 3.2.13 Ruby 1.9.3.

I have the following code in my en.yml file:

date:
  formats:
    default: "%Y-%m-%d"
    short: "%b %d"
    long: "%A, %B %d, %Y"
    mmyy: "%B %Y"

I have the following code in my fr.yml file:

date:
  formats:
    default: "%Y-%m-%d"
    short: "%b %d"
    long: "%A, %d %B %Y"
    mmyy: "%B %Y"

My date field media_created is stored in a string in YYYY-MM-DD format

Here is the code in my view to display the short date format in my view:

<%= l media_item.media_created.to_date, format: :mmyy %>

Here is an example of how my short format works in localhost (date 2013-07-01):

July 2013 (en)
juillet 2013 (fr)

Here is an example of how my long format works in production (date 2013-07-01):

July 2013 (en)
July 2013 (fr)

Here is the code in my view to display the long date format in my view:

<%= l media_item.media_created.to_date, format: :long %>

Here is an example of how my long format works in localhost (date 2013-06-23 which was on Sunday):

Sunday, June 23, 2013 (en)
dimanche, 23 juin 2013 (fr)

Here is an example of how my long format works in production (date 2013-06-23):

Sunday, June 23, 2013 (en)
Sunday, June 23, 2013 (fr)

I read the http://guides.rubyonrails.org/i18n.html and several examples on Stack Overflow which used the l helper as described in section 3.3. However in section 5 it talks about using the t helper for custom translations. I am using t for all my other I18n internationalization and it is working fine. The only problem is when I use the l helper for dates.

I have looked for examples of how to use the t helper as described in the link Rails Guide link. The link does not give an example of how to code a statement with a field name. All of the examples I have found in Stack Overflow are using the l helper or the strftime method. I want to be able to 'translate' the date formats like I do the rest of the application in production like it works in localhost. I have checked all the files that I have changed to do this on my production server to make sure that all the files were moved over there. From what I did read it seems like the l helper may not work that well for custom translations. Maybe using the t helper will take care of this problem which was suggested by the Rails Guide. I will keep looking to see if I can find examples using the t helper like I included here for the l helper or try and guess some solutions.

Any help would be appreciated.

UPDATE: 7/29/2013 12:47 pm CDT - The only other difference that I can see between the two servers is that the development server is running ruby 1.9.3p327 and the production server is running ruby 1.9.3p362. However I cannot believe that could be causing my problem but it is a difference that I feel I should note.

回答1:

I could not find any other questions or comments relating to this. I decided to copy the fr.yml hashes for the rails-i18n gem. I did not have to do this on my development server to get the date formatting to translate into French. As I stated it was working fine without them. When I deployed the new yaml file in production all my clauses are in French. I guess there is a needle in a haystack type of bug somewhere in the i18n process. At least the Rails translations for dates are working now.