如何使Rails 3本地化我的日期格式? [英] How can I make Rails 3 localize my date formats?
问题描述
我正在一个Rails 3项目中,该项目在表单中可以放置日期.带有日期的文本字段使用日期选择器,因此不必担心以错误的格式输入日期,但是日期将以:db格式显示(例如2010-01-21).
I am working on a Rails 3 project where there is place for date input within a form. The text field with the date uses a date picker so there is no concern about the date being entered in a wrong format, however the date is being displayed in the :db format (e.g. 2010-01-21).
(注意:这特别是在表单字段中-例如<%= f.text_field :publish_date %>
,该字段应自动使用:default格式,并且不需要提供值)
(Note: this is specifically in form fields - e.g. <%= f.text_field :publish_date %>
, which should automatically use :default format, and shouldn't need to be provided with a value)
我尝试添加具有以下日期配置的自定义语言环境:
I have tried adding in a customized locale which has the following date configuration:
date:
formats:
# Use the strftime parameters for formats.
# When no format has been given, it uses default.
# You can provide other formats here if you like!
default: "%d/%m/%Y"
short: "%b %d"
long: "%B %d, %Y"
然后将我的语言环境设置为此(config.i18n.default_locale = "en-AU"
),但这似乎没有用,并且变得非常令人沮丧.
And then setting my locale to this (config.i18n.default_locale = "en-AU"
) however this doesn't seem to take and its becoming quite frustrating.
该应用程序最终将支持多种语言环境,因此设置一个初始化程序以覆盖应用程序启动时的日期格式并不是很合适,而且我知道此应该可以工作-我猜我在这里错过了什么.
The app will eventually support a number of locales, so setting up an initializer to override the date formats at application startup isn't really suitable, and I know that this should work - I'm guessing I've missed something here.
语言环境文件是:config/locales/en-AU.yml
,在我的application.rb文件中,我包括:
The locale file is: config/locales/en-AU.yml
and in my application.rb I am including:
config.i18n.load_path += Dir[Rails.root.join("config", "locales", "*.yml").to_s]
config.i18n.default_locale = "en-AU"
在我的application.rb文件中.
in my application.rb file.
推荐答案
显示日期时,可以使用I18n.l
When displaying a date, you can use I18n.l
所以你会做:
I18n.l @entry.created_at
如果您想更改其格式,则:
And if you want to change it's format :
I18n.l @entry.created_at, :format => :short
国际化导轨指南正在对此进行记录.
这篇关于如何使Rails 3本地化我的日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!