discourse/lib/guardian/localization_guardian.rb
Natalie Tay 5322295b8c
FEATURE: Introduce tag localizations with API, without UI yet (#36754)
This commit introduces tag localizations... but only on the API. We can go
down the stack:

### tag localizations controller
This adds the ability to GET, PUT, DELETE a tag localization, and checks
if the user can see the tag (tag group permissions) and localize based
on the `content_localization_allowed_groups` site setting. This also
adds each accompanying
- serializer
- routes
- guardian
- service

Note, this controller was created but I am not yet sure if we will be
using it in tags settings. Category settings make use of a big endpoint
that involves ALL category settings, so updating category localizations
is part of updating a category's settings. We will know more once I
implement the tags settings page that will come after slugs are
introduced.

### ai
On the AI side this adds the required jobs (backfill detection, backfill
localization), and associated services (tag candidates, localizer).

Localization for tags involve both `name` and `description`, and uses
the existing `ShortTextTranslator` and its quota.

_____

Reviewer note: This will look very similar to the other models, but I
chose this duplication over small if-elses scattered everywhere. We can
def revisit dedup if needed.
2025-12-18 18:25:58 +08:00

47 lines
1.6 KiB
Ruby

# frozen_string_literal: true
module LocalizationGuardian
# Users that pass this guard are allowed to localize all content site-wide
def can_localize_content?
return false if !SiteSetting.content_localization_enabled
return false if anonymous?
@user.in_any_groups?(SiteSetting.content_localization_allowed_groups_map)
end
def can_localize_post?(post_or_post_id)
return false if !SiteSetting.content_localization_enabled
return false if anonymous?
post = post_or_post_id.is_a?(Post) ? post_or_post_id : Post.find_by(id: post_or_post_id)
return false if !can_see_post?(post)
return true if @user.in_any_groups?(SiteSetting.content_localization_allowed_groups_map)
return false if !SiteSetting.content_localization_allow_author_localization
post.user_id == @user.id
end
def can_localize_topic?(topic_or_topic_id)
return false if !SiteSetting.content_localization_enabled
return false if anonymous?
topic =
topic_or_topic_id.is_a?(Topic) ? topic_or_topic_id : Topic.find_by(id: topic_or_topic_id)
return false if !can_see_topic?(topic)
return true if @user.in_any_groups?(SiteSetting.content_localization_allowed_groups_map)
return false if !SiteSetting.content_localization_allow_author_localization
topic.user_id == @user.id
end
def can_localize_tag?(tag_or_tag_id)
return false if !SiteSetting.content_localization_enabled
return false if anonymous?
return false if !@user.in_any_groups?(SiteSetting.content_localization_allowed_groups_map)
tag = tag_or_tag_id.is_a?(Tag) ? tag_or_tag_id : Tag.find_by(id: tag_or_tag_id)
!hidden_tag_names.include?(tag.name)
end
end