使用BeautifulSoup翻译XLIFF文件



我正在使用BeautifulSoup和googletrans包翻译Xliff文件。我设法提取所有字符串并翻译它们,并设法通过创建带有翻译的新标签来替换字符串,例如

<trans-unit id="100890::53706_004">
<source>Continue in store</source>
<target>Kontynuuj w sklepie</target>
</trans-unit>

当源标签中有其他标签时,会出现这个问题。

<source><x ctype="x-htmltag" equiv-text="&lt;b&gt;" id="html_tag_191"/>Choose your product
<x ctype="x-htmltag" equiv-text="&lt;/b&gt;" id="html_tag_192"/>From a list: </source>

这些标签的数量不同,字符串出现的顺序也不同。例:<source> text1 <x /> <x/> text2 <x/> text3 </source>;每个x标签都是唯一的,具有不同的id和属性。

是否有一种方法来修改标签内的文本,而不必创建一个新的标签?我想我可以提取x标签和它的属性,但顺序或字符串和x标签在不同的代码行不同很多,我不知道如何做到这一点。也许有其他更适合翻译xliff文件的包?

您可以使用for-loop与source中的所有子元素一起工作。
并且您可以将它们与copy.copy(child)append复制到target
同时您可以检查child是否为NavigableString并将其转换。


text = '''<source><x ctype="x-htmltag" equiv-text="&lt;b&gt;" id="html_tag_191"/>Choose your product
<x ctype="x-htmltag" equiv-text="&lt;/b&gt;" id="html_tag_192"/>From a list: </source>'''
conversions = {
'Choose your product': 'Wybierz swój produkt',
'From a list: ': 'Z listy: ',
}
from bs4 import BeautifulSoup as BS
from bs4.element import NavigableString
import copy
#soup = BS(text, 'html.parser')  # it has problem to parse it
#soup = BS(text, 'html5lib')     # it has problem to parse it
soup = BS(text, 'lxml')
# create `<target>`
target = soup.new_tag('target')
# add `<target>` after `<source>
source = soup.find('source')
source.insert_after('', target)
# work with children in `<source>`
for child in source:
print('type:', type(child))
# duplicate child and add to `<target>`
child = copy.copy(child)
target.append(child)
# convert text and replace in child in `<target>`        
if isinstance(child, NavigableString):
new_text = conversions[child.string]
child.string.replace_with(new_text)
print('--- target ---')
print(target)
print('--- source ---')
print(source)
print('--- soup ---')
print(soup)

结果(稍微调整以使其更具可读性):

type: <class 'bs4.element.Tag'>
type: <class 'bs4.element.NavigableString'>
type: <class 'bs4.element.Tag'>
type: <class 'bs4.element.NavigableString'>
--- target ---
<target>
<x ctype="x-htmltag" equiv-text="&lt;b&gt;" id="html_tag_191"></x>
Wybierz swój produkt
<x ctype="x-htmltag" equiv-text="&lt;/b&gt;" id="html_tag_192"></x>
Z listy: 
</target>
--- source ---
<source>
<x ctype="x-htmltag" equiv-text="&lt;b&gt;" id="html_tag_191"></x>
Choose your product
<x ctype="x-htmltag" equiv-text="&lt;/b&gt;" id="html_tag_192"></x>
From a list: 
</source>
--- soup ---
<html><body>
<source>
<x ctype="x-htmltag" equiv-text="&lt;b&gt;" id="html_tag_191"></x>
Choose your product
<x ctype="x-htmltag" equiv-text="&lt;/b&gt;" id="html_tag_192"></x>
From a list: 
</source>
<target>
<x ctype="x-htmltag" equiv-text="&lt;b&gt;" id="html_tag_191"></x>
Wybierz swój produkt
<x ctype="x-htmltag" equiv-text="&lt;/b&gt;" id="html_tag_192"></x>
Z listy: 
</target>
</body></html>

要从<source>中提取两个文本条目,您可以使用以下方法:

from bs4 import BeautifulSoup
import requests
html = """<source><x ctype="x-htmltag" equiv-text="&lt;b&gt;" id="html_tag_191"/>Choose your product
<x ctype="x-htmltag" equiv-text="&lt;/b&gt;" id="html_tag_192"/>From a list: </source>"""
soup = BeautifulSoup(html, 'lxml')
print(list(soup.source.stripped_strings))

给你:

['Choose your product', 'From a list:']

我建议不要使用通用XML解析器解析XLIFF文件。相反,应该尝试找到专门的XLIFF工具包。周围有一些python项目,但我没有使用它们的经验(我:主要是Java人)。

最新更新