如何告诉 Googletrans 忽略某些部分?
How to tell Googletrans to ignore certain parts?
我想使用 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.'
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é.
实际上,您需要以下格式:
(3 '_' before) (string) (2 '_' after)
Like: ___%s__
我想使用 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.'
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é.
实际上,您需要以下格式:
(3 '_' before) (string) (2 '_' after)
Like: ___%s__