Skip to content

Commit

Permalink
got the frontend to work and translate
Browse files Browse the repository at this point in the history
  • Loading branch information
vickyc2266 committed Nov 13, 2024
1 parent bcb8830 commit c916b09
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions public/src/client/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/posts/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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]]');
Expand All @@ -37,6 +39,8 @@ module.exports = function (Posts) {
content: content,
timestamp: timestamp,
endorsed: endorsed,
translatedContent: translatedContent,
isEnglish: isEnglish,
};

if (data.toPid) {
Expand Down
2 changes: 2 additions & 0 deletions src/posts/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
13 changes: 13 additions & 0 deletions src/translate/index.js
Original file line number Diff line number Diff line change
@@ -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];
};

0 comments on commit c916b09

Please sign in to comment.