Skip to content

Commit eec1887

Browse files
committed
Use % string interpolation for translations
Instead of traversing the string to replace multiple options, use built-in ruby methods to interpolate string templates in a single pass.
1 parent 4d90535 commit eec1887

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/ice_cube/null_i18n.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@ def self.t(key, options = {})
77

88
base = base[options[:count] == 1 ? "one" : "other"] if options[:count]
99

10-
if base.is_a?(Hash)
11-
return base.each_with_object({}) do |(key, value), hash|
12-
hash[key.is_a?(String) ? key.to_sym : key] = value
10+
case base
11+
when Hash
12+
base.each_with_object({}) do |(k, v), hash|
13+
hash[k.is_a?(String) ? k.to_sym : k] = v
1314
end
15+
when Array
16+
base.each_with_index.each_with_object({}) do |(v, k), hash|
17+
hash[k] = v
18+
end
19+
else
20+
return base unless base.include?('%{')
21+
base % options
1422
end
15-
16-
options.reduce(base) { |result, (find, replace)| result.gsub("%{#{find}}", "#{replace}") }
1723
end
1824

1925
def self.l(date_or_time, options = {})

0 commit comments

Comments
 (0)