Fix bug with parametrize when `locale` is passed

Also add tests for parametrize and transliterate
This commit is contained in:
Sharang Dashputre 2019-03-12 00:39:58 +05:30
parent 878e98091d
commit 818437c366
4 changed files with 19 additions and 1 deletions

View File

@ -90,7 +90,7 @@ module ActiveSupport
#
def parameterize(string, separator: "-", preserve_case: false, locale: nil)
# Replace accented chars with their ASCII equivalents.
parameterized_string = transliterate(string, locale)
parameterized_string = transliterate(string, locale: locale)
# Turn unwanted chars into the separator.
parameterized_string.gsub!(/[^a-z0-9\-_]+/i, separator)

View File

@ -206,6 +206,12 @@ class StringInflectionsTest < ActiveSupport::TestCase
end
end
def test_parameterize_with_locale
word = "Fünf autos"
I18n.backend.store_translations(:de, i18n: { transliterate: { rule: { "ü" => "ue" } } })
assert_equal("fuenf-autos", word.parameterize(locale: :de))
end
def test_humanize
UnderscoreToHuman.each do |underscore, human|
assert_equal(human, underscore.humanize)

View File

@ -304,6 +304,12 @@ class InflectorTest < ActiveSupport::TestCase
end
end
def test_parameterize_with_locale
word = "Fünf autos"
I18n.backend.store_translations(:de, i18n: { transliterate: { rule: { "ü" => "ue" } } })
assert_equal("fuenf-autos", ActiveSupport::Inflector.parameterize(word, locale: :de))
end
def test_classify
ClassNameToTableName.each do |class_name, table_name|
assert_equal(class_name, ActiveSupport::Inflector.classify(table_name))

View File

@ -30,6 +30,12 @@ class TransliterateTest < ActiveSupport::TestCase
I18n.locale = default_locale
end
def test_transliterate_respects_the_locale_argument
char = [117, 776].pack("U*") # "ü" as ASCII "u" plus COMBINING DIAERESIS
I18n.backend.store_translations(:de, i18n: { transliterate: { rule: { "ü" => "ue" } } })
assert_equal "ue", ActiveSupport::Inflector.transliterate(char, locale: :de)
end
def test_transliterate_should_allow_a_custom_replacement_char
assert_equal "a*b", ActiveSupport::Inflector.transliterate("a索b", "*")
end