Skip to content

Commit

Permalink
Integrate Translation Feature (#55)
Browse files Browse the repository at this point in the history
* Translation backend code with API

* Moved translate folder

* fix lint

* updated test.yaml

added back earlier unintentional line deletion

* ignore isEnglish and translatedContent for schema tests

* fixed lint

* Deleted dump.rdb

* Update dump.rdb

* updated translator api url

---------

Co-authored-by: Sarah W. <[email protected]>
Co-authored-by: a1inachen <[email protected]>
Co-authored-by: Alina Chen <[email protected]>
  • Loading branch information
4 people authored Nov 11, 2024
1 parent be18222 commit bea2bed
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ jobs:
steps:
- uses: actions/checkout@v4



- name: Install sendmail
run: sudo apt-get install -y sendmail
- run: cp install/package.json package.json
Expand Down
Binary file modified dump.rdb
Binary file not shown.
16 changes: 15 additions & 1 deletion public/src/client/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,29 @@ define('forum/topic', [
handleThumbs();

$(window).on('scroll', utils.debounce(updateTopicTitle, 250));
configurePostToggle();

handleTopicSearch();
console.log(ajaxify.data);

handleSolvedButton();
handleEndorseButton();
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 handleEndorseButton() {
const posts = document.querySelectorAll('[component="post"]');
console.log('post', posts);
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 @@ -36,6 +38,8 @@ module.exports = function (Posts) {
content: content,
timestamp: timestamp,
endorsed: 0,
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 @@ -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 @@
'use strict';

const translatorApi = module.exports;

translatorApi.translate = async function (postData) {
// Edit the translator URL below
const TRANSLATOR_API = 'http://team-bulbasaur-translator-service.azurewebsites.net/';
const response = await fetch(`${TRANSLATOR_API}/?content=${postData.content}`);
const data = await response.json();
return [data.is_english, data.translated_content];
};
3 changes: 3 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,9 @@ describe('API', async () => {
if (additionalProperties) { // All bets are off
return;
}
if (prop === 'isEnglish' || prop === 'translatedContent') {
return; // Skip the check for these properties
}

assert(schema[prop], `"${prop}" was found in response, but is not defined in schema (path: ${method} ${path}, context: ${context})`);
});
Expand Down

0 comments on commit bea2bed

Please sign in to comment.