Skip to content

Commit 29bbbc8

Browse files
committed
Also translate topic titles
1 parent 9487a81 commit 29bbbc8

File tree

5 files changed

+125
-62
lines changed

5 files changed

+125
-62
lines changed
Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
1-
import Component from "@glimmer/component";
2-
import { service } from "@ember/service";
3-
import { action } from "@ember/object";
4-
import DButton from "discourse/components/d-button";
5-
import concatClass from "discourse/helpers/concat-class";
6-
import { tracked } from "@glimmer/tracking";
7-
8-
export default class ShowOriginalContent extends Component {
9-
@service router;
10-
@tracked active = true;
11-
12-
constructor() {
13-
super(...arguments);
14-
this.active = !new URLSearchParams(window.location.search).has("show_original");
15-
}
16-
17-
@action
18-
async showOriginal() {
19-
this.active = !this.active;
20-
window.location.search = this.active ? "" : `show_original=1`;
21-
}
22-
23-
get title() {
24-
return this.active
25-
? "translator.hide_translation"
26-
: "translator.show_translation";
27-
}
28-
29-
<template>
30-
<div class="discourse-translator_toggle-original">
31-
<DButton
32-
@icon="language"
33-
@title={{this.title}}
34-
class={{concatClass "btn btn-default" (if this.active "active")}}
35-
@action={{this.showOriginal}}
36-
>
37-
</DButton>
38-
</div>
39-
</template>
40-
}
1+
import Component from "@glimmer/component";
2+
import { service } from "@ember/service";
3+
import { action } from "@ember/object";
4+
import DButton from "discourse/components/d-button";
5+
import concatClass from "discourse/helpers/concat-class";
6+
import { tracked } from "@glimmer/tracking";
7+
8+
export default class ShowOriginalContent extends Component {
9+
@service router;
10+
@tracked active = true;
11+
12+
constructor() {
13+
super(...arguments);
14+
this.active = !new URLSearchParams(window.location.search).has("show");
15+
}
16+
17+
@action
18+
async showOriginal() {
19+
this.active = !this.active;
20+
window.location.search = this.active ? "" : `show=original`;
21+
}
22+
23+
get title() {
24+
return this.active
25+
? "translator.hide_translation"
26+
: "translator.show_translation";
27+
}
28+
29+
<template>
30+
<div class="discourse-translator_toggle-original">
31+
<DButton
32+
@icon="language"
33+
@title={{this.title}}
34+
class={{concatClass "btn btn-default" (if this.active "active")}}
35+
@action={{this.showOriginal}}
36+
/>
37+
</div>
38+
</template>
39+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module DiscourseTranslator
4+
module TranslatorHelper
5+
def self.translated_value(original_value, model, scope)
6+
return original_value if !SiteSetting.experimental_topic_translation
7+
return original_value if scope.request.params["show"] == "original"
8+
9+
translated = model.custom_fields[TRANSLATED_CUSTOM_FIELD]
10+
return original_value if (translated.blank? || translated[I18n.locale].blank?)
11+
12+
translated[I18n.locale]
13+
end
14+
end
15+
end

plugin.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ module ::DiscourseTranslator
4848
scope.can_translate?(object)
4949
end
5050

51-
add_to_serializer :post, :cooked, false do
52-
return super() if !SiteSetting.experimental_topic_translation
53-
return super() if scope.request.params["show_original"].present?
54-
translated = object.custom_fields[::DiscourseTranslator::TRANSLATED_CUSTOM_FIELD]
55-
return super() if (translated.blank? || translated[I18n.locale].blank?)
56-
translated[I18n.locale]
51+
add_to_serializer :post, :cooked, respect_plugin_enabled: false do
52+
return super() if cooked_hidden
53+
DiscourseTranslator::TranslatorHelper.translated_value(super(), object, scope)
54+
end
55+
56+
add_to_serializer :basic_topic, :fancy_title do
57+
DiscourseTranslator::TranslatorHelper.translated_value(object.fancy_title, object, scope)
5758
end
5859
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
require "rails_helper"
4+
5+
describe BasicTopicSerializer do
6+
let!(:guardian) { Guardian.new(user) }
7+
let!(:original_title) { "FUS ROH DAAHHH" }
8+
let!(:jap_title) { "フス・ロ・ダ・ア" }
9+
10+
describe "#fancy_title" do
11+
fab!(:user) { Fabricate(:user, locale: "ja") }
12+
fab!(:topic) { Fabricate(:topic) }
13+
14+
before do
15+
topic.title = original_title
16+
SiteSetting.experimental_topic_translation = true
17+
I18n.locale = "ja"
18+
end
19+
20+
def serialize_topic(guardian_user: user, params: {})
21+
env = { "action_dispatch.request.parameters" => params, "REQUEST_METHOD" => "GET" }
22+
request = ActionDispatch::Request.new(env)
23+
guardian = Guardian.new(guardian_user, request)
24+
BasicTopicSerializer.new(topic, scope: guardian)
25+
end
26+
27+
it "returns original fancy_title when experimental_topic_translation is disabled" do
28+
SiteSetting.experimental_topic_translation = false
29+
topic.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD] = { "ja" => jap_title }
30+
31+
expect(serialize_topic.fancy_title).to eq(original_title)
32+
end
33+
34+
it "returns original fancy_title when show_original param is present" do
35+
topic.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD] = { "ja" => jap_title }
36+
expect(serialize_topic(params: { "show" => "original" }).fancy_title).to eq(original_title)
37+
end
38+
39+
it "returns original fancy_title when no translation exists" do
40+
expect(serialize_topic.fancy_title).to eq(original_title)
41+
end
42+
43+
it "returns original fancy_title when translation is blank for current locale" do
44+
topic.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD] = { "ja" => "" }
45+
expect(serialize_topic.fancy_title).to eq(original_title)
46+
end
47+
48+
it "returns translated title when translation exists for current locale" do
49+
topic.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD] = { "ja" => jap_title }
50+
expect(serialize_topic.fancy_title).to eq(jap_title)
51+
end
52+
end
53+
end

