r18n-rails-api 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/r18n-rails-api/backend.rb +14 -7
- data/lib/r18n-rails-api/loader.rb +5 -6
- data/spec/backend_spec.rb +9 -1
- data/spec/data/general/ru.yml +4 -0
- metadata +7 -6
@@ -37,14 +37,14 @@ module R18n
|
|
37
37
|
scope, default, separator = options.values_at(*RESERVED_KEYS)
|
38
38
|
params = options.reject { |name, value| RESERVED_KEYS.include?(name) }
|
39
39
|
|
40
|
-
result = lookup(scope, key, separator, params)
|
40
|
+
result = lookup(locale, scope, key, separator, params)
|
41
41
|
|
42
42
|
if result.is_a? Untranslated
|
43
43
|
options = options.reject { |key, value| key == :default }
|
44
44
|
|
45
45
|
Array(default).each do |entry|
|
46
46
|
if entry.is_a? Symbol
|
47
|
-
value = lookup(scope, entry, separator, params)
|
47
|
+
value = lookup(locale, scope, entry, separator, params)
|
48
48
|
return value unless value.is_a? Untranslated
|
49
49
|
else
|
50
50
|
return entry
|
@@ -65,12 +65,13 @@ module R18n
|
|
65
65
|
# +:only_day+ and +:only_second+) and R18n (+:full+, +:human+, +:standard+
|
66
66
|
# and +:month+) time formatters.
|
67
67
|
def localize(locale, object, format = :default, options = {})
|
68
|
+
i18n = get_i18n(locale)
|
68
69
|
if format.is_a? Symbol
|
69
|
-
key
|
70
|
+
key = format
|
70
71
|
type = object.respond_to?(:sec) ? 'time' : 'date'
|
71
|
-
format =
|
72
|
+
format = i18n[type].formats[key] | format
|
72
73
|
end
|
73
|
-
|
74
|
+
i18n.localize(object, format)
|
74
75
|
end
|
75
76
|
|
76
77
|
# Return array of available locales codes.
|
@@ -85,6 +86,12 @@ module R18n
|
|
85
86
|
|
86
87
|
protected
|
87
88
|
|
89
|
+
def get_i18n(locale)
|
90
|
+
i18n = R18n.get
|
91
|
+
i18n = R18n.change(locale.to_s) if i18n.locale.code != locale.to_s
|
92
|
+
i18n
|
93
|
+
end
|
94
|
+
|
88
95
|
def format_value(result)
|
89
96
|
if result.is_a? TranslatedString
|
90
97
|
result.to_s
|
@@ -112,12 +119,12 @@ module R18n
|
|
112
119
|
|
113
120
|
# Find translation by <tt>scope.key(params)</tt> in current R18n I18n
|
114
121
|
# object.
|
115
|
-
def lookup(scope, key, separator, params)
|
122
|
+
def lookup(locale, scope, key, separator, params)
|
116
123
|
keys = (Array(scope) + Array(key)).map { |k|
|
117
124
|
k.to_s.split(separator || ::I18n.default_separator) }.flatten
|
118
125
|
last = keys.pop.to_sym
|
119
126
|
|
120
|
-
result = keys.inject(
|
127
|
+
result = keys.inject(get_i18n(locale).t) do |node, key|
|
121
128
|
if node.is_a? TranslatedString
|
122
129
|
node.get_untranslated(key)
|
123
130
|
else
|
@@ -33,15 +33,13 @@ module R18n
|
|
33
33
|
# R18n::I18n.new('en',
|
34
34
|
# R18n::Loader::Rails.new(I18n::Backend::ActiveRecord.new))
|
35
35
|
class Rails
|
36
|
+
include ::R18n::YamlMethods
|
37
|
+
|
36
38
|
# Create new loader for some +backend+ from Rails I18n. Backend must have
|
37
39
|
# +reload!+, +init_translations+ and +translations+ methods.
|
38
40
|
def initialize(backend = ::I18n::Backend::Simple.new)
|
39
41
|
@backend = backend
|
40
|
-
|
41
|
-
@private_type_class = ::YAML::PrivateType
|
42
|
-
else
|
43
|
-
@private_type_class = ::Syck::PrivateType
|
44
|
-
end
|
42
|
+
detect_yaml_private_type
|
45
43
|
end
|
46
44
|
|
47
45
|
# Array of locales, which has translations in +I18n.load_path+.
|
@@ -52,6 +50,7 @@ module R18n
|
|
52
50
|
|
53
51
|
# Return Hash with translations for +locale+.
|
54
52
|
def load(locale)
|
53
|
+
initialize_types
|
55
54
|
reload!
|
56
55
|
@translations[locale.code.downcase]
|
57
56
|
end
|
@@ -89,7 +88,7 @@ module R18n
|
|
89
88
|
else
|
90
89
|
Utils.hash_map(value) { |k, v| [k.to_s, transform(v)] }
|
91
90
|
end
|
92
|
-
elsif value.is_a? @private_type_class
|
91
|
+
elsif @private_type_class and value.is_a? @private_type_class
|
93
92
|
Typed.new(value.type_id, value.value)
|
94
93
|
else
|
95
94
|
value
|
data/spec/backend_spec.rb
CHANGED
@@ -9,7 +9,7 @@ describe R18n::Backend do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should return available locales" do
|
12
|
-
I18n.available_locales.should =~ [:en]
|
12
|
+
I18n.available_locales.should =~ [:en, :ru]
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should localize objects" do
|
@@ -108,4 +108,12 @@ describe R18n::Backend do
|
|
108
108
|
}
|
109
109
|
end
|
110
110
|
|
111
|
+
it "should change locale in place" do
|
112
|
+
I18n.load_path << PL
|
113
|
+
I18n.t('users', :count => 5).should == '5 users'
|
114
|
+
I18n.t('users', :count => 5, :locale => :ru).should == 'Много'
|
115
|
+
|
116
|
+
I18n.l(Date.parse('1970-01-01'), :locale => :ru).should == '01.01.1970'
|
117
|
+
end
|
118
|
+
|
111
119
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: r18n-rails-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-09-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: r18n-core
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - '='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 1.1.
|
21
|
+
version: 1.1.1
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - '='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 1.1.
|
29
|
+
version: 1.1.1
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: i18n
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -186,6 +186,7 @@ files:
|
|
186
186
|
- r18n-rails-api.gemspec
|
187
187
|
- spec/backend_spec.rb
|
188
188
|
- spec/data/general/en.yml
|
189
|
+
- spec/data/general/ru.yml
|
189
190
|
- spec/data/other/en.yml
|
190
191
|
- spec/data/other/ru.yml
|
191
192
|
- spec/data/pl/ru.yml
|
@@ -209,7 +210,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
209
210
|
version: '0'
|
210
211
|
segments:
|
211
212
|
- 0
|
212
|
-
hash:
|
213
|
+
hash: -1841956301363324580
|
213
214
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
214
215
|
none: false
|
215
216
|
requirements:
|
@@ -218,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
218
219
|
version: '0'
|
219
220
|
segments:
|
220
221
|
- 0
|
221
|
-
hash:
|
222
|
+
hash: -1841956301363324580
|
222
223
|
requirements: []
|
223
224
|
rubyforge_project:
|
224
225
|
rubygems_version: 1.8.23
|