FEATURE: Better handling of quotation marks in site text search

It also matches 3 dots with the ellipsis symbol.
This commit is contained in:
Gerhard Schlager
2018-11-10 01:17:07 +01:00
parent c7377e2f2d
commit 7c4d4331bc
6 changed files with 69 additions and 7 deletions

View File

@@ -70,7 +70,7 @@ module I18n
target = opts[:backend] || backend
results = opts[:overridden] ? {} : target.search(config.locale, query)
regexp = /#{Regexp.escape(query)}/i
regexp = I18n::Backend::DiscourseI18n.create_search_regexp(query)
(overrides_by_locale(locale) || {}).each do |k, v|
results.delete(k)
results[k] = v if (k =~ regexp || v =~ regexp)

View File

@@ -39,11 +39,22 @@ module I18n
false
end
def self.create_search_regexp(query, as_string: false)
regexp = Regexp.escape(query)
regexp.gsub!(/[']/, "[']")
regexp.gsub!(/["“”„«»]/, '["“”„«»]')
regexp.gsub!(/(?:\\\.\\\.\\\.|…)/, '(?:\.\.\.|…)')
as_string ? regexp : /#{regexp}/i
end
def search(locale, query)
results = {}
regexp = self.class.create_search_regexp(query)
fallbacks(locale).each do |fallback|
find_results(/#{Regexp.escape(query)}/i, results, translations[fallback])
find_results(regexp, results, translations[fallback])
end
results