A platform for community discussion. Free, open, simple.
DEV: Use localizable concerns across post, topic, categories (#34137)
Natalie Tayf4e08421aedf
diff --git a/app/models/category.rb b/app/models/category.rbindex 2fd277caf97..31addb9a0ee 100644--- a/app/models/category.rb+++ b/app/models/category.rb@@ -16,6 +16,7 @@ class Category < ActiveRecord::Baseinclude CategoryHashtaginclude AnonCacheInvalidatorinclude HasDestroyedWebHook+ include LocalizableSLUG_REF_SEPARATOR = ":"DEFAULT_TEXT_COLORS = %w[FFFFFF 000000]diff --git a/app/models/category_localization.rb b/app/models/category_localization.rbindex 615b35f3edd..1754946515c 100644--- a/app/models/category_localization.rb+++ b/app/models/category_localization.rb@@ -1,6 +1,8 @@# frozen_string_literal: trueclass CategoryLocalization < ActiveRecord::Base+ include LocaleMatchable+belongs_to :categoryvalidates :locale, presence: true, length: { maximum: 20 }diff --git a/app/models/concerns/locale_matchable.rb b/app/models/concerns/locale_matchable.rbnew file mode 100644index 00000000000..662a52425c4--- /dev/null+++ b/app/models/concerns/locale_matchable.rb@@ -0,0 +1,13 @@+# frozen_string_literal: true++module LocaleMatchable+ extend ActiveSupport::Concern++ included do+ scope :matching_locale,+ ->(locale) do+ regionless_locale = locale.to_s.split("_").first+ where("locale LIKE ?", "#{regionless_locale}%")+ end+ end+enddiff --git a/app/models/concerns/localizable.rb b/app/models/concerns/localizable.rbnew file mode 100644index 00000000000..a245f51d7f9--- /dev/null+++ b/app/models/concerns/localizable.rb@@ -0,0 +1,18 @@+# frozen_string_literal: true++module Localizable+ extend ActiveSupport::Concern++ included { has_many :localizations, class_name: "#{model_name}Localization", dependent: :destroy }++ def get_localization(locale = I18n.locale)+ locale_str = locale.to_s.sub("-", "_")++ # prioritise exact match+ if match = localizations.find { |l| l.locale == locale_str }+ return match+ end++ localizations.find { |l| LocaleNormalizer.is_same?(l.locale, locale_str) }+ end+enddiff --git a/app/models/post.rb b/app/models/post.rbindex 7157cae2a22..9314325ca7b 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -9,6 +9,7 @@ class Post < ActiveRecord::Baseinclude Searchableinclude HasCustomFieldsinclude LimitedEdit+ include Localizableself.ignored_columns = ["avg_time", # TODO: Remove when 20240212034010_drop_deprecated_columns has been promoted to pre-deploy@@ -67,8 +68,6 @@ class Post < ActiveRecord::Basehas_many :user_actions, foreign_key: :target_post_id- has_many :post_localizations, dependent: :destroy-belongs_to :image_upload, class_name: "Upload"has_many :post_hotlinked_media, dependent: :destroy, class_name: "PostHotlinkedMedia"@@ -1343,17 +1342,6 @@ class Post < ActiveRecord::BaseLocaleNormalizer.is_same?(locale, I18n.locale)end- def get_localization(locale = I18n.locale)- locale_str = locale.to_s.sub("-", "_")-- # prioritise exact match- if match = post_localizations.find { |l| l.locale == locale_str }- return match- end-- post_localizations.find { |l| LocaleNormalizer.is_same?(l.locale, locale_str) }- end-privatedef parse_quote_into_arguments(quote)diff --git a/app/models/post_localization.rb b/app/models/post_localization.rbindex 150fff545f5..0ec077b7be3 100644--- a/app/models/post_localization.rb+++ b/app/models/post_localization.rb@@ -1,6 +1,8 @@# frozen_string_literal: trueclass PostLocalization < ActiveRecord::Base+ include LocaleMatchable+belongs_to :postvalidates :post_version, presence: truediff --git a/app/models/topic.rb b/app/models/topic.rbindex b5220ef92ea..2187e61dbc6 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -11,6 +11,7 @@ class Topic < ActiveRecord::Baseinclude Trashableinclude Searchableinclude LimitedEdit+ include Localizableextend ForwardableEXTERNAL_ID_MAX_LENGTH = 50@@ -31,8 +32,6 @@ class Topic < ActiveRecord::Baseattr_accessor :allowed_user_ids, :allowed_group_ids, :tags_changed, :includes_destination_category- has_many :topic_localizations, dependent: :destroy-def self.max_fancy_title_length400end@@ -2137,24 +2136,13 @@ class Topic < ActiveRecord::Baseenddef has_localization?(locale = I18n.locale)- topic_localizations.exists?(locale: locale.to_s.sub("-", "_"))+ localizations.exists?(locale: locale.to_s.sub("-", "_"))enddef in_user_locale?LocaleNormalizer.is_same?(locale, I18n.locale)end- def get_localization(locale = I18n.locale)- locale_str = locale.to_s.sub("-", "_")-- # prioritise exact match- if match = topic_localizations.find { |l| l.locale == locale_str }- return match- end-- topic_localizations.find { |l| LocaleNormalizer.is_same?(l.locale, locale_str) }- end-privatedef invite_to_private_message(invited_by, target_user, guardian)diff --git a/app/models/topic_list.rb b/app/models/topic_list.rbindex b356ee59e63..ce06f6b7959 100644--- a/app/models/topic_list.rb+++ b/app/models/topic_list.rb@@ -139,7 +139,7 @@ class TopicList{ category: :parent_category },]- topic_preloader_associations << :topic_localizations if SiteSetting.content_localization_enabled+ topic_preloader_associations << :localizations if SiteSetting.content_localization_enabledDiscoursePluginRegistry.topic_preloader_associations.each do |a|fields = a[:fields]diff --git a/app/models/topic_localization.rb b/app/models/topic_localization.rbindex 3429886d67f..65412531c48 100644--- a/app/models/topic_localization.rb+++ b/app/models/topic_localization.rb@@ -1,6 +1,8 @@# frozen_string_literal: trueclass TopicLocalization < ActiveRecord::Base+ include LocaleMatchable+belongs_to :topicvalidates :locale, presence: true, length: { maximum: 20 }diff --git a/app/serializers/post_serializer.rb b/app/serializers/post_serializer.rbindex b3d3ae8b881..77c63c97fa3 100644--- a/app/serializers/post_serializer.rb+++ b/app/serializers/post_serializer.rb@@ -656,11 +656,11 @@ class PostSerializer < BasicPostSerializerenddef has_post_localizations- object.post_localizations.any?+ object.localizations.any?enddef post_localizations_count- object.post_localizations.size+ object.localizations.sizeenddef include_has_post_localizations?diff --git a/lib/topic_view.rb b/lib/topic_view.rbindex 787515bc1ff..7c8c82dc1f2 100644--- a/lib/topic_view.rb+++ b/lib/topic_view.rb@@ -929,7 +929,7 @@ class TopicView:deleted_by,:incoming_email,:image_upload,- :post_localizations,+ :localizations,)@posts = @posts.includes({ user: :user_status }) if SiteSetting.enable_user_statusdiff --git a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rbindex 7b55ee228d6..bde9f63c9df 100644--- a/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb+++ b/plugins/discourse-ai/app/jobs/regular/detect_translate_post.rb@@ -41,9 +41,7 @@ module Jobslocales.each do |locale|next if LocaleNormalizer.is_same?(locale, detected_locale)-- regionless_locale = locale.split("_").first- exists = post.post_localizations.where("locale LIKE ?", "#{regionless_locale}%").exists?+ exists = post.localizations.matching_locale(locale).exists?if exists && !DiscourseAi::Translation::PostLocalizer.has_relocalize_quota?(post, locale)nextdiff --git a/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb b/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rbindex e5cc1543613..2afe1c5c3c2 100644--- a/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb+++ b/plugins/discourse-ai/app/jobs/regular/detect_translate_topic.rb@@ -39,8 +39,7 @@ module Jobslocales.each do |locale|next if LocaleNormalizer.is_same?(locale, detected_locale)- regionless_locale = locale.split("_").first- next if topic.topic_localizations.where("locale LIKE ?", "#{regionless_locale}%").exists?+ next if topic.localizations.matching_locale(locale).exists?beginDiscourseAi::Translation::TopicLocalizer.localize(topic, locale)diff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rbindex aa858e00dbb..04fab275eeb 100644--- a/spec/lib/topic_query_spec.rb+++ b/spec/lib/topic_query_spec.rb@@ -2400,7 +2400,7 @@ RSpec.describe TopicQuery dotopic_query = TopicQuery.new(user)topic_list = topic_query.list_latest- expect(topic_list.topics.first.association(:topic_localizations).loaded?).to eq(true)+ expect(topic_list.topics.first.association(:localizations).loaded?).to eq(true)queries =track_sql_queries { topic_list.topics.each { |topic| topic.get_localization&.fancy_title } }