0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 17:25:34 +08:00
discourse/app/controllers/topic_localizations_controller.rb
Natalie Tay cb739f7f2d
UX: Allow user to set post and topic title language when manually creating post translations in modal (#41734)
Related:
https://meta.discourse.org/t/manual-localization-doesnt-seem-to-be-working/407319

Users have gotten tripped over and over again when manually inserting
translations for posts, not knowing they need to set the post language
first before the translation is shown (explained
[here](https://meta.discourse.org/t/manual-localization-doesnt-seem-to-be-working/407319/7?u=nat)).

This PR adds the ability to allow the user to set the post language in
the modal when they are working with translations, highlighting the fact
that they need to set the post language before translations can be shown
2026-07-16 10:43:39 +08:00

61 lines
1.8 KiB
Ruby
Vendored

# frozen_string_literal: true
class TopicLocalizationsController < ApplicationController
before_action :ensure_logged_in
def show
topic = Topic.find_by(id: params[:topic_id])
raise Discourse::NotFound unless topic
guardian.ensure_can_see!(topic)
localization = TopicLocalization.find_by(topic_id: topic.id, locale: params[:locale])
raise Discourse::NotFound unless localization
render json: TopicLocalizationSerializer.new(localization, root: false)
end
def create_or_update
topic_id, locale, title = params.require(%i[topic_id locale title])
topic = Topic.find_by(id: topic_id)
raise Discourse::NotFound unless topic
guardian.ensure_can_localize_topic!(topic)
topic_localization = TopicLocalization.find_by(topic_id: topic.id, locale: params[:locale])
if topic_localization
TopicLocalizationUpdater.update(
topic:,
locale: params[:locale],
title: params[:title],
user: current_user,
)
render json: success_json, status: :ok
else
TopicLocalizationCreator.create(topic:, locale:, title:, user: current_user)
render json: success_json, status: :created
end
end
def update_locale
topic = Topic.find_by(id: params[:topic_id])
raise Discourse::NotFound unless topic
updated_topic =
TopicLocaleUpdater.update(topic:, locale: params.fetch(:locale).presence, user: current_user)
render json: { locale: updated_topic.locale }, status: :ok
end
def destroy
topic_id, locale = params.require(%i[topic_id locale])
topic = Topic.find_by(id: topic_id)
raise Discourse::NotFound unless topic
guardian.ensure_can_localize_topic!(topic)
TopicLocalizationDestroyer.destroy(topic:, locale:, acting_user: current_user)
head :no_content
end
end