@@ -34,17 +34,21 @@ def translate(locale, key, options = {})
3434 entry = resolve ( locale , key , entry , options )
3535 end
3636
37- entry = entry . dup if entry . is_a? ( String )
38-
3937 count = options [ :count ]
40- entry = pluralize ( locale , entry , count ) if count
4138
42- if entry . nil?
39+ if entry . nil? && ( subtrees? || ! count )
4340 if ( options . key? ( :default ) && !options [ :default ] . nil? ) || !options . key? ( :default )
4441 throw ( :exception , I18n ::MissingTranslation . new ( locale , key , options ) )
4542 end
4643 end
4744
45+ entry = entry . dup if entry . is_a? ( String )
46+ entry = pluralize ( locale , entry , count ) if count
47+
48+ if entry . nil? && !subtrees?
49+ throw ( :exception , I18n ::MissingTranslation . new ( locale , key , options ) )
50+ end
51+
4852 deep_interpolation = options [ :deep_interpolation ]
4953 values = options . except ( *RESERVED_KEYS )
5054 if values
@@ -97,6 +101,10 @@ def lookup(locale, key, scope = [], options = {})
97101 raise NotImplementedError
98102 end
99103
104+ def subtrees?
105+ true
106+ end
107+
100108 # Evaluates defaults.
101109 # If given subject is an Array, it walks the array and returns the
102110 # first translation that can be resolved. Otherwise it tries to resolve
@@ -145,8 +153,7 @@ def resolve(locale, object, subject, options = {})
145153 def pluralize ( locale , entry , count )
146154 return entry unless entry . is_a? ( Hash ) && count
147155
148- key = :zero if count == 0 && entry . has_key? ( :zero )
149- key ||= count == 1 ? :one : :other
156+ key = pluralization_key ( entry , count )
150157 raise InvalidPluralizationData . new ( entry , count , key ) unless entry . has_key? ( key )
151158 entry [ key ]
152159 end
@@ -161,9 +168,7 @@ def pluralize(locale, entry, count)
161168 # each element of the array is recursively interpolated (until it finds a string)
162169 # method interpolates ["yes, %{user}", ["maybe no, %{user}, "no, %{user}"]], :user => "bartuz"
163170 # # => "["yes, bartuz",["maybe no, bartuz", "no, bartuz"]]"
164-
165-
166- def interpolate ( locale , subject , values = { } )
171+ def interpolate ( locale , subject , values = { } )
167172 return subject if values . empty?
168173
169174 case subject
@@ -240,6 +245,11 @@ def translate_localization_format(locale, object, format, options)
240245 end
241246 end
242247 end
248+
249+ def pluralization_key ( entry , count )
250+ key = :zero if count == 0 && entry . has_key? ( :zero )
251+ key ||= count == 1 ? :one : :other
252+ end
243253 end
244254 end
245255end
0 commit comments