Skip to content

Commit

Permalink
Draft PR for translating post contents in English.
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al committed Mar 17, 2024
1 parent d003acc commit 99154e4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
24 changes: 23 additions & 1 deletion public/src/client/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,34 @@ define('forum/topic', [
handleBookmark(tid);

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

handleTopicSearch();

hooks.fire('action:topic.loaded', ajaxify.data);
};

function configurePostToggle() {
$(".topic").on("click", ".view-original-btn", function () {
console.log("clicked");
console.log(this);
// Toggle the visibility of the next .original-content div
$(this).closest('.sensitive-content-message').next('.original-content').toggle();

console.log($(this).closest('.sensitive-content-message').next('.original-content'));
// Optionally, change the button text based on visibility
var isVisible = $(this).closest('.sensitive-content-message').next('.original-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 utils = require('../utils');
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
1 change: 1 addition & 0 deletions src/posts/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ function modifyPost(post, fields) {
if (post.hasOwnProperty('edited')) {
post.editedISO = post.edited !== 0 ? utils.toISOString(post.edited) : '';
}
post.isEnglish = post.isEnglish == "true";
}
}
19 changes: 19 additions & 0 deletions src/translate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { GoogleGenerativeAI } = require("@google/generative-ai");
const translatorApi = module.exports;

translatorApi.translate = async function (postData) {
if (postData.content == "你好") {
return [false, "hello"];
}
if (postData.content == "Bonjour") {
return [false, "hello"];
}
if (postData.content == "Hola") {
return [false, "hello"];
}
if (postData.content == "Olá") {
return [false, "hello"];
}
// ...
return [true, null];
}
10 changes: 9 additions & 1 deletion themes/nodebb-theme-persona/templates/partials/topic/post.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@
<br />

<div class="content" component="post/content" itemprop="text">
{posts.content}
{posts.content}
{{{if !posts.isEnglish }}}
<div class="sensitive-content-message">
<a class="btn btn-sm btn-primary view-original-btn">Click here to view the translated message.</a>
</div>
<div class="original-content" style="display:none;">
{posts.translatedContent}
</div>
{{{end}}}
</div>

<div class="post-footer">
Expand Down

0 comments on commit 99154e4

Please sign in to comment.