The discussion revolves around modifying Discourse’s URL generation to support non-Latin characters. the220hq proposed changing the encoded_generator function in lib/slug.rb to use I18n.transliterate for better support of non-Latin characters. The modified function would generate friendly URLs without escape strings.
lhc_fl pointed out that some apps may not support non-Latin text links, implying that compatibility should be considered.
the220hq further explored customization options for transliteration using locale settings in locale.yml files.
However, the220hq encountered issues with their code changes being reverted after running ./launcher destroy app && ./launcher start app.
After experimenting, the220hq discovered the following best practices:
Avoid modifying ascii_generator as it can cause issues with template/theme/css compilation.
Use encoded_generator and hard-code locale settings if necessary, while keeping the system default locale setting (English).
Add customizations to slug.rb in a .yml file to persist changes between app rebuilds.
I tried encoded but actually the URL can be seen good on address of internet browser. However, when copy or send it, the real URL is a escape string and not friendly anymore.
I check the lib/slug.rb and suggest to change encoded_generator function likes this:
def self.encoded_generator(string, downcase: true)
# This generator will sanitize almost all special characters,
# including reserved characters from RFC3986.
# See also URI::REGEXP::PATTERN.
string = I18n.transliterate(string, :locale => SiteSetting.default_locale)
string = string.strip.gsub(/\s+/, "-").gsub(CHAR_FILTER_REGEXP, "")
string = string.downcase if downcase
string
#CGI.escape(string)
end
I tried with Viet Nam locale and got the desired results. Hope it works with another language.
I’m not a ruby code writer, but I read the document and find this:
Setting a Hash in <locale>.yml:
i18n:
transliterate:
rule:
ü: "ue"
ö: "oe"
So this is the way for customize transliterate in setting, at the end of function, we got Latin text (friendly url) only. I tried with my native language, and no need to define custom hash setting.