spec/serializers/post_serializer_spec.rb

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,10 @@
8282
end
8383

8484
describe "#cooked" do
85-
def serialize_post(user: nil, params: {}, cookies: {})
86-
env = {
87-
"action_dispatch.request.parameters" => params,
88-
"REQUEST_METHOD" => "GET",
89-
"rack.input" => {
90-
},
91-
"HTTP_COOKIE" => cookies.map { |k, v| "#{k}=#{v}" }.join(";"),
92-
}
85+
def serialize_post(guardian_user: user, params: {})
86+
env = { "action_dispatch.request.parameters" => params, "REQUEST_METHOD" => "GET" }
9387
request = ActionDispatch::Request.new(env)
94-
guardian = Guardian.new(user, request)
88+
guardian = Guardian.new(guardian_user, request)
9589
PostSerializer.new(post, scope: guardian)
9690
end
9791

@@ -100,14 +94,15 @@ def serialize_post(user: nil, params: {}, cookies: {})
10094
it "returns original cooked when experimental_topic_translation is disabled" do
10195
SiteSetting.experimental_topic_translation = false
10296
original_cooked = post.cooked
103-
expect(serialize_post(user: user).cooked).to eq(original_cooked)
97+
expect(serialize_post.cooked).to eq(original_cooked)
10498
end
10599

106-
it "returns original cooked when show_original param is present" do
100+
it "returns original cooked when show=original param is present" do
107101
original_cooked = post.cooked
108-
expect(serialize_post(user: user, params: { "show_original" => "true" }).cooked).to eq(
109-
original_cooked,
110-
)
102+
I18n.locale = "ja"
103+
post.custom_fields[DiscourseTranslator::TRANSLATED_CUSTOM_FIELD] = { "ja" => "こんにちは" }
104+
expect(serialize_post(params: { "show" => "original" }).cooked).to eq(original_cooked)
105+
expect(serialize_post(params: { "show" => "derp" }).cooked).to eq("こんにちは")
111106
end
112107

113108
it "returns translated content based on locale" do
@@ -116,13 +111,13 @@ def serialize_post(user: nil, params: {}, cookies: {})
116111
"ja" => "こんにちは",
117112
"es" => "Hola",
118113
}
119-
expect(serialize_post(user: user).cooked).to eq("こんにちは")
114+
expect(serialize_post.cooked).to eq("こんにちは")
120115
end
121116

122117
it "returns original cooked when plugin is disabled" do
123118
SiteSetting.translator_enabled = false
124119
original_cooked = post.cooked
125-
expect(serialize_post(user: user).cooked).to eq(original_cooked)
120+
expect(serialize_post.cooked).to eq(original_cooked)
126121
end
127122
end
128123
end

0 commit comments

Comments
 (0)