- {{this.translatedTitle}}
+ {{#if this.showTranslation}}
+
+ {{#if this.translatedTitle}}
+
+ {{this.translatedTitle}}
+
+ {{/if}}
+
+ {{i18n
+ "translator.translated_from"
+ language=this.post.detectedLang
+ translator=this.siteSettings.translator
+ }}
+
+
+ {{htmlSafe this.post.translatedText}}
{{/if}}
-
- {{i18n
- "translator.translated_from"
- language=this.post.detectedLang
- translator=this.siteSettings.translator
- }}
-
-
- {{htmlSafe this.post.translatedText}}
-
diff --git a/assets/javascripts/discourse/initializers/extend-for-translate-button.js b/assets/javascripts/discourse/initializers/extend-for-translate-button.js
index 182c9c21..c8208a1f 100644
--- a/assets/javascripts/discourse/initializers/extend-for-translate-button.js
+++ b/assets/javascripts/discourse/initializers/extend-for-translate-button.js
@@ -31,9 +31,7 @@ function initializeTranslation(api) {
api.renderInOutlet("topic-navigation", ShowOriginalContent);
}
- if (!siteSettings.experimental_topic_translation) {
- customizePostMenu(api);
- }
+ customizePostMenu(api);
}
function customizePostMenu(api, container) {
diff --git a/assets/javascripts/discourse/services/translator.js b/assets/javascripts/discourse/services/translator.js
index bcb5ca16..238db2ae 100644
--- a/assets/javascripts/discourse/services/translator.js
+++ b/assets/javascripts/discourse/services/translator.js
@@ -1,7 +1,11 @@
-import Service from "@ember/service";
+import Service, { service } from "@ember/service";
import { ajax } from "discourse/lib/ajax";
export default class TranslatorService extends Service {
+ @service siteSettings;
+ @service appEvents;
+ @service documentTitle;
+
async translatePost(post) {
const response = await ajax("/translator/translate", {
type: "POST",
@@ -11,6 +15,16 @@ export default class TranslatorService extends Service {
post.detectedLang = response.detected_lang;
post.translatedText = response.translation;
post.translatedTitle = response.title_translation;
+ if (this.siteSettings.experimental_topic_translation) {
+ if (post.post_number === 1) {
+ post.topic.set("fancy_title", response.title_translation);
+ this.appEvents.trigger("header:update-topic", post.topic);
+ this.documentTitle.setTitle(response.title_translation);
+ }
+ post.set("cooked", response.translation);
+ post.set("can_translate", false);
+ this.appEvents.trigger("post-stream:refresh", { id: post.id });
+ }
}
clearPostTranslation(post) {
diff --git a/test/javascripts/integration/toggle-translation-button-test.js b/test/javascripts/integration/toggle-translation-button-test.js
new file mode 100644
index 00000000..02073835
--- /dev/null
+++ b/test/javascripts/integration/toggle-translation-button-test.js
@@ -0,0 +1,51 @@
+import { render } from "@ember/test-helpers";
+import { hbs } from "ember-cli-htmlbars";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+
+module("Integration | Component | toggle-translation-button", function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("doesn't render when post cannot be translated", async function (assert) {
+ this.set("post", { can_translate: false });
+
+ await render(hbs`
+