Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
8d30193b
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8d30193b
编写于
9月 24, 2010
作者:
R
Rodrigo Rosenfeld Rosas
提交者:
José Valim
9月 24, 2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Properly interpolate i18n keys in modules [#5572 state:resolved]
上级
f625b87f
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
37 addition
and
8 deletion
+37
-8
activemodel/lib/active_model/errors.rb
activemodel/lib/active_model/errors.rb
+2
-2
activemodel/lib/active_model/naming.rb
activemodel/lib/active_model/naming.rb
+10
-5
activemodel/lib/active_model/translation.rb
activemodel/lib/active_model/translation.rb
+1
-1
activemodel/test/cases/validations/i18n_validation_test.rb
activemodel/test/cases/validations/i18n_validation_test.rb
+19
-0
activemodel/test/models/person.rb
activemodel/test/models/person.rb
+5
-0
未找到文件。
activemodel/lib/active_model/errors.rb
浏览文件 @
8d30193b
...
...
@@ -266,8 +266,8 @@ def generate_message(attribute, type = :invalid, options = {})
type
=
options
.
delete
(
:message
)
if
options
[
:message
].
is_a?
(
Symbol
)
defaults
=
@base
.
class
.
lookup_ancestors
.
map
do
|
klass
|
[
:"
#{
@base
.
class
.
i18n_scope
}
.errors.models.
#{
klass
.
model_name
.
underscore
}
.attributes.
#{
attribute
}
.
#{
type
}
"
,
:"
#{
@base
.
class
.
i18n_scope
}
.errors.models.
#{
klass
.
model_name
.
underscore
}
.
#{
type
}
"
]
[
:"
#{
@base
.
class
.
i18n_scope
}
.errors.models.
#{
klass
.
model_name
.
i18n_key
}
.attributes.
#{
attribute
}
.
#{
type
}
"
,
:"
#{
@base
.
class
.
i18n_scope
}
.errors.models.
#{
klass
.
model_name
.
i18n_key
}
.
#{
type
}
"
]
end
defaults
<<
options
.
delete
(
:message
)
...
...
activemodel/lib/active_model/naming.rb
浏览文件 @
8d30193b
...
...
@@ -2,7 +2,7 @@
module
ActiveModel
class
Name
<
String
attr_reader
:singular
,
:plural
,
:element
,
:collection
,
:partial_path
,
:route_key
,
:param_key
attr_reader
:singular
,
:plural
,
:element
,
:collection
,
:partial_path
,
:route_key
,
:param_key
,
:i18n_key
alias_method
:cache_key
,
:collection
def
initialize
(
klass
,
namespace
=
nil
)
...
...
@@ -18,6 +18,7 @@ def initialize(klass, namespace = nil)
@partial_path
=
"
#{
@collection
}
/
#{
@element
}
"
.
freeze
@param_key
=
(
namespace
?
_singularize
(
@unnamespaced
)
:
@singular
).
freeze
@route_key
=
(
namespace
?
ActiveSupport
::
Inflector
.
pluralize
(
@param_key
)
:
@plural
).
freeze
@i18n_key
=
_singularize
(
self
,
'.'
).
to_sym
end
# Transform the model name into a more humane format, using I18n. By default,
...
...
@@ -31,7 +32,7 @@ def human(options={})
@klass
.
respond_to?
(
:i18n_scope
)
defaults
=
@klass
.
lookup_ancestors
.
map
do
|
klass
|
klass
.
model_name
.
underscore
.
to_sym
klass
.
model_name
.
i18n_key
end
defaults
<<
options
.
delete
(
:default
)
if
options
[
:default
]
...
...
@@ -42,9 +43,10 @@ def human(options={})
end
private
def
_singularize
(
str
)
ActiveSupport
::
Inflector
.
underscore
(
str
).
tr
(
'/'
,
'_'
)
end
def
_singularize
(
string
,
replacement
=
'_'
)
ActiveSupport
::
Inflector
.
underscore
(
string
).
tr
(
'/'
,
replacement
)
end
end
# == Active Model Naming
...
...
@@ -60,6 +62,9 @@ def _singularize(str)
# BookCover.model_name # => "BookCover"
# BookCover.model_name.human # => "Book cover"
#
# BookCover.model_name.i18n_key # => "book_cover"
# BookModule::BookCover.model_name.i18n_key # => "book_module.book_cover"
#
# Providing the functionality that ActiveModel::Naming provides in your object
# is required to pass the Active Model Lint test. So either extending the provided
# method below, or rolling your own is required..
...
...
activemodel/lib/active_model/translation.rb
浏览文件 @
8d30193b
...
...
@@ -44,7 +44,7 @@ def lookup_ancestors
# Specify +options+ with additional translating options.
def
human_attribute_name
(
attribute
,
options
=
{})
defaults
=
lookup_ancestors
.
map
do
|
klass
|
:"
#{
self
.
i18n_scope
}
.attributes.
#{
klass
.
model_name
.
underscore
}
.
#{
attribute
}
"
:"
#{
self
.
i18n_scope
}
.attributes.
#{
klass
.
model_name
.
i18n_key
}
.
#{
attribute
}
"
end
defaults
<<
:"attributes.
#{
attribute
}
"
...
...
activemodel/test/cases/validations/i18n_validation_test.rb
浏览文件 @
8d30193b
...
...
@@ -55,6 +55,14 @@ def test_errors_full_messages_translates_human_attribute_name_for_model_attribut
assert_equal
[
"Person's name not found"
],
@person
.
errors
.
full_messages
end
def
test_errors_full_messages_translates_human_attribute_name_for_model_in_module_attributes
I18n
.
backend
.
store_translations
(
'en'
,
:activemodel
=>
{
:attributes
=>
{
:person_module
=>
{
:person
=>
{
:name
=>
"Person in Module's name"
}}}})
person
=
PersonModule
::
Person
.
new
person
.
errors
.
add
(
:name
,
'not found'
)
PersonModule
::
Person
.
expects
(
:human_attribute_name
).
with
(
:name
,
:default
=>
'Name'
).
returns
(
"Person in Module's name"
)
assert_equal
[
"Person in Module's name not found"
],
person
.
errors
.
full_messages
end
def
test_errors_full_messages_uses_format
I18n
.
backend
.
store_translations
(
'en'
,
:errors
=>
{
:format
=>
"Field %{attribute} %{message}"
})
@person
.
errors
.
add
(
'name'
,
'empty'
)
...
...
@@ -363,4 +371,15 @@ def test_validates_with_message_string
assert_equal
[
"I am a custom error"
],
@person
.
errors
[
:title
]
end
def
test_model_with_module_i18n_scope
I18n
.
backend
.
store_translations
'en'
,
:activemodel
=>
{
:errors
=>
{
:models
=>
{
:person_module
=>
{
:person
=>
{
:blank
=>
'generic blank'
}}}}}
PersonModule
::
Person
.
validates_presence_of
:title
person
=
PersonModule
::
Person
.
new
person
.
valid?
assert_equal
[
'generic blank'
],
person
.
errors
[
:title
]
I18n
.
backend
.
store_translations
'en'
,
:activemodel
=>
{
:errors
=>
{
:models
=>
{
:person_module
=>
{
:person
=>
{
:attributes
=>
{
:title
=>
{
:blank
=>
'title cannot be blank'
}}}}}}}
person
.
valid?
assert_equal
[
'title cannot be blank'
],
person
.
errors
[
:title
]
end
end
activemodel/test/models/person.rb
浏览文件 @
8d30193b
...
...
@@ -11,3 +11,8 @@ def condition_is_true
class
Child
<
Person
end
module
PersonModule
class
Person
<
::
Person
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录