diff options
-rw-r--r-- | activesupport/lib/active_support/inflector/transliterate.rb | 4 | ||||
-rw-r--r-- | activesupport/test/transliterate_test.rb | 8 |
2 files changed, 12 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/inflector/transliterate.rb b/activesupport/lib/active_support/inflector/transliterate.rb index ec6e9ccb59..61651ba101 100644 --- a/activesupport/lib/active_support/inflector/transliterate.rb +++ b/activesupport/lib/active_support/inflector/transliterate.rb @@ -56,8 +56,12 @@ module ActiveSupport # # transliterate('Jürgen', locale: :de) # # => "Juergen" + # + # Transliteration of ASCII-8BIT / BINARY strings is not + # supported and will raise an ArgumentError. def transliterate(string, replacement = "?", locale: nil) raise ArgumentError, "Can only transliterate strings. Received #{string.class.name}" unless string.is_a?(String) + raise ArgumentError, "Can not transliterate strings with ASCII-8BIT encoding" if string.encoding == ::Encoding::ASCII_8BIT I18n.transliterate( ActiveSupport::Multibyte::Unicode.tidy_bytes(string).unicode_normalize(:nfc), diff --git a/activesupport/test/transliterate_test.rb b/activesupport/test/transliterate_test.rb index 9e29a93ea0..620bb20305 100644 --- a/activesupport/test/transliterate_test.rb +++ b/activesupport/test/transliterate_test.rb @@ -57,4 +57,12 @@ class TransliterateTest < ActiveSupport::TestCase end assert_equal "Can only transliterate strings. Received Object", exception.message end + + def test_transliterate_handles_ascci_8bit_strings + ascii_8bit_string = "A".b + exception = assert_raises ArgumentError do + ActiveSupport::Inflector.transliterate(ascii_8bit_string) + end + assert_equal "Can not transliterate strings with ASCII-8BIT encoding", exception.message + end end |