Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV: Use backend plugin api instead of frontend apis to override translation values #210

Merged
merged 5 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .discourse-compatibility
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
< 3.5.0.beta1-dev: f7d6ca6b32c0cff27422dd9f7583f41b203c0c57
< 3.5.0.beta1-dev: 65d7ea2dbc9e7bf276e5ac3f9e23c2111e64e278
< 3.4.0.beta3-dev: b4cf3a065884816fa3f770248c2bf908ba65d8ac
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

< 3.4.0.beta1-dev: 5346b4bafba2c2fb817f030a473b7bbca97b909c
< 3.3.0.beta1-dev: 6750e10a6d9dfd3fc2c9a0cac5a83aca1a8ee401
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,6 @@ function initializeTranslation(api) {
(currentUser || siteSettings.experimental_anon_language_switcher)
) {
api.renderInOutlet("topic-navigation", ShowOriginalContent);
api.decorateCookedElement((cookedElement, helper) => {
if (helper) {
const translatedCooked = helper.getModel().get("translated_cooked");
if (translatedCooked) {
cookedElement.innerHTML = translatedCooked;
} else {
// this experimental feature does not yet support
// translating individual untranslated posts
}
}
});

api.registerModelTransformer("topic", (topics) => {
topics.forEach((topic) => {
if (topic.translated_title) {
topic.set("fancy_title", topic.translated_title);
}
});
});
}

if (!siteSettings.experimental_topic_translation) {
Expand Down
32 changes: 24 additions & 8 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,33 @@ module ::DiscourseTranslator
scope.can_translate?(object)
end

add_to_serializer :post, :translated_cooked do
if !SiteSetting.experimental_topic_translation || scope.request.params["show"] == "original"
return nil
register_modifier(:basic_post_serializer_cooked) do |cooked, serializer|
if !SiteSetting.experimental_topic_translation ||
serializer.scope.request.params["show"] == "original" ||
serializer.object.detected_locale == I18n.locale.to_s.gsub("_", "-")
cooked
else
serializer.object.translation_for(I18n.locale).presence
end
object.translation_for(I18n.locale) || nil
end

add_to_serializer :topic_view, :translated_title do
if !SiteSetting.experimental_topic_translation || scope.request.params["show"] == "original"
return nil
register_modifier(:topic_serializer_fancy_title) do |fancy_title, serializer|
if !SiteSetting.experimental_topic_translation ||
serializer.scope.request.params["show"] == "original" ||
serializer.object.detected_locale == I18n.locale.to_s.gsub("_", "-")
fancy_title
else
serializer.object.translation_for(I18n.locale).presence
end
end

register_modifier(:topic_view_serializer_fancy_title) do |fancy_title, serializer|
if !SiteSetting.experimental_topic_translation ||
serializer.scope.request.params["show"] == "original" ||
serializer.object.topic.detected_locale == I18n.locale.to_s.gsub("_", "-")
fancy_title
else
serializer.object.topic.translation_for(I18n.locale).presence
end
object.topic.translation_for(I18n.locale) || nil
end
end
61 changes: 61 additions & 0 deletions spec/serializers/basic_topic_serializer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

require "rails_helper"

describe BasicTopicSerializer do
fab!(:user) { Fabricate(:user, locale: "ja") }
fab!(:topic)

before do
SiteSetting.translator_enabled = true
SiteSetting.experimental_topic_translation = true
end

describe "#fancy_title" do
let!(:guardian) { Guardian.new(user) }
let!(:original_title) { "FUS ROH DAAHHH" }
let!(:jap_title) { "フス・ロ・ダ・ア" }

before do
topic.title = original_title
SiteSetting.experimental_topic_translation = true
I18n.locale = "ja"
end

def serialize_topic(guardian_user: user, params: {})
env = { "action_dispatch.request.parameters" => params, "REQUEST_METHOD" => "GET" }
request = ActionDispatch::Request.new(env)
guardian = Guardian.new(guardian_user, request)
BasicTopicSerializer.new(topic, scope: guardian)
end

it "does not replace fancy_title with translation when experimental_topic_translation is disabled" do
SiteSetting.experimental_topic_translation = false
topic.set_translation("ja", jap_title)

expect(serialize_topic.fancy_title).to eq(topic.fancy_title)
end

it "does not replace fancy_title with translation when show_original param is present" do
topic.set_translation("ja", jap_title)
expect(serialize_topic(params: { "show" => "original" }).fancy_title).to eq(topic.fancy_title)
end

it "does not replace fancy_title with translation when no translation exists" do
expect(serialize_topic.fancy_title).to eq(topic.fancy_title)
end

it "does not replace fancy_title when topic is already in correct locale" do
I18n.locale = "ja"
topic.set_detected_locale("ja")
topic.set_translation("ja", jap_title)

expect(serialize_topic.fancy_title).to eq(topic.fancy_title)
end

it "returns translated title in fancy_title when translation exists for current locale" do
topic.set_translation("ja", jap_title)
expect(serialize_topic.fancy_title).to eq(jap_title)
end
end
end
23 changes: 17 additions & 6 deletions spec/serializers/post_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,42 @@ def serialize_post(guardian_user: user, params: {})
PostSerializer.new(post, scope: guardian)
end

before { SiteSetting.experimental_topic_translation = true }
before do
SiteSetting.translator_enabled = true
SiteSetting.experimental_topic_translation = true
end

it "does not return translated_cooked when experimental_topic_translation is disabled" do
SiteSetting.experimental_topic_translation = false
expect(serialize_post.translated_cooked).to eq(nil)
expect(serialize_post.cooked).to eq(post.cooked)
end

it "does not return translated_cooked when show=original param is present" do
I18n.locale = "ja"
post.set_translation("ja", "こんにちは")

expect(serialize_post(params: { "show" => "original" }).translated_cooked).to eq(nil)
expect(serialize_post(params: { "show" => "derp" }).translated_cooked).to eq("こんにちは")
expect(serialize_post(params: { "show" => "original" }).cooked).to eq(post.cooked)
expect(serialize_post(params: { "show" => "derp" }).cooked).to eq("こんにちは")
end

it "does not return translated_cooked when post is already in correct locale" do
I18n.locale = "ja"
post.set_detected_locale("ja")
post.set_translation("ja", "こんにちは")

expect(serialize_post.cooked).to eq(post.cooked)
end

it "returns translated content based on locale" do
I18n.locale = "ja"
post.set_translation("ja", "こんにちは")
post.set_translation("es", "Hola")
expect(serialize_post.translated_cooked).to eq("こんにちは")
expect(serialize_post.cooked).to eq("こんにちは")
end

it "does not return translated_cooked when plugin is disabled" do
SiteSetting.translator_enabled = false
expect(serialize_post.translated_cooked).to eq(nil)
expect(serialize_post.cooked).to eq(post.cooked)
end
end
end
26 changes: 17 additions & 9 deletions spec/serializers/topic_view_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
expect(topic_view.posts.first.association(:content_locale)).to be_loaded
end

describe "#translated_title" do
describe "#fancy_title" do
fab!(:user) { Fabricate(:user, locale: "ja") }
fab!(:topic)

Expand All @@ -52,25 +52,33 @@ def serialize_topic(guardian_user: user, params: {})
TopicViewSerializer.new(TopicView.new(topic), scope: guardian)
end

it "does not return translated_title when experimental_topic_translation is disabled" do
it "does not replace fancy_title with translation when experimental_topic_translation is disabled" do
SiteSetting.experimental_topic_translation = false
topic.set_translation("ja", jap_title)

expect(serialize_topic.translated_title).to eq(nil)
expect(serialize_topic.fancy_title).to eq(topic.fancy_title)
end

it "does not return translated_title when show_original param is present" do
it "does not replace fancy_title with translation when show_original param is present" do
topic.set_translation("ja", jap_title)
expect(serialize_topic(params: { "show" => "original" }).translated_title).to eq(nil)
expect(serialize_topic(params: { "show" => "original" }).fancy_title).to eq(topic.fancy_title)
end

it "does not return translated_title when no translation exists" do
expect(serialize_topic.translated_title).to eq(nil)
it "does not replace fancy_title with translation when no translation exists" do
expect(serialize_topic.fancy_title).to eq(topic.fancy_title)
end

it "returns translated title when translation exists for current locale" do
it "does not replace fancy_title when topic is already in correct locale" do
I18n.locale = "ja"
topic.set_detected_locale("ja")
topic.set_translation("ja", jap_title)

expect(serialize_topic.fancy_title).to eq(topic.fancy_title)
end

it "returns translated title in fancy_title when translation exists for current locale" do
topic.set_translation("ja", jap_title)
expect(serialize_topic.translated_title).to eq(jap_title)
expect(serialize_topic.fancy_title).to eq(jap_title)
end
end
end
Loading