A platform for community discussion. Free, open, simple.
FIX: Search tags by localized name when content localization is enabled (#39566)
Jarek Radosz645cb014c0d8
diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rbindex a3848da24fb..b01cc083ff1 100644--- a/lib/discourse_tagging.rb+++ b/lib/discourse_tagging.rb@@ -574,12 +574,19 @@ module DiscourseTaggingbuilder_params[:cleaned_term] = termif opts[:term_type] == DiscourseTagging.term_types[:starts_with]- builder.where("starts_with(LOWER(name), LOWER(:cleaned_term))")- sql.gsub!("/*and_name_like*/", "AND starts_with(LOWER(t.name), LOWER(:cleaned_term))")+ name_match_sql = "starts_with(LOWER(t.name), LOWER(:cleaned_term))"else- builder.where("position(LOWER(:cleaned_term) IN LOWER(t.name)) <> 0")- sql.gsub!("/*and_name_like*/", "AND position(LOWER(:cleaned_term) IN LOWER(t.name)) <> 0")+ name_match_sql = "position(LOWER(:cleaned_term) IN LOWER(t.name)) <> 0"end++ localized_tag_ids = tag_ids_matching_localizations(term, term_type: opts[:term_type])+ if localized_tag_ids.present?+ builder_params[:localized_tag_ids] = localized_tag_ids+ name_match_sql = "(#{name_match_sql} OR t.id IN (:localized_tag_ids))"+ end++ builder.where(name_match_sql)+ sql.gsub!("/*and_name_like*/", "AND #{name_match_sql}")elsesql.gsub!("/*and_name_like*/", "")end@@ -822,6 +829,19 @@ module DiscourseTaggingtruncate ? tag[0...SiteSetting.max_tag_length] : tagend+ def self.tag_ids_matching_localizations(term, term_type:)+ return [] if !SiteSetting.content_localization_enabled++ match_sql =+ if term_type == term_types[:starts_with]+ "starts_with(LOWER(name), LOWER(?))"+ else+ "position(LOWER(?) IN LOWER(name)) <> 0"+ end++ TagLocalization.where(locale: I18n.locale.to_s).where(match_sql, term).pluck(:tag_id)+ end+def self.tags_for_saving(tags_arg, guardian, opts = {})return [] unless guardian.can_tag_topics? && tags_arg.present?diff --git a/spec/services/tags/search_spec.rb b/spec/services/tags/search_spec.rbindex 7bdd7948f7d..82fde0d6e74 100644--- a/spec/services/tags/search_spec.rb+++ b/spec/services/tags/search_spec.rb@@ -248,5 +248,29 @@ RSpec.describe(Tags::Search) doexpect(result[:required_tag_group][:min_count]).to eq(1)endend++ context "with content localization enabled" do+ fab!(:strategy_tag) { Fabricate(:tag, name: "strategy", locale: "en") }+ fab!(:strategy_ja_localization) do+ Fabricate(:tag_localization, tag: strategy_tag, locale: "ja", name: "戦略")+ end++ let(:params) { { q: "戦" } }++ before do+ SiteSetting.content_localization_enabled = true+ SiteSetting.content_localization_supported_locales = "en|ja"+ end++ it "matches tags by their localized name in the current locale" do+ I18n.with_locale(:ja) do+ expect(result[:tags].map { |t| t[:name] }).to contain_exactly("戦略")+ end+ end++ it "does not match localizations from other locales" do+ I18n.with_locale(:en) { expect(result[:tags].map { |t| t[:name] }).to be_empty }+ end+ endendenddiff --git a/spec/system/page_objects/components/select_kit.rb b/spec/system/page_objects/components/select_kit.rbindex 52db8c04ddb..b3a9240b789 100644--- a/spec/system/page_objects/components/select_kit.rb+++ b/spec/system/page_objects/components/select_kit.rb@@ -114,6 +114,11 @@ module PageObjectsdef search(value = nil)expanded_component.find(".select-kit-filter .filter-input").fill_in(with: value)+ wait_for_loaded+ end++ def wait_for_loaded+ has_no_css?("#{@context}.is-loading")enddef select_row_by_value(value)