Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
13af5ace
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,发现更多精彩内容 >>
提交
13af5ace
编写于
7月 31, 2012
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #7197 from davidcelis/i18n_inflector
Make ActiveSupport::Inflector locale aware and multilingual
上级
b09d6aae
7db0b073
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
84 addition
and
25 deletion
+84
-25
activesupport/lib/active_support/core_ext/string/inflections.rb
...support/lib/active_support/core_ext/string/inflections.rb
+18
-4
activesupport/lib/active_support/inflections.rb
activesupport/lib/active_support/inflections.rb
+1
-1
activesupport/lib/active_support/inflector/inflections.rb
activesupport/lib/active_support/inflector/inflections.rb
+14
-9
activesupport/lib/active_support/inflector/methods.rb
activesupport/lib/active_support/inflector/methods.rb
+16
-6
activesupport/test/inflector_test.rb
activesupport/test/inflector_test.rb
+30
-1
railties/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb
...rs/rails/app/templates/config/initializers/inflections.rb
+5
-4
未找到文件。
activesupport/lib/active_support/core_ext/string/inflections.rb
浏览文件 @
13af5ace
...
...
@@ -13,6 +13,11 @@ class String
# the singular form will be returned if <tt>count == 1</tt>.
# For any other value of +count+ the plural will be returned.
#
# If the optional parameter +locale+ is specified,
# the word will be pluralized as a word of that language.
# By default, this parameter is set to <tt>:en</tt>.
# You must define your own inflection rules for languages other than English.
#
# 'post'.pluralize # => "posts"
# 'octopus'.pluralize # => "octopi"
# 'sheep'.pluralize # => "sheep"
...
...
@@ -21,15 +26,23 @@ class String
# 'CamelOctopus'.pluralize # => "CamelOctopi"
# 'apple'.pluralize(1) # => "apple"
# 'apple'.pluralize(2) # => "apples"
def
pluralize
(
count
=
nil
)
# 'ley'.pluralize(:es) # => "leyes"
# 'ley'.pluralize(1, :es) # => "ley"
def
pluralize
(
count
=
nil
,
locale
=
:en
)
locale
=
count
if
count
.
is_a?
(
Symbol
)
if
count
==
1
self
else
ActiveSupport
::
Inflector
.
pluralize
(
self
)
ActiveSupport
::
Inflector
.
pluralize
(
self
,
locale
)
end
end
# The reverse of +pluralize+, returns the singular form of a word in a string.
#
# If the optional parameter +locale+ is specified,
# the word will be singularized as a word of that language.
# By default, this paramter is set to <tt>:en</tt>.
# You must define your own inflection rules for languages other than English.
#
# 'posts'.singularize # => "post"
# 'octopi'.singularize # => "octopus"
...
...
@@ -37,8 +50,9 @@ def pluralize(count = nil)
# 'word'.singularize # => "word"
# 'the blue mailmen'.singularize # => "the blue mailman"
# 'CamelOctopi'.singularize # => "CamelOctopus"
def
singularize
ActiveSupport
::
Inflector
.
singularize
(
self
)
# 'leyes'.singularize(:es) # => "ley"
def
singularize
(
locale
=
:en
)
ActiveSupport
::
Inflector
.
singularize
(
self
,
locale
)
end
# +constantize+ tries to find a declared constant with the name specified
...
...
activesupport/lib/active_support/inflections.rb
浏览文件 @
13af5ace
require
'active_support/inflector/inflections'
module
ActiveSupport
Inflector
.
inflections
do
|
inflect
|
Inflector
.
inflections
(
:en
)
do
|
inflect
|
inflect
.
plural
(
/$/
,
's'
)
inflect
.
plural
(
/s$/i
,
's'
)
inflect
.
plural
(
/^(ax|test)is$/i
,
'\1es'
)
...
...
activesupport/lib/active_support/inflector/inflections.rb
浏览文件 @
13af5ace
require
'active_support/core_ext/array/prepend_and_append'
require
'active_support/i18n'
module
ActiveSupport
module
Inflector
extend
self
# A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
# inflection rules.
# inflection rules. If passed an optional locale, rules for other languages can be specified. The default locale is
# <tt>:en</tt>. Only rules for English are provided.
#
# ActiveSupport::Inflector.inflections do |inflect|
# ActiveSupport::Inflector.inflections
(:en)
do |inflect|
# inflect.plural /^(ox)$/i, '\1\2en'
# inflect.singular /^(ox)en/i, '\1'
#
...
...
@@ -20,8 +22,9 @@ module Inflector
# pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
# already have been loaded.
class
Inflections
def
self
.
instance
@__instance__
||=
new
def
self
.
instance
(
locale
=
:en
)
@__instance__
||=
Hash
.
new
{
|
h
,
k
|
h
[
k
]
=
new
}
@__instance__
[
locale
]
end
attr_reader
:plurals
,
:singulars
,
:uncountables
,
:humans
,
:acronyms
,
:acronym_regex
...
...
@@ -160,16 +163,18 @@ def clear(scope = :all)
end
# Yields a singleton instance of Inflector::Inflections so you can specify additional
# inflector rules.
# inflector rules. If passed an optional locale, rules for other languages can be specified.
# If not specified, defaults to <tt>:en</tt>. Only rules for English are provided.
#
#
# ActiveSupport::Inflector.inflections do |inflect|
# ActiveSupport::Inflector.inflections
(:en)
do |inflect|
# inflect.uncountable "rails"
# end
def
inflections
def
inflections
(
locale
=
:en
)
if
block_given?
yield
Inflections
.
instance
yield
Inflections
.
instance
(
locale
)
else
Inflections
.
instance
Inflections
.
instance
(
locale
)
end
end
end
...
...
activesupport/lib/active_support/inflector/methods.rb
浏览文件 @
13af5ace
...
...
@@ -10,31 +10,41 @@ module ActiveSupport
#
# The Rails core team has stated patches for the inflections library will not be accepted
# in order to avoid breaking legacy applications which may be relying on errant inflections.
# If you discover an incorrect inflection and require it for your application
, you'll need
#
to correct it
yourself (explained below).
# If you discover an incorrect inflection and require it for your application
or wish to
#
define rules for languages other than English, please correct or add them
yourself (explained below).
module
Inflector
extend
self
# Returns the plural form of the word in the string.
#
# If passed an optional +locale+ parameter, the word will be
# pluralized using rules defined for that language. By default,
# this parameter is set to <tt>:en</tt>.
#
# "post".pluralize # => "posts"
# "octopus".pluralize # => "octopi"
# "sheep".pluralize # => "sheep"
# "words".pluralize # => "words"
# "CamelOctopus".pluralize # => "CamelOctopi"
def
pluralize
(
word
)
apply_inflections
(
word
,
inflections
.
plurals
)
# "ley".pluralize(:es) # => "leyes"
def
pluralize
(
word
,
locale
=
:en
)
apply_inflections
(
word
,
inflections
(
locale
).
plurals
)
end
# The reverse of +pluralize+, returns the singular form of a word in a string.
#
# If passed an optional +locale+ parameter, the word will be
# pluralized using rules defined for that language. By default,
# this parameter is set to <tt>:en</tt>.
#
# "posts".singularize # => "post"
# "octopi".singularize # => "octopus"
# "sheep".singularize # => "sheep"
# "word".singularize # => "word"
# "CamelOctopi".singularize # => "CamelOctopus"
def
singularize
(
word
)
apply_inflections
(
word
,
inflections
.
singulars
)
# "leyes".singularize(:es) # => "ley"
def
singularize
(
word
,
locale
=
:en
)
apply_inflections
(
word
,
inflections
(
locale
).
singulars
)
end
# By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+
...
...
activesupport/test/inflector_test.rb
浏览文件 @
13af5ace
...
...
@@ -354,6 +354,35 @@ def test_clear_#{inflection_type}
RUBY
end
def
test_inflector_locality
ActiveSupport
::
Inflector
.
inflections
(
:es
)
do
|
inflect
|
inflect
.
plural
(
/$/
,
's'
)
inflect
.
plural
(
/z$/i
,
'ces'
)
inflect
.
singular
(
/s$/
,
''
)
inflect
.
singular
(
/es$/
,
''
)
inflect
.
irregular
(
'el'
,
'los'
)
end
assert_equal
(
'hijos'
,
'hijo'
.
pluralize
(
:es
))
assert_equal
(
'luces'
,
'luz'
.
pluralize
(
:es
))
assert_equal
(
'luzs'
,
'luz'
.
pluralize
)
assert_equal
(
'sociedad'
,
'sociedades'
.
singularize
(
:es
))
assert_equal
(
'sociedade'
,
'sociedades'
.
singularize
)
assert_equal
(
'los'
,
'el'
.
pluralize
(
:es
))
assert_equal
(
'els'
,
'el'
.
pluralize
)
ActiveSupport
::
Inflector
.
inflections
(
:es
)
{
|
inflect
|
inflect
.
clear
}
assert
ActiveSupport
::
Inflector
.
inflections
(
:es
).
plurals
.
empty?
assert
ActiveSupport
::
Inflector
.
inflections
(
:es
).
singulars
.
empty?
assert
!
ActiveSupport
::
Inflector
.
inflections
.
plurals
.
empty?
assert
!
ActiveSupport
::
Inflector
.
inflections
.
singulars
.
empty?
end
def
test_clear_all
with_dup
do
ActiveSupport
::
Inflector
.
inflections
do
|
inflect
|
...
...
@@ -467,7 +496,7 @@ def test_clear_with_default
# there are module functions that access ActiveSupport::Inflector.inflections,
# so we need to replace the singleton itself.
def
with_dup
original
=
ActiveSupport
::
Inflector
.
inflections
original
=
ActiveSupport
::
Inflector
::
Inflections
.
instance_variable_get
(
:@__instance__
)
ActiveSupport
::
Inflector
::
Inflections
.
instance_variable_set
(
:@__instance__
,
original
.
dup
)
ensure
ActiveSupport
::
Inflector
::
Inflections
.
instance_variable_set
(
:@__instance__
,
original
)
...
...
railties/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb
浏览文件 @
13af5ace
# Be sure to restart your server when you modify this file.
# Add new inflection rules using the following format
# (all these examples are active by default):
# ActiveSupport::Inflector.inflections do |inflect|
# Add new inflection rules using the following format. Inflections
# are locale specific, and you may define rules for as many different
# locales as you wish. All of these examples are active by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
...
...
@@ -10,6 +11,6 @@
# end
#
# These inflection rules are supported but not enabled by default:
# ActiveSupport::Inflector.inflections do |inflect|
# ActiveSupport::Inflector.inflections
(:en)
do |inflect|
# inflect.acronym 'RESTful'
# end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录