Skip to content

Commit

Permalink
Fix how UTF8 is handled inside I18n.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 26, 2010
1 parent 6c13777 commit e8d5820
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 29 deletions.
4 changes: 2 additions & 2 deletions ci/Gemfile.rails-3.x.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ PLATFORMS
ruby

DEPENDENCIES
activerecord (~> 3)
activesupport (~> 3)
activerecord (~> 3.0.0)
activesupport (~> 3.0.0)
mocha
sqlite3-ruby
41 changes: 14 additions & 27 deletions lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,25 +150,23 @@ def pluralize(locale, entry, count)
def interpolate(locale, string, values = {})
return string unless string.is_a?(::String) && !values.empty?

preserve_encoding(string) do
string = string.gsub(DEPRECATED_INTERPOLATION_SYNTAX_PATTERN) do
escaped, key = $1, $2.to_sym
if escaped
"{{#{key}}}"
else
warn_syntax_deprecation!(locale, string)
"%{#{key}}"
end
end

values.each do |key, value|
value = value.call(values) if interpolate_lambda?(value, string, key)
value = value.to_s unless value.is_a?(::String)
values[key] = value
string = string.gsub(DEPRECATED_INTERPOLATION_SYNTAX_PATTERN) do
escaped, key = $1, $2.to_sym
if escaped
"{{#{key}}}"
else
warn_syntax_deprecation!(locale, string)
"%{#{key}}"
end
end

string % values
values.each do |key, value|
value = value.call(values) if interpolate_lambda?(value, string, key)
value = value.to_s unless value.is_a?(::String)
values[key] = value
end

string % values
rescue KeyError => e
if string =~ RESERVED_KEYS_PATTERN
raise ReservedInterpolationKey.new($1.to_sym, string)
Expand All @@ -177,17 +175,6 @@ def interpolate(locale, string, values = {})
end
end

def preserve_encoding(string)
if string.respond_to?(:encoding)
encoding = string.encoding
result = yield
result.force_encoding(encoding) if result.respond_to?(:force_encoding)
result
else
yield
end
end

# returns true when the given value responds to :call and the key is
# an interpolation placeholder in the given string
def interpolate_lambda?(object, string, key)
Expand Down
22 changes: 22 additions & 0 deletions test/backend/simple_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# encoding: utf-8

$:.unshift(File.expand_path(File.dirname(__FILE__) + '/../')); $:.uniq!
require 'test_helper'

Expand Down Expand Up @@ -78,4 +79,25 @@ def setup
I18n.backend.reload!
assert_equal I18n.backend.initialized?, false
end

# Encoding
if "string".respond_to?(:force_encoding)
test "ASCII strings in the backend should be encoded to UTF8 if interpolation options are in UTF8" do
I18n.backend.store_translations 'en', 'encoding' => ('%{who} let me go'.force_encoding("ASCII"))
result = I18n.t 'encoding', :who => "måmmå miå"
assert_equal Encoding::UTF_8, result.encoding
end

test "UTF8 strings in the backend are still returned as UTF8" do
I18n.backend.store_translations 'en', 'encoding' => 'måmmå miå %{what}'
result = I18n.t 'encoding', :what => 'let me go'.force_encoding("ASCII")
assert_equal Encoding::UTF_8, result.encoding
end

test "UTF8 strings in the backend are still returned as UTF8 even with numbers" do
I18n.backend.store_translations 'en', 'encoding' => '%{count} times: måmmå miå'
result = I18n.t 'encoding', :count => 3
assert_equal Encoding::UTF_8, result.encoding
end
end
end

0 comments on commit e8d5820

Please sign in to comment.