Created
November 9, 2009 11:01
-
-
Save rainchen/229872 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rails i18n helpers by RainChen @ 2009-11-09 | |
# more detail: http://hi.baidu.com/rainchen/blog/item/b393fc03589f4b82d53f7c37.html | |
class String | |
# short curt for I18n.t | |
# This returns the translation for foo or default if no translation was found: | |
# "foo".t # => "foo" | |
# "hello".t "en" # => "hello" | |
# "hello".t :default => 'halo' | |
def t(*args) | |
locale = args.first | |
locale = I18n.locale if locale.nil? || locale.is_a?(Hash) | |
defaults = { | |
:locale => locale, | |
:default => self | |
} | |
options = args.last.is_a?(Hash) ? args.last : {} | |
I18n.t self.gsub(" ", "_"), defaults.merge(options) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if defined?(ActiveRecord) | |
module ActiveRecord | |
module ConnectionAdapters | |
class TableDefinition | |
def column_with_enumerated_attribute(name, type, options = {}) | |
type = 'string' if type.to_s == 'enum' | |
column_without_enumerated_attribute(name, type, options) | |
end | |
alias_method_chain :column, :enumerated_attribute | |
def enum(*args) | |
options = args.extract_options! | |
column_names = args | |
column_names.each { |name| column(name, 'string', options) } | |
end | |
end | |
end | |
end | |
end | |
#ARGV is used by generators -- if it contains one of these generator commands - add enumeration support | |
#unless ((ARGV || []) & ["scaffold", "rspec_scaffold", "nifty_scaffold"]).empty? | |
if ((ARGV || []).any?{|o| o =~ /scaffold/ }) | |
require 'rails_generator' | |
module Rails | |
module Generator | |
class GeneratedAttribute | |
def field_type_with_enumerated_attribute | |
return (@field_type = :enum_select) if type == :enum | |
field_type_without_enumerated_attribute | |
end | |
alias_method_chain :field_type, :enumerated_attribute | |
end | |
end | |
end | |
end | |
if defined?(ActionView::Base) | |
module ActionView | |
module Helpers | |
#form_options_helper.rb | |
module FormOptionsHelper | |
#def select | |
def enum_select(object, method, options={}, html_options={}) | |
InstanceTag.new(object, method, self, options.delete(:object)).to_enum_select_tag(options, html_options) | |
end | |
end | |
class InstanceTag | |
def to_enum_select_tag(options, html_options={}) | |
choices = [] | |
if self.object.respond_to?(:enums) | |
enums = self.object.enums(method_name.to_sym) | |
# Hack: auto use i18n | |
if I18n.locale != :en | |
enums.enums.each do |enum| | |
enums.set_label enum, "activerecord.attributes.#{self.object.class.name.downcase}.#{method_name}_labels.#{enum}".t(:default => enums.label(enum)) | |
end | |
end | |
choices = enums ? enums.select_options : [] | |
if (value = self.object.__send__(method_name.to_sym)) | |
options[:selected] ||= value.to_s | |
else | |
# BUGFIXED: include blank if allow nil | |
options[:include_blank] = enums.allows_nil? if options[:include_blank].nil? | |
end | |
end | |
to_select_tag(choices, options, html_options) | |
end | |
#initialize record_name, method, self | |
def to_tag_with_enumerated_attribute(options={}) | |
#look for an enum | |
if (column_type == :string && | |
self.object.class.respond_to?(:has_enumerated_attribute?) && | |
self.object.class.has_enumerated_attribute?(method_name.to_sym)) | |
to_enum_select_tag(options) | |
else | |
to_tag_without_enumerated_attribute(options) | |
end | |
end | |
alias_method_chain :to_tag, :enumerated_attribute | |
end | |
class FormBuilder | |
def enum_select(method, options={}, html_options={}) | |
@template.enum_select(@object_name, method, objectify_options(options), @default_options.merge(html_options)) | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
zh-CN: | |
activerecord: | |
errors: | |
template: | |
header: | |
one: "无法保存 {{model}}: 1 错误" | |
other: "无法保存 {{model}}: {{count}} 错误." | |
body: "请检查以下字段:" | |
messages: | |
inclusion: "不包含于列表中" | |
exclusion: "不能用" | |
invalid: "不合法" | |
confirmation: "与确认值不匹配" | |
accepted: "必须接受" | |
empty: "不能留空" | |
blank: "不能为空字符" | |
too_long: "过长 (不能超过 {{count}} 个字符)" | |
too_short: "过短 (不能少于 {{count}} 个字符)" | |
wrong_length: "长度非法 (必须为 {{count}} 个字符)" | |
taken: "不可用" | |
not_a_number: "不是数字" | |
greater_than: "必须大于 {{count}}" | |
greater_than_or_equal_to: "必须大于或等于 {{count}}" | |
equal_to: "必须等于 {{count}}" | |
less_than: "必须小于 {{count}}" | |
less_than_or_equal_to: "必须小于或等于 {{count}}" | |
odd: "必须为单数" | |
even: "必须为双数" | |
# Append your own errors here or at the model/attributes scope. | |
# You can define own errors for models or model attributes. | |
# The values :model, :attribute and :value are always available for interpolation. | |
# | |
# For example, | |
# models: | |
# user: | |
# blank: "This is a custom blank message for {{model}}: {{attribute}}" | |
# attributes: | |
# login: | |
# blank: "This is a custom blank message for User login" | |
# Will define custom blank validation message for User model and | |
# custom blank validation message for login attribute of User model. | |
models: | |
models: | |
user: "用户" | |
attributes: | |
user: | |
login: "帐号" | |
email: "邮箱" | |
password: "密码" | |
password_confirmation: "密码确认" | |
remember_me: "在这台电脑上记住我" | |
gender: "性别" | |
gender_labels: | |
male: "男" | |
female: "女" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment