From 3352e10275c7399512f472f39c56831fb88f2365 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20A=2E=20Ceccon?= Date: Tue, 21 Feb 2017 14:30:48 -0300 Subject: [PATCH] Fix transliteration to default replacement char MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Commit f6a4294c introduced a bug where non-ASCII chars not present in the transliteration map would be removed from the output, instead of being replaced by the default replacement char, violating the behaviour stated in the documentation: I18n.transliterate("日本語") # => "" (should be "???") This fixes the issue while preserving the optimization introduced by that commit. --- lib/i18n/backend/transliterator.rb | 3 ++- test/i18n_test.rb | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/i18n/backend/transliterator.rb b/lib/i18n/backend/transliterator.rb index 538dd600..2617bcf2 100644 --- a/lib/i18n/backend/transliterator.rb +++ b/lib/i18n/backend/transliterator.rb @@ -75,7 +75,8 @@ def initialize(rule = nil) add rule if rule end - def transliterate(string, replacement = DEFAULT_REPLACEMENT_CHAR) + def transliterate(string, replacement = nil) + replacement ||= DEFAULT_REPLACEMENT_CHAR string.gsub(/[^\x00-\x7f]/u) do |char| approximations[char] || replacement end diff --git a/test/i18n_test.rb b/test/i18n_test.rb index dd9d6b15..ac8a4498 100644 --- a/test/i18n_test.rb +++ b/test/i18n_test.rb @@ -351,6 +351,10 @@ def call(exception, locale, key, options); key; end end end + test "transliterate non-ASCII chars not in map with default replacement char" do + assert_equal "???", I18n.transliterate("日本語") + end + test "I18n.locale_available? returns true when the passed locale is available" do I18n.available_locales = [:en, :de] assert_equal true, I18n.locale_available?(:de)