discourse/app/controllers/post_localizations_controller.rb
Keegan George 4d380a28e7
FEATURE: Add post language on creating a new post (#33160)
This is a second attempt at:
https://github.com/discourse/discourse/pull/33001

We had to [revert the
commit](https://github.com/discourse/discourse/pull/33157) because it
was performing a site-setting check at boot time, which is prone to
issues and not allowed.

This PR:
- re-introduces the changes in the original PR
- a fix by not performing a site-setting check at boot time (verified
by: `SKIP_DB_AND_REDIS=1 DISCOURSE_DEV_DB="nonexist" bin/rails runner
"puts 'booted'"` locally and should be caught by the new CI check
introduced here: https://github.com/discourse/discourse/pull/33158)
- adds a fix to the translation editor to not show the original post
locale in the dropdown, as well as adding an indicator of what the
original post locale is in a small badge in the header:
- ![Screenshot 2025-06-11 at 08 42
36](https://github.com/user-attachments/assets/5f0944c5-ec4d-40b3-b97f-25b1fcab8329)
2025-06-11 10:39:01 -07:00

74 lines
2 KiB
Ruby

# frozen_string_literal: true
class PostLocalizationsController < ApplicationController
before_action :ensure_logged_in
def show
guardian.ensure_can_localize_content!
params.require(:post_id)
post = Post.find_by(id: params[:post_id])
return render json_error(I18n.t("not_found"), status: :not_found) if post.blank?
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
guardian.ensure_can_localize_content!
params.require(%i[post_id locale raw])
localization = PostLocalization.find_by(post_id: params[:post_id], locale: params[:locale])
if localization
PostLocalizationUpdater.update(
post_id: params[:post_id],
locale: params[:locale],
raw: params[:raw],
user: current_user,
)
render json: success_json, status: :ok
else
PostLocalizationCreator.create(
post_id: params[:post_id],
locale: params[:locale],
raw: params[:raw],
user: current_user,
)
render json: success_json, status: :created
end
end
def destroy
guardian.ensure_can_localize_content!
params.require(%i[post_id locale])
PostLocalizationDestroyer.destroy(
post_id: params[:post_id],
locale: params[:locale],
acting_user: current_user,
)
head :no_content
end
end