Skip to content
Snippets Groups Projects
Unverified Commit 7c73c018 authored by Marcelo Schmidt's avatar Marcelo Schmidt
Browse files

Auto translation initial work

parent 0b1d83a4
No related branches found
No related tags found
No related merge requests found
......@@ -43,6 +43,7 @@ rocketchat:api
rocketchat:assets
rocketchat:authorization
rocketchat:autolinker
rocketchat:autotranslate
rocketchat:bot-helpers
rocketchat:cas
rocketchat:channel-settings
......
......@@ -124,6 +124,7 @@ rocketchat:api@0.0.1
rocketchat:assets@0.0.1
rocketchat:authorization@0.0.1
rocketchat:autolinker@0.0.1
rocketchat:autotranslate@0.0.1
rocketchat:bot-helpers@0.0.1
rocketchat:cas@1.0.0
rocketchat:channel-settings@0.0.1
......
# Rocket.Chat Auto Translate
Rocket.Chat supports auto translate through Google Cloud Translation API
Through this you will be able to read messages in your native languages
regardless of language the message was written on.
Package.describe({
name: 'rocketchat:autotranslate',
version: '0.0.1',
summary: 'Rocket.Chat automatic translations',
git: ''
});
Package.onUse(function(api) {
api.use([ 'ecmascript', 'rocketchat:lib' ]);
api.use([ 'templating', 'kadira:flow-router'], 'client');
// api.addFiles([ 'client/loadScript.js', 'client/trackEvents.js' ], 'client');
api.addFiles([ 'server/settings.js', 'server/autotranslate.js', 'server/models/Messages.js' ], 'server');
});
function tokenizeEmojis(message) {
if (!message.tokens || !Array.isArray(message.tokens)) {
message.tokens = [];
}
let count = message.tokens.length;
message.msg = message.msg.replace(/:\w+:/g, function(match) {
const token = `<i class=notranslate>{${count++}}</i>`;
message.tokens.push({
token: token,
text: match
});
return token;
});
return message;
}
function tokenizeInlineCode(message) {
if (!message.tokens || !Array.isArray(message.tokens)) {
message.tokens = [];
}
let count = message.tokens.length;
message.html = message.msg;
RocketChat.MarkdownCode.handle_inlinecode(message);
message.msg = message.html;
for (const tokenIndex in message.tokens) {
if (message.tokens.hasOwnProperty(tokenIndex)) {
const token = message.tokens[tokenIndex].token;
if (token.indexOf('notranslate') === -1) {
const newToken = `<i class=notranslate>{${count++}}</i>`;
message.msg = message.msg.replace(token, newToken);
message.tokens[tokenIndex].token = newToken;
}
}
}
return message;
}
function tokenizeCodeBlock(message) {
if (!message.tokens || !Array.isArray(message.tokens)) {
message.tokens = [];
}
let count = message.tokens.length;
message.html = message.msg;
RocketChat.MarkdownCode.handle_codeblocks(message);
message.msg = message.html;
for (const tokenIndex in message.tokens) {
if (message.tokens.hasOwnProperty(tokenIndex)) {
const token = message.tokens[tokenIndex].token;
if (token.indexOf('notranslate') === -1) {
const newToken = `<i class=notranslate>{${count++}}</i>`;
message.msg = message.msg.replace(token, newToken);
message.tokens[tokenIndex].token = newToken;
}
}
}
return message;
}
function tokenize(message) {
message = tokenizeEmojis(message);
message = tokenizeInlineCode(message);
message = tokenizeCodeBlock(message);
return message;
}
function deTokenize(message) {
if (message.tokens && message.tokens.length > 0) {
for (const {token, text, noHtml} of message.tokens) {
message.msg = message.msg.replace(token, () => noHtml ? noHtml : text);
}
}
return message.msg;
}
RocketChat.callbacks.add('afterSaveMessage', function(message /*, room */) {
if (RocketChat.settings.get('AutoTranslate_GoogleAPIKey') && message.msg) {
Meteor.defer(function() {
const translations = {};
const targetLanguages = ['pt', 'es'];
message.html = s.escapeHTML(String(message.msg));
message = tokenize(message);
let msgs = message.msg.split('\n');
msgs = msgs.map(msg => encodeURIComponent(msg));
const query = `q=${msgs.join('&q=')}`;
targetLanguages.forEach(language => {
const result = HTTP.get('https://translation.googleapis.com/language/translate/v2', { params: { key: RocketChat.settings.get('AutoTranslate_GoogleAPIKey'), target: language }, query: query });
if (result.statusCode === 200 && result.data && result.data.data && result.data.data.translations && Array.isArray(result.data.data.translations) && result.data.data.translations.length > 0) {
const txt = result.data.data.translations.map(translation => translation.translatedText).join('\n');
translations[language] = deTokenize(Object.assign({}, message, { msg: txt }));
}
});
if (!_.isEmpty(translations)) {
RocketChat.models.Messages.setTranslations(message._id, translations);
}
});
}
return message;
}, RocketChat.callbacks.priority.MEDIUM, 'AutoTranslate');
RocketChat.models.Messages.setTranslations = function(messageId, translations) {
return this.update({ _id: messageId }, { $set: { translations: translations }});
};
Meteor.startup(function() {
RocketChat.settings.add('AutoTranslate_GoogleAPIKey', '', { type: 'string', group: 'Message', section: 'AutoTranslate' });
});
......@@ -213,6 +213,7 @@
"AutoLinker_Urls_TLD": "AutoLinker TLD URLs",
"AutoLinker_Urls_www": "AutoLinker 'www' URLs",
"AutoLinker_UrlsRegExp": "AutoLinker URL Regular Expression",
"AutoTranslate_GoogleAPIKey": "Google API Key",
"Available": "Available",
"Available_agents": "Available agents",
"Avatar": "Avatar",
......
......@@ -24,6 +24,7 @@ class MarkdownCode
message.tokens.push
token: token
text: "#{p1}<span class=\"copyonly\">`</span><span><code class=\"code-colors inline\">#{p2}</code></span><span class=\"copyonly\">`</span>#{p3}"
noHtml: match
return token
......@@ -71,6 +72,7 @@ class MarkdownCode
highlight: true
token: token
text: "<pre><code class='code-colors hljs " + result.language + "'><span class='copyonly'>```<br></span>" + result.value + "<span class='copyonly'><br>```</span></code></pre>"
noHtml: "```\n#{s.stripTags(result.value)}\n```"
msgParts[index] = token
else
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment