File size: 3,235 Bytes
9fa4f9e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# MTUOC_GoogleTranslate
# Copyright (C) 2023 Antoni Oliver
# v. 07/06/2023
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from MTUOC_misc import printLOG
import config
###GoogleTranslate imports
from google.cloud import translate as translateGoogle
from google.cloud import translate
def Google_translate_text_with_glossary(text):
glossary = config.client.glossary_path(
config.Google_project_id, config.Google_location, config.Google_glossary
)
glossary_config = translateGoogle.TranslateTextGlossaryConfig(glossary=config.Google_glossary)
# Supported language codes: https://cloud.google.com/translate/docs/languages
response = client.translate_text(
request={
"contents": [text],
"target_language_code": config.Google_sllang,
"source_language_code": config.Google_tllang,
"parent": config.parent,
"glossary_config": glossary_config,
}
)
translation=response.glossary_translations[0]
return(translation.translated_text)
def Google_translate_text(text):
try:
"""Translating Text. segment,config.Google_sllang,config.Google_tllang"""
# Detail on supported types can be found here:
# https://cloud.google.com/translate/docs/supported-formats
response = config.client.translate_text(
parent=config.parent,
contents=[text],
mime_type="text/plain", # mime types: text/plain, text/html
source_language_code=config.Google_sllang,
target_language_code=config.Google_tllang,
)
# Display the translation for each input text provided
translation=response.translations[0]
return(translation.translated_text)
except:
print("ERROR:",sys.exc_info())
def Google_translate(segment):
segment=segment.rstrip()
if config.Google_glossary==None:
cadena="Translating without glossary from "+config.Google_sllang+" to "+config.Google_tllang
printLOG(3,cadena)
cadena="Source segment: "+segment
printLOG(3,cadena)
translation=Google_translate_text(segment)
cadena="Translation: "+translation
printLOG(3,cadena)
else:
cadena="Translating with glossary "+Google_glossary+" from "+config.Google_sllang+" to "+config.Google_tllang
printLOG(3,cadena)
cadena="Source segment: "+segment
printLOG(3,cadena)
translation=Google_translate_text_with_glossary()
cadena="Translation: "+translation
printLOG(3,cadena)
return(translation) |