Skip to content

Commit 878e980

Browse files
authored
Merge pull request rails#35571 from KaanOzkan/ko-parameterize-locale
Add locale option to #parameterize
2 parents f1b8bb4 + bc9711f commit 878e980

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

activesupport/lib/active_support/core_ext/string/inflections.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ def deconstantize
162162

163163
# Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
164164
#
165+
# If the optional parameter +locale+ is specified,
166+
# the word will be parameterized as a word of that language.
167+
# By default, this parameter is set to <tt>nil</tt> and it will use
168+
# configured I18n.locale
169+
#
165170
# class Person
166171
# def to_param
167172
# "#{id}-#{name.parameterize}"
@@ -187,8 +192,8 @@ def deconstantize
187192
#
188193
# <%= link_to(@person.name, person_path) %>
189194
# # => <a href="/person/1-Donald-E-Knuth">Donald E. Knuth</a>
190-
def parameterize(separator: "-", preserve_case: false)
191-
ActiveSupport::Inflector.parameterize(self, separator: separator, preserve_case: preserve_case)
195+
def parameterize(separator: "-", preserve_case: false, locale: nil)
196+
ActiveSupport::Inflector.parameterize(self, separator: separator, preserve_case: preserve_case, locale: locale)
192197
end
193198

194199
# Creates the name of a table like Rails does for models to table names. This method

activesupport/lib/active_support/inflector/transliterate.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,18 @@ module Inflector
5151
#
5252
# Now you can have different transliterations for each locale:
5353
#
54-
# I18n.locale = :en
55-
# transliterate('Jürgen')
54+
# transliterate('Jürgen', locale: :en)
5655
# # => "Jurgen"
5756
#
58-
# I18n.locale = :de
59-
# transliterate('Jürgen')
57+
# transliterate('Jürgen', locale: :de)
6058
# # => "Juergen"
61-
def transliterate(string, replacement = "?")
59+
def transliterate(string, replacement = "?", locale: nil)
6260
raise ArgumentError, "Can only transliterate strings. Received #{string.class.name}" unless string.is_a?(String)
6361

6462
I18n.transliterate(
6563
ActiveSupport::Multibyte::Unicode.tidy_bytes(string).unicode_normalize(:nfc),
66-
replacement: replacement
64+
replacement: replacement,
65+
locale: locale
6766
)
6867
end
6968

@@ -89,9 +88,9 @@ def transliterate(string, replacement = "?")
8988
# parameterize("^très|Jolie-- ", separator: "_") # => "tres_jolie--"
9089
# parameterize("^très_Jolie-- ", separator: ".") # => "tres_jolie--"
9190
#
92-
def parameterize(string, separator: "-", preserve_case: false)
91+
def parameterize(string, separator: "-", preserve_case: false, locale: nil)
9392
# Replace accented chars with their ASCII equivalents.
94-
parameterized_string = transliterate(string)
93+
parameterized_string = transliterate(string, locale)
9594

9695
# Turn unwanted chars into the separator.
9796
parameterized_string.gsub!(/[^a-z0-9\-_]+/i, separator)

0 commit comments

Comments
 (0)