parent
09eb833a79
commit
d7d4f082f7
@ -1,24 +1,44 @@ |
||||
Meteor.startup(function() { |
||||
Tracker.autorun(function() { |
||||
if (RocketChat.settings.get('AutoTranslate_Enabled') && RocketChat.authz.hasAtLeastOnePermission(['auto-translate'])) { |
||||
RocketChat.callbacks.add('renderMessage', (message) => { |
||||
if (message.u._id !== Meteor.userId()) { |
||||
const subscription = RocketChat.models.Subscriptions.findOne({ rid: message.rid }, { fields: { autoTranslate: 1, autoTranslateLanguage: 1, autoTranslateDisplay: 1 } }); |
||||
if (subscription && subscription.autoTranslate === true && subscription.autoTranslateLanguage && !!subscription.autoTranslateDisplay !== !!message.autoTranslateShowInverse) { |
||||
const autoTranslateLanguage = subscription.autoTranslateLanguage; |
||||
if (!message.translations) { |
||||
message.translations = {}; |
||||
RocketChat.AutoTranslate = { |
||||
messageIdsToWait: {}, |
||||
|
||||
init() { |
||||
Tracker.autorun(() => { |
||||
if (RocketChat.settings.get('AutoTranslate_Enabled') && RocketChat.authz.hasAtLeastOnePermission(['auto-translate'])) { |
||||
RocketChat.callbacks.add('renderMessage', (message) => { |
||||
if (message.u._id !== Meteor.userId()) { |
||||
const subscription = RocketChat.models.Subscriptions.findOne({ rid: message.rid }, { fields: { autoTranslate: 1, autoTranslateLanguage: 1, autoTranslateDisplay: 1 } }); |
||||
if (subscription && subscription.autoTranslate === true && subscription.autoTranslateLanguage && !!subscription.autoTranslateDisplay !== !!message.autoTranslateShowInverse) { |
||||
const autoTranslateLanguage = subscription.autoTranslateLanguage; |
||||
if (!message.translations) { |
||||
message.translations = {}; |
||||
} |
||||
message.translations['original'] = message.html; |
||||
if (message.translations[autoTranslateLanguage]) { |
||||
message.html = message.translations[autoTranslateLanguage]; |
||||
} |
||||
return message; |
||||
} |
||||
message.translations['original'] = message.html; |
||||
if (message.translations[autoTranslateLanguage]) { |
||||
message.html = message.translations[autoTranslateLanguage]; |
||||
} |
||||
return message; |
||||
} |
||||
} |
||||
}, RocketChat.callbacks.priority.HIGH - 3, 'autotranslate'); |
||||
} else { |
||||
RocketChat.callbacks.remove('renderMessage', 'autotranslate'); |
||||
} |
||||
}); |
||||
}, RocketChat.callbacks.priority.HIGH - 3, 'autotranslate'); |
||||
|
||||
RocketChat.callbacks.add('streamMessage', (message) => { |
||||
const subscription = RocketChat.models.Subscriptions.findOne({ rid: message.rid }, { fields: { autoTranslate: 1, autoTranslateLanguage: 1, autoTranslateDisplay: 1 } }); |
||||
if (subscription && subscription.autoTranslate === true && subscription.autoTranslateLanguage && subscription.autoTranslateDisplay && (!message.translations || !message.translations[subscription.autoTranslateLanguage])) { |
||||
RocketChat.models.Messages.update({ _id: message._id }, { $set: { autoTranslateFetching: true } }); |
||||
} |
||||
if (this.messageIdsToWait[message._id] !== undefined) { |
||||
RocketChat.models.Messages.update({ _id: message._id }, { $set: { autoTranslateShowInverse: true }, $unset: { autoTranslateFetching: 1 } }); |
||||
delete this.messageIdsToWait[message._id]; |
||||
} |
||||
}, RocketChat.callbacks.priority.HIGH - 3, 'autotranslate-stream'); |
||||
} else { |
||||
RocketChat.callbacks.remove('renderMessage', 'autotranslate'); |
||||
RocketChat.callbacks.remove('streamMessage', 'autotranslate-stream'); |
||||
} |
||||
}); |
||||
} |
||||
}; |
||||
|
||||
Meteor.startup(function() { |
||||
RocketChat.AutoTranslate.init(); |
||||
}); |
||||
|
||||
@ -0,0 +1,8 @@ |
||||
Meteor.methods({ |
||||
'autoTranslate.translateMessage'(message) { |
||||
const room = RocketChat.models.Rooms.findOneById(message && message.rid); |
||||
if (message && room && RocketChat.AutoTranslate) { |
||||
return RocketChat.AutoTranslate.translateMessage(message, room); |
||||
} |
||||
} |
||||
}); |
||||
@ -1,3 +1,8 @@ |
||||
RocketChat.models.Messages.setTranslations = function(messageId, translations) { |
||||
return this.update({ _id: messageId }, { $set: { translations: translations }}); |
||||
RocketChat.models.Messages.addTranslations = function(messageId, translations) { |
||||
const updateObj = {}; |
||||
Object.keys(translations).forEach((key) => { |
||||
const translation = translations[key]; |
||||
updateObj[`translations.${key}`] = translation; |
||||
}); |
||||
return this.update({ _id: messageId }, { $set: updateObj }); |
||||
}; |
||||
|
||||
Loading…
Reference in new issue