Files
odoo_source/addons/mail/controllers/google_translate.py
T
Killian Frappart a3bab45dbe [IMP] mail: translate message from chatter
This commit introduces a new optional feature in the chatter.

Users can request translation from the Google Cloud Translation API.
Documentation: <https://cloud.google.com/translate/docs#docs>

Adding the key in settings will instantly enable the feature for all
users of the database and consumption is not measured nor limited
by Odoo as it is natively supported using the Google Cloud Console.

Translation values are stored in the database to reduce cost as
much as possible. Therefore, translation values are automatically
garbage collected every 2 weeks to prevent database pollution.

Task-3338165

closes odoo/odoo#126555

Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
2023-10-25 11:37:17 +00:00

56 lines
2.8 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
import babel
import requests
from odoo.http import request, route, Controller
class GoogleTranslateController(Controller):
@route("/mail/message/translate", type="json", auth="user")
def translate(self, message_id):
message = request.env["mail.message"].search([("id", "=", message_id)])
if not message:
raise request.not_found()
domain = [("message_id", "=", message.id), ("target_lang", "=", request.env.user.lang.split("_")[0])]
# sudo: mail.message.translation - searching translations of a message that can be read with standard ACL
translation = request.env["mail.message.translation"].sudo().search(domain)
if not translation:
try:
source_lang = self._detect_source_lang(message)
target_lang = request.env.user.lang.split("_")[0]
# sudo: mail.message.translation - create translation of a message that can be read with standard ACL
vals = {
"body": self._get_translation(str(message.body), source_lang, target_lang),
"message_id": message.id,
"source_lang": source_lang,
"target_lang": target_lang,
}
translation = request.env["mail.message.translation"].sudo().create(vals)
except requests.exceptions.HTTPError as err:
return {"error": err.response.json()["error"]["message"]}
return {
"body": translation.body,
"lang_name": babel.Locale(translation.source_lang).get_display_name(request.env.user.lang),
}
def _detect_source_lang(self, message):
# sudo: mail.message.translation - searching translations of a message that can be read with standard ACL
translation = request.env["mail.message.translation"].sudo().search([("message_id", "=", message.id)], limit=1)
if translation:
return translation.source_lang
response = self._post(endpoint="detect", data={"q": str(message.body)})
return response.json()["data"]["detections"][0][0]["language"]
def _get_translation(self, body, source_lang, target_lang):
response = self._post(data={"q": body, "target": target_lang, "source": source_lang})
return response.json()["data"]["translations"][0]["translatedText"]
def _post(self, endpoint="", data=None):
# sudo: ir.config_parameter - reading google translate api key, using it to make the request
api_key = request.env["ir.config_parameter"].sudo().get_param("mail.google_translate_api_key")
url = f"https://translation.googleapis.com/language/translate/v2/{endpoint}?key={api_key}"
response = requests.post(url, data=data, timeout=3)
response.raise_for_status()
return response