0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 12:50:27 +08:00
discourse/app/controllers/post_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

77 lines
2.3 KiB
Ruby
Vendored

# frozen_string_literal: true
class PostLocalizationsController < ApplicationController
before_action :ensure_logged_in
def show
post_id = params[:id] || params[:post_id]
raise ActionController::ParameterMissing.new(:id) if post_id.blank?
post = Post.find_by(id: post_id)
return render_json_error(I18n.t("not_found"), status: :not_found) if post.blank?
guardian.ensure_can_localize_post!(post)
post_localizations = PostLocalization.where(post_id: post.id)
topic_localizations_by_locale = {}
if post.is_first_post?
TopicLocalization
.where(topic_id: post.topic_id)
.each { |tl| topic_localizations_by_locale[tl.locale] = tl }
end
post_localizations.each do |pl|
pl.define_singleton_method(:topic_localization) { topic_localizations_by_locale[pl.locale] }
end
render json: {
post_localizations:
ActiveModel::ArraySerializer.new(
post_localizations,
each_serializer: PostLocalizationSerializer,
root: false,
).as_json,
},
status: :ok
end
def create_or_update
post_id, locale, raw = params.require(%i[post_id locale raw])
post = Post.find_by(id: post_id)
raise Discourse::NotFound unless post
guardian.ensure_can_localize_post!(post)
localization = PostLocalization.find_by(post_id: post.id, locale:)
if localization
PostLocalizationUpdater.update(post:, locale:, raw:, user: current_user)
render json: success_json, status: :ok
else
PostLocalizationCreator.create(post:, locale:, raw:, user: current_user)
render json: success_json, status: :created
end
end
def update_locale
post = Post.find_by(id: params[:post_id])
raise Discourse::NotFound unless post
updated_post =
PostLocaleUpdater.update(post:, locale: params.fetch(:locale).presence, user: current_user)
render json: { locale: updated_post.locale }, status: :ok
end
def destroy
post_id, locale = params.require(%i[post_id locale])
post = Post.find_by(id: post_id)
raise Discourse::NotFound unless post
guardian.ensure_can_localize_post!(post)
PostLocalizationDestroyer.destroy(post:, locale:, acting_user: current_user)
head :no_content
end
end