Skip to content

Commit

Permalink
Merge pull request #54 from CMU-313/translate_feature_P4
Browse files Browse the repository at this point in the history
Adding Translate Service
  • Loading branch information
evelynzhengg authored Nov 13, 2024
2 parents e1026e0 + 773e9b9 commit f3e82d5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
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');

Check warning on line 86 in public/src/client/topic.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Unexpected var, use let or const instead
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
6 changes: 5 additions & 1 deletion 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 @@ -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
Expand All @@ -41,7 +43,9 @@ module.exports = function (Posts) {
uid: data.uid,
tid,
content,
timestamp
timestamp,
translatedContent: translatedContent,

Check warning on line 47 in src/posts/create.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Expected property shorthand
isEnglish: isEnglish,

Check warning on line 48 in src/posts/create.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Expected property shorthand
}

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 @@ -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;
}
}
11 changes: 11 additions & 0 deletions src/translate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var request = require('request');

Check warning on line 1 in src/translate/index.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Unexpected var, use let or const instead

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"]]
}

0 comments on commit f3e82d5

Please sign in to comment.