如何告诉Googletrans忽略某些部分?

时间:2019-02-03 14:38:25

标签: python google-translate google-translation-api

我想使用googletrans来使用Google翻译API。但是,其中包含变量名的字符串:

User "%(first_name)s %(last_name)s (%(email)s)" has been deleted.

如果我通过googletrans使用它

from googletrans import Translator
translator = Translator()
translator.translate(u'User "%(first_name)s %(last_name)s (%(email)s)" has been assigned.', src='en', dest='fr').text

我得到以下信息:

L'utilisateur "% (first_name) s% (last_name) s (% (email) s)" a été affecté.

但是,“%(first_name)s%(last_name)s(%(email)s)”引入了一些字符串。有没有解决的办法?我已经尝试过:

u'User "<span class="notranslate">%(first_name)s %(last_name)s (%(email)s)</span>" has been assigned.'

2 个答案:

答案 0 :(得分:1)

似乎Googletrans保持不变,例如__1__。因此,您可以在转换之前将%(first_name)s替换为__0__,将%(last_name)s替换为__1__,依此类推,然后再恢复变量。这是执行此操作的代码:

from googletrans import Translator
import re

translator = Translator()
txtorig = u'User "%(first_name)s %(last_name)s (%(email)s)" has been assigned.'

# temporarily replace variables of format "%(example_name)s" with "__n__" to
#  protect them during translate()
VAR, REPL = re.compile(r'%\(\w+\)s'), re.compile(r'__(\d+)__')
varlist = []
def replace(matchobj):
  varlist.append(matchobj.group())
  return "__%d__" %(len(varlist)-1)
def restore(matchobj):
  return varlist[int(matchobj.group(1))]

txtorig = VAR.sub(replace, txtorig)
txttrans = translator.translate(txtorig, src='en', dest='fr').text
txttrans = REPL.sub(restore, txttrans)

print(txttrans)

结果在这里:

L'utilisateur "%(first_name)s %(last_name)s (%(email)s)" a été attribué.

答案 1 :(得分:0)

实际上,您需要以下格式:

(3 '_' before) (string) (2 '_' after)
Like: ___%s__