From de414b5a276ae5a1f9174f896fcfb77e6d0362f0 Mon Sep 17 00:00:00 2001 From: Anuja Date: Wed, 13 Nov 2024 20:33:41 -0500 Subject: [PATCH 1/4] creating translate and index.js code --- src/translate/index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/translate/index.js diff --git a/src/translate/index.js b/src/translate/index.js new file mode 100644 index 0000000000..5f7392d6b7 --- /dev/null +++ b/src/translate/index.js @@ -0,0 +1,10 @@ +var request = require('request'); + +const translatorApi = module.exports; + +translatorApi.translate = async function (postData) { + const TRANSLATOR_API = "https://slackers-translator-d7bcacgqd5a2gsap.canadacentral-01.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 From bfb88806314afb943b27b2632f09aefa7713d956 Mon Sep 17 00:00:00 2001 From: Anuja Date: Wed, 13 Nov 2024 20:34:44 -0500 Subject: [PATCH 2/4] data.js code addition for translate --- src/posts/data.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/posts/data.js b/src/posts/data.js index 1889c5d2d5..577b94319c 100644 --- a/src/posts/data.js +++ b/src/posts/data.js @@ -68,5 +68,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; } } From e803f5d2725c106ffe3a5df055aaec135a896e3b Mon Sep 17 00:00:00 2001 From: Anuja Date: Wed, 13 Nov 2024 20:36:20 -0500 Subject: [PATCH 3/4] create.js code addition for translate --- src/posts/create.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/posts/create.js b/src/posts/create.js index d541564c2e..171ac414b5 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 (!uid && parseInt(uid, 10) !== 0) { throw new Error('[[error:invalid-uid]]'); @@ -35,6 +37,8 @@ module.exports = function (Posts) { tid: tid, content: content, timestamp: timestamp, + translatedContent: translatedContent, + isEnglish: isEnglish, }; if (data.toPid) { From a7a321e231315c47ab1350c24450b766c853611e Mon Sep 17 00:00:00 2001 From: Anuja Date: Wed, 13 Nov 2024 20:39:41 -0500 Subject: [PATCH 4/4] topic.js code addition translate function --- public/src/client/topic.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/public/src/client/topic.js b/public/src/client/topic.js index 7e65cbeb4f..5b61bb19f8 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) {