diff --git a/public/src/client/topic.js b/public/src/client/topic.js index 7e65cbeb4f..8667b1d361 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 c035f86013..be9070d44c 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) { @@ -20,6 +21,7 @@ module.exports = function (Posts) { const timestamp = data.timestamp || Date.now(); const isMain = data.isMain || false; const endorsed = data.endorsed || false; + const [isEnglish, translatedContent] = await translate.translate(data); if (!uid && parseInt(uid, 10) !== 0) { throw new Error('[[error:invalid-uid]]'); @@ -37,6 +39,8 @@ module.exports = function (Posts) { content: content, timestamp: timestamp, endorsed: endorsed, + translatedContent: translatedContent, + isEnglish: isEnglish, }; if (data.toPid) { diff --git a/src/posts/data.js b/src/posts/data.js index 99d551fdf8..6e14166d86 100644 --- a/src/posts/data.js +++ b/src/posts/data.js @@ -71,5 +71,7 @@ function modifyPost(post, fields) { if (post.hasOwnProperty('endorsed')) { post.endorsed = post.endorsed ? post.endorsed : false; } + // 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..4c8a414123 --- /dev/null +++ b/src/translate/index.js @@ -0,0 +1,13 @@ +'use strict'; + +// const request = require('request'); + +const translatorApi = module.exports; + +translatorApi.translate = async function (postData) { + // Edit the translator URL below + const TRANSLATOR_API = 'https://nodebb-boba-translator.azurewebsites.net/'; + const response = await fetch(`${TRANSLATOR_API}/?content=${postData.content}`); + const data = await response.json(); + return [data.is_english, data.translated_content]; +};