Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
dad80ad7
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,发现更多精彩内容 >>
提交
dad80ad7
编写于
6月 20, 2010
作者:
J
José Valim
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
I18n.reload! is only called if any of the locale files actually changed.
上级
772c2b0b
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
196 addition
and
179 deletion
+196
-179
activesupport/lib/active_support/i18n.rb
activesupport/lib/active_support/i18n.rb
+1
-0
activesupport/lib/active_support/i18n_railtie.rb
activesupport/lib/active_support/i18n_railtie.rb
+80
-0
activesupport/lib/active_support/railtie.rb
activesupport/lib/active_support/railtie.rb
+1
-71
railties/test/application/initializers/i18n_test.rb
railties/test/application/initializers/i18n_test.rb
+114
-19
railties/test/railties/i18n_railtie_test.rb
railties/test/railties/i18n_railtie_test.rb
+0
-89
未找到文件。
activesupport/lib/active_support/i18n.rb
浏览文件 @
dad80ad7
...
...
@@ -4,5 +4,6 @@
$stderr
.
puts
"You don't have i18n installed in your application. Please add it to your Gemfile and run bundle install"
raise
e
end
I18n
.
load_path
<<
"
#{
File
.
dirname
(
__FILE__
)
}
/locale/en.yml"
ActiveSupport
.
run_load_hooks
(
:i18n
)
activesupport/lib/active_support/i18n_railtie.rb
0 → 100644
浏览文件 @
dad80ad7
require
"active_support"
require
"rails"
require
"active_support/file_update_checker"
module
I18n
class
Railtie
<
Rails
::
Railtie
config
.
i18n
=
ActiveSupport
::
OrderedOptions
.
new
config
.
i18n
.
railties_load_path
=
[]
config
.
i18n
.
load_path
=
[]
config
.
i18n
.
fallbacks
=
ActiveSupport
::
OrderedOptions
.
new
def
self
.
reloader
@reloader
||=
ActiveSupport
::
FileUpdateChecker
.
new
([]){
I18n
.
reload!
}
end
# Add I18n::Railtie.reloader to ActionDispatch callbacks. Since, at this
# point, no path was added to the reloader, I18n.reload! is not triggered
# on to_prepare callbacks. This will only happen on the config.after_initialize
# callback below.
initializer
"i18n.callbacks"
do
ActionDispatch
::
Callbacks
.
to_prepare
do
I18n
::
Railtie
.
reloader
.
execute_if_updated
end
end
# Set the i18n configuration only after initialization since a lot of
# configuration is still usually done in application initializers.
config
.
after_initialize
do
|
app
|
fallbacks
=
app
.
config
.
i18n
.
delete
(
:fallbacks
)
app
.
config
.
i18n
.
each
do
|
setting
,
value
|
case
setting
when
:railties_load_path
app
.
config
.
i18n
.
load_path
.
unshift
(
*
value
)
when
:load_path
I18n
.
load_path
+=
value
else
I18n
.
send
(
"
#{
setting
}
="
,
value
)
end
end
init_fallbacks
(
fallbacks
)
if
fallbacks
&&
validate_fallbacks
(
fallbacks
)
reloader
.
paths
.
concat
I18n
.
load_path
reloader
.
execute_if_updated
end
protected
def
self
.
include_fallbacks_module
I18n
.
backend
.
class
.
send
(
:include
,
I18n
::
Backend
::
Fallbacks
)
end
def
self
.
init_fallbacks
(
fallbacks
)
include_fallbacks_module
args
=
case
fallbacks
when
ActiveSupport
::
OrderedOptions
[
*
(
fallbacks
[
:defaults
]
||
[])
<<
fallbacks
[
:map
]].
compact
when
Hash
,
Array
Array
.
wrap
(
fallbacks
)
else
# TrueClass
[]
end
I18n
.
fallbacks
=
I18n
::
Locale
::
Fallbacks
.
new
(
*
args
)
end
def
self
.
validate_fallbacks
(
fallbacks
)
case
fallbacks
when
ActiveSupport
::
OrderedOptions
!
fallbacks
.
empty?
when
TrueClass
,
Array
,
Hash
true
else
raise
"Unexpected fallback type
#{
fallbacks
.
inspect
}
"
end
end
end
end
\ No newline at end of file
activesupport/lib/active_support/railtie.rb
浏览文件 @
dad80ad7
require
"active_support"
require
"rails"
require
"active_support/i18n_railtie"
module
ActiveSupport
class
Railtie
<
Rails
::
Railtie
...
...
@@ -26,75 +27,4 @@ class Railtie < Rails::Railtie
Time
.
zone_default
=
zone_default
end
end
end
module
I18n
class
Railtie
<
Rails
::
Railtie
config
.
i18n
=
ActiveSupport
::
OrderedOptions
.
new
config
.
i18n
.
railties_load_path
=
[]
config
.
i18n
.
load_path
=
[]
config
.
i18n
.
fallbacks
=
ActiveSupport
::
OrderedOptions
.
new
initializer
"i18n.initialize"
do
ActiveSupport
.
on_load
(
:i18n
)
do
I18n
.
reload!
ActionDispatch
::
Callbacks
.
to_prepare
do
I18n
.
reload!
end
end
end
# Set the i18n configuration from config.i18n but special-case for
# the load_path which should be appended to what's already set instead of overwritten.
config
.
after_initialize
do
|
app
|
fallbacks
=
app
.
config
.
i18n
.
delete
(
:fallbacks
)
app
.
config
.
i18n
.
each
do
|
setting
,
value
|
case
setting
when
:railties_load_path
app
.
config
.
i18n
.
load_path
.
unshift
(
*
value
)
when
:load_path
I18n
.
load_path
+=
value
else
I18n
.
send
(
"
#{
setting
}
="
,
value
)
end
end
init_fallbacks
(
fallbacks
)
if
fallbacks
&&
validate_fallbacks
(
fallbacks
)
I18n
.
reload!
end
class
<<
self
protected
def
init_fallbacks
(
fallbacks
)
include_fallbacks_module
args
=
case
fallbacks
when
ActiveSupport
::
OrderedOptions
[
*
(
fallbacks
[
:defaults
]
||
[])
<<
fallbacks
[
:map
]].
compact
when
Hash
,
Array
Array
.
wrap
(
fallbacks
)
else
# TrueClass
[]
end
I18n
.
fallbacks
=
I18n
::
Locale
::
Fallbacks
.
new
(
*
args
)
end
def
include_fallbacks_module
I18n
.
backend
.
class
.
send
(
:include
,
I18n
::
Backend
::
Fallbacks
)
end
def
validate_fallbacks
(
fallbacks
)
case
fallbacks
when
ActiveSupport
::
OrderedOptions
!
fallbacks
.
empty?
when
TrueClass
,
Array
,
Hash
true
else
raise
"Unexpected fallback type
#{
fallbacks
.
inspect
}
"
end
end
end
end
end
\ No newline at end of file
railties/test/application/initializers/i18n_test.rb
浏览文件 @
dad80ad7
...
...
@@ -8,48 +8,143 @@ def setup
build_app
boot_rails
FileUtils
.
rm_rf
"
#{
app_path
}
/config/environments"
require
"rails/all"
end
# i18n
def
load_app
require
"
#{
app_path
}
/config/environment"
end
def
app
@app
||=
Rails
::
Application
end
def
assert_fallbacks
(
fallbacks
)
fallbacks
.
each
do
|
locale
,
expected
|
actual
=
I18n
.
fallbacks
[
locale
]
assert_equal
expected
,
actual
,
"expected fallbacks for
#{
locale
.
inspect
}
to be
#{
expected
.
inspect
}
, but were
#{
actual
.
inspect
}
"
end
end
def
assert_no_fallbacks
assert
!
I18n
.
backend
.
class
.
included_modules
.
include?
(
I18n
::
Backend
::
Fallbacks
)
end
# Locales
test
"setting another default locale"
do
add_to_config
<<-
RUBY
config.root = "
#{
app_path
}
"
config.i18n.default_locale = :de
RUBY
require
"
#{
app_path
}
/config/environment"
load_app
assert_equal
:de
,
I18n
.
default_locale
end
# Load paths
test
"no config locales dir present should return empty load path"
do
FileUtils
.
rm_rf
"
#{
app_path
}
/config/locales"
add_to_config
<<-
RUBY
config.root = "
#{
app_path
}
"
RUBY
require
"
#{
app_path
}
/config/environment"
load_app
assert_equal
[],
Rails
.
application
.
config
.
i18n
.
load_path
end
test
"config locales dir present should be added to load path"
do
test
"locale files should be added to the load path"
do
app_file
"config/another_locale.yml"
,
""
add_to_config
<<-
RUBY
config.
root = "
#{
app_path
}
"
config.
i18n.load_path << config.root.join("config/another_locale.yml").to_s
RUBY
require
"
#{
app_path
}
/config/environment"
assert_equal
[
"
#{
app_path
}
/config/locales/en.yml"
],
Rails
.
application
.
config
.
i18n
.
load_path
load_app
assert_equal
[
"
#{
app_path
}
/config/locales/en.yml"
,
"
#{
app_path
}
/config/another_locale.yml"
],
Rails
.
application
.
config
.
i18n
.
load_path
assert
I18n
.
load_path
.
include?
(
"
#{
app_path
}
/config/locales/en.yml"
)
assert
I18n
.
load_path
.
include?
(
"
#{
app_path
}
/config/another_locale.yml"
)
end
test
"
config defaults should be added with config setting
s"
do
test
"
locales are reloaded if they change between request
s"
do
add_to_config
<<-
RUBY
config.root = "
#{
app_path
}
"
config.i18n.load_path << "my/other/locale.yml"
config.cache_classes = false
RUBY
require
"
#{
app_path
}
/config/environment"
assert_equal
[
"
#{
app_path
}
/config/locales/en.yml"
,
"my/other/locale.yml"
],
Rails
.
application
.
config
.
i18n
.
load_path
app_file
"config/locales/en.yml"
,
<<-
YAML
en:
foo: "1"
YAML
app_file
'config/routes.rb'
,
<<-
RUBY
AppTemplate::Application.routes.draw do |map|
match '/i18n', :to => lambda { |env| [200, {}, [I18n.t(:foo)]] }
end
RUBY
require
'rack/test'
extend
Rack
::
Test
::
Methods
load_app
get
"/i18n"
assert_equal
"1"
,
last_response
.
body
app_file
"config/locales/en.yml"
,
<<-
YAML
en:
foo: "2"
YAML
get
"/i18n"
assert_equal
"2"
,
last_response
.
body
end
# Fallbacks
test
"not using config.i18n.fallbacks does not initialize I18n.fallbacks"
do
I18n
.
backend
=
Class
.
new
{
include
I18n
::
Backend
::
Base
}.
new
load_app
assert_no_fallbacks
end
test
"config.i18n.fallbacks = true initializes I18n.fallbacks with default settings"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
=
true
load_app
assert
I18n
.
backend
.
class
.
included_modules
.
include?
(
I18n
::
Backend
::
Fallbacks
)
assert_fallbacks
:de
=>
[
:de
,
:en
]
end
test
"config.i18n.fallbacks = true initializes I18n.fallbacks with default settings even when backend changes"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
=
true
I18n
::
Railtie
.
config
.
i18n
.
backend
=
Class
.
new
{
include
I18n
::
Backend
::
Base
}.
new
load_app
assert
I18n
.
backend
.
class
.
included_modules
.
include?
(
I18n
::
Backend
::
Fallbacks
)
assert_fallbacks
:de
=>
[
:de
,
:en
]
end
test
"config.i18n.fallbacks.defaults = [:'en-US'] initializes fallbacks with en-US as a fallback default"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
.
defaults
=
[
:'en-US'
]
load_app
assert_fallbacks
:de
=>
[
:de
,
:'en-US'
,
:en
]
end
test
"config.i18n.fallbacks.map = { :ca => :'es-ES' } initializes fallbacks with a mapping ca => es-ES"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
.
map
=
{
:ca
=>
:'es-ES'
}
load_app
assert_fallbacks
:ca
=>
[
:ca
,
:"es-ES"
,
:es
,
:en
]
end
test
"[shortcut] config.i18n.fallbacks = [:'en-US'] initializes fallbacks with en-US as a fallback default"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
=
[
:'en-US'
]
load_app
assert_fallbacks
:de
=>
[
:de
,
:'en-US'
,
:en
]
end
test
"[shortcut] config.i18n.fallbacks = [{ :ca => :'es-ES' }] initializes fallbacks with a mapping de-AT => de-DE"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
.
map
=
{
:ca
=>
:'es-ES'
}
load_app
assert_fallbacks
:ca
=>
[
:ca
,
:"es-ES"
,
:es
,
:en
]
end
test
"[shortcut] config.i18n.fallbacks = [:'en-US', { :ca => :'es-ES' }] initializes fallbacks with the given arguments"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
=
[
:'en-US'
,
{
:ca
=>
:'es-ES'
}]
load_app
assert_fallbacks
:ca
=>
[
:ca
,
:"es-ES"
,
:es
,
:'en-US'
,
:en
]
end
end
end
\ No newline at end of file
railties/test/railties/i18n_railtie_test.rb
已删除
100644 → 0
浏览文件 @
772c2b0b
require
"isolation/abstract_unit"
module
RailtiesTest
class
I18nRailtieTest
<
Test
::
Unit
::
TestCase
include
ActiveSupport
::
Testing
::
Isolation
def
setup
build_app
boot_rails
FileUtils
.
rm_rf
(
"
#{
app_path
}
/config/environments"
)
require
"rails/all"
end
def
load_app
require
"
#{
app_path
}
/config/environment"
end
def
assert_fallbacks
(
fallbacks
)
fallbacks
.
each
do
|
locale
,
expected
|
actual
=
I18n
.
fallbacks
[
locale
]
assert_equal
expected
,
actual
,
"expected fallbacks for
#{
locale
.
inspect
}
to be
#{
expected
.
inspect
}
, but were
#{
actual
.
inspect
}
"
end
end
def
assert_no_fallbacks
assert
!
I18n
.
backend
.
class
.
included_modules
.
include?
(
I18n
::
Backend
::
Fallbacks
)
end
test
"config.i18n.load_path gets added to I18n.load_path"
do
I18n
.
load_path
=
[
'existing/path/to/locales'
]
I18n
::
Railtie
.
config
.
i18n
.
load_path
=
[
'new/path/to/locales'
]
load_app
assert
I18n
.
load_path
.
include?
(
'existing/path/to/locales'
)
assert
I18n
.
load_path
.
include?
(
'new/path/to/locales'
)
end
test
"not using config.i18n.fallbacks does not initialize I18n.fallbacks"
do
I18n
.
backend
=
Class
.
new
{
include
I18n
::
Backend
::
Base
}.
new
load_app
assert_no_fallbacks
end
test
"config.i18n.fallbacks = true initializes I18n.fallbacks with default settings"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
=
true
load_app
assert
I18n
.
backend
.
class
.
included_modules
.
include?
(
I18n
::
Backend
::
Fallbacks
)
assert_fallbacks
:de
=>
[
:de
,
:en
]
end
test
"config.i18n.fallbacks = true initializes I18n.fallbacks with default settings even when backend changes"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
=
true
I18n
::
Railtie
.
config
.
i18n
.
backend
=
Class
.
new
{
include
I18n
::
Backend
::
Base
}.
new
load_app
assert
I18n
.
backend
.
class
.
included_modules
.
include?
(
I18n
::
Backend
::
Fallbacks
)
assert_fallbacks
:de
=>
[
:de
,
:en
]
end
test
"config.i18n.fallbacks.defaults = [:'en-US'] initializes fallbacks with en-US as a fallback default"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
.
defaults
=
[
:'en-US'
]
load_app
assert_fallbacks
:de
=>
[
:de
,
:'en-US'
,
:en
]
end
test
"config.i18n.fallbacks.map = { :ca => :'es-ES' } initializes fallbacks with a mapping ca => es-ES"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
.
map
=
{
:ca
=>
:'es-ES'
}
load_app
assert_fallbacks
:ca
=>
[
:ca
,
:"es-ES"
,
:es
,
:en
]
end
test
"[shortcut] config.i18n.fallbacks = [:'en-US'] initializes fallbacks with en-US as a fallback default"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
=
[
:'en-US'
]
load_app
assert_fallbacks
:de
=>
[
:de
,
:'en-US'
,
:en
]
end
test
"[shortcut] config.i18n.fallbacks = [{ :ca => :'es-ES' }] initializes fallbacks with a mapping de-AT => de-DE"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
.
map
=
{
:ca
=>
:'es-ES'
}
load_app
assert_fallbacks
:ca
=>
[
:ca
,
:"es-ES"
,
:es
,
:en
]
end
test
"[shortcut] config.i18n.fallbacks = [:'en-US', { :ca => :'es-ES' }] initializes fallbacks with the given arguments"
do
I18n
::
Railtie
.
config
.
i18n
.
fallbacks
=
[
:'en-US'
,
{
:ca
=>
:'es-ES'
}]
load_app
assert_fallbacks
:ca
=>
[
:ca
,
:"es-ES"
,
:es
,
:'en-US'
,
:en
]
end
end
end
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录