1""" 2Translate text from msgid's 3""" 4 5import re 6import sys 7from google.cloud import translate 8 9 10def clean_text(_text): 11 """clean_text: Clean msgid's text from a pot file 12 13 :_text: list of msgid's text 14 :returns: clean text, without 'msgid "' or '"' 15 """ 16 17 _rawtext = "" 18 for l in _text: 19 _temptext = "" 20 _temptext = re.sub(r'^msgid "', '', l) 21 _temptext = re.sub(r'^"', '', _temptext) 22 _temptext = re.sub(r'"\n', '\n', _temptext) 23 _rawtext += _temptext 24 25 return _rawtext 26 27 28def clean_output_text(_text): 29 """clean_output_text: Clean msgstr's text after translates 30 31 :_text: list of msgid's text 32 :returns: clean text, with 'msgstr "' and double quotes 33 """ 34 35 _temp = "" 36 _templist = _text.splitlines() 37 _templistlen = len(_templist) - 1 38 for i, l in enumerate(_templist): 39 # Clean 40 _temptext = l.rstrip() 41 # https://github.com/Khan/polib/blob/master/polib.py 42 # Escapes the characters ``\\\\``, ``\\t``, ``\\n``, ``\\r`` and ``"`` 43 # in the given string ``st`` and returns it. 44 _temptext = _temptext.replace('\\', r'\\')\ 45 .replace('\t', r'\t')\ 46 .replace('\r', r'\r')\ 47 .replace('\n', r'\n')\ 48 .replace('\"', r'\"') 49 50 # First line, add 'msg str "...' to the text 51 if i == 0 and i == _templistlen: 52 # Single line, do not insert space in the end 53 _temp += 'msgstr "' + _temptext + '"\n' 54 elif i == 0 and i != _templistlen and len(_temptext) > 0: 55 # Multiple line, insert space in the end 56 _temp += 'msgstr "' + _temptext + ' "\n' 57 elif i == 0 and i != _templistlen and len(_temptext) == 0: 58 # Multiple line and line empty, do not insert space in the end 59 _temp += 'msgstr "' + _temptext + '"\n' 60 elif i != 0 and i == _templistlen: 61 # Last line, do not insert space in the end 62 _temp += '"' + _temptext + '"\n' 63 else: 64 # It's not the firs or last line, insert space in the end 65 _temp += '"' + _temptext + ' "\n' 66 67 return _temp 68 69 70def translate_text(_text, _targ_lang, _error=False, _print_process=False): 71 """translate_text: Translate msgid text from a pot file 72 73 :_text: msgid text 74 :_targ_lang: translate text to this language 75 :_error: save translated texts as fuzzy(draft) 76 :_print_process: print translate errors if exist 77 :returns: text translated 78 """ 79 80 try: 81 _client = translate.Client() 82 except Exception as error: 83 print(error) 84 sys.exit(1) 85 86 if _print_process: 87 print('............................................................') 88 print('Tranlating message:') 89 print(clean_text(_text)) 90 try: 91 # Preserve line breaks 92 # The translate api has a parameter format which you can set to text. 93 # This will preserve line breaks. See 94 # https://cloud.google.com/translate/docs/reference/translate. 95 trans = _client.translate( 96 clean_text(_text), 97 target_language=_targ_lang, 98 format_="text") 99 except Exception as error: 100 if _error: 101 print(error) 102 103 _newtext = clean_output_text(trans['translatedText']) 104 105 if _print_process: 106 print('-->') 107 print(trans['translatedText']) 108 109 return _newtext 110