Skip to content

Commit

Permalink
Merge pull request #45 from CMU-313/translator-stuff
Browse files Browse the repository at this point in the history
Translator code backend code
  • Loading branch information
heyanuja authored Nov 14, 2024
2 parents ba0d26f + a7a321e commit df391d1
Show file tree
Hide file tree
Showing 4 changed files with 31 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 () {

Check failure on line 82 in public/src/client/topic.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Expected indentation of 2 tabs but found 8 spaces

Check failure on line 82 in public/src/client/topic.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Strings must use singlequote

Check failure on line 82 in public/src/client/topic.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Strings must use singlequote

Check failure on line 82 in public/src/client/topic.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Strings must use singlequote
// Toggle the visibility of the next .translated-content div

Check failure on line 83 in public/src/client/topic.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Expected indentation of 3 tabs but found 12 spaces
$(this).closest('.sensitive-content-message').next('.translated-content').toggle();

Check failure on line 84 in public/src/client/topic.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Expected indentation of 3 tabs but found 12 spaces
// Optionally, change the button text based on visibility

Check failure on line 85 in public/src/client/topic.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Expected indentation of 3 tabs but found 12 spaces
var isVisible = $(this).closest('.sensitive-content-message').next('.translated-content').is(':visible');

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

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Expected indentation of 3 tabs but found 12 spaces
if (isVisible) {

Check failure on line 87 in public/src/client/topic.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Expected indentation of 3 tabs but found 12 spaces
$(this).text('Hide the translated message.');

Check failure on line 88 in public/src/client/topic.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Expected indentation of 4 tabs but found 16 spaces
} 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 @@ -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]]');
Expand All @@ -35,6 +37,8 @@ module.exports = function (Posts) {
tid: tid,
content: content,
timestamp: timestamp,
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 @@ -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;
}
}
10 changes: 10 additions & 0 deletions src/translate/index.js
Original file line number Diff line number Diff line change
@@ -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"]];
}

0 comments on commit df391d1

Please sign in to comment.