From 773e9b94ac22d4d425a7718a65caa52df7dbbf19 Mon Sep 17 00:00:00 2001 From: Kanittha Thaveesittikullarp Date: Wed, 13 Nov 2024 17:43:59 -0500 Subject: [PATCH] adding translate feature code --- public/src/client/topic.js | 15 +++++++++++++++ src/posts/create.js | 6 +++++- src/posts/data.js | 2 ++ src/translate/index.js | 11 +++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/translate/index.js diff --git a/public/src/client/topic.js b/public/src/client/topic.js index 0a291f1f9e..7cdcb1e80a 100644 --- a/public/src/client/topic.js +++ b/public/src/client/topic.js @@ -71,12 +71,27 @@ define('forum/topic', [ handleThumbs() $(window).on('scroll', utils.debounce(updateTopicTitle, 250)) + configurePostToggle(); handleTopicSearch() hooks.fire('action:topic.loaded', ajaxify.data) } + function configurePostToggle() { + $(".topic").on("click", ".view-translated-btn", function () { + // Toggle the visibility of the next .translated-content div + $(this).closest('.sensitive-content-message').next('.translated-content').toggle(); + // Optionally, change the button text based on visibility + var isVisible = $(this).closest('.sensitive-content-message').next('.translated-content').is(':visible'); + if (isVisible) { + $(this).text('Hide the translated message.'); + } else { + $(this).text('Click here to view the translated message.'); + } + }); +} + function handleTopicSearch () { require(['mousetrap'], (mousetrap) => { if (config.topicSearchEnabled) { diff --git a/src/posts/create.js b/src/posts/create.js index 5203ca1726..ab493437c2 100644 --- a/src/posts/create.js +++ b/src/posts/create.js @@ -10,6 +10,7 @@ const topics = require('../topics') const categories = require('../categories') const groups = require('../groups') const privileges = require('../privileges') +const translate = require('../translate'); module.exports = function (Posts) { Posts.create = async function (data) { @@ -19,6 +20,7 @@ module.exports = function (Posts) { const content = data.content.toString() const timestamp = data.timestamp || Date.now() const isMain = data.isMain || false + const [isEnglish, translatedContent] = await translate.translate(data) if (data.isAnonymous) { data.uid = 0 @@ -41,7 +43,9 @@ module.exports = function (Posts) { uid: data.uid, tid, content, - timestamp + timestamp, + translatedContent: translatedContent, + isEnglish: isEnglish, } if (data.toPid) { diff --git a/src/posts/data.js b/src/posts/data.js index 134ea8f6ab..48347872c5 100644 --- a/src/posts/data.js +++ b/src/posts/data.js @@ -67,5 +67,7 @@ function modifyPost (post, fields) { if (post.hasOwnProperty('edited')) { post.editedISO = post.edited !== 0 ? utils.toISOString(post.edited) : '' } + // Mark post as "English" if decided by translator service or if it has no info + post.isEnglish = post.isEnglish == "true" || post.isEnglish === undefined; } } diff --git a/src/translate/index.js b/src/translate/index.js new file mode 100644 index 0000000000..ade0b54419 --- /dev/null +++ b/src/translate/index.js @@ -0,0 +1,11 @@ +var request = require('request'); + +const translatorApi = module.exports; + +translatorApi.translate = async function (postData) { + // Edit the translator URL below + const TRANSLATOR_API = "translator-service-aawaa.azurewebsites.net" + const response = await fetch(TRANSLATOR_API+'/?content='+postData.content); + const data = await response.json(); + return [data["is_english"], data["translated_content"]] +} \ No newline at end of file