使用 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 文件的软件包?

解决方法

要从 <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:']
,

您可以使用 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>
,

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...