本文整理汇总了Python中translate.storage.xliff.xlifffile函数的典型用法代码示例。如果您正苦于以下问题:Python xlifffile函数的具体用法?Python xlifffile怎么用?Python xlifffile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了xlifffile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_notes
def test_notes(self):
xlifffile = xliff.xlifffile()
unit = xlifffile.addsourceunit("Concept")
# We don't want to add unnecessary notes
assert not "note" in str(xlifffile)
unit.addnote(None)
assert not "note" in str(xlifffile)
unit.addnote("")
assert not "note" in str(xlifffile)
unit.addnote("Please buy bread")
assert unit.getnotes() == "Please buy bread"
notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
assert len(notenodes) == 1
unit.addnote("Please buy milk", origin="Mom")
notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
assert len(notenodes) == 2
assert not "from" in notenodes[0].attrib
assert notenodes[1].get("from") == "Mom"
assert unit.getnotes(origin="Mom") == "Please buy milk"
unit.addnote("Don't forget the beer", origin="Dad")
notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
assert len(notenodes) == 3
assert notenodes[1].get("from") == "Mom"
assert notenodes[2].get("from") == "Dad"
assert unit.getnotes(origin="Dad") == "Don't forget the beer"
assert not unit.getnotes(origin="Bob") == "Please buy bread\nPlease buy milk\nDon't forget the beer"
assert not notenodes[2].get("from") == "Mom"
assert not "from" in notenodes[0].attrib
assert unit.getnotes() == "Please buy bread\nPlease buy milk\nDon't forget the beer"
assert unit.correctorigin(notenodes[2], "ad") == True
assert unit.correctorigin(notenodes[2], "om") == False
示例2: test_rich_target
def test_rich_target(self):
xlifffile = xliff.xlifffile()
xliffunit = xlifffile.addsourceunit(u'')
# Test 1
xliffunit.set_rich_target([StringElem([u'foo', X(id='bar'), u'baz'])], u'fr')
target_dom_node = xliffunit.getlanguageNode(None, 1)
x_placeable = target_dom_node[0]
assert target_dom_node.text == 'foo'
assert x_placeable.tag == u'x'
assert x_placeable.attrib['id'] == 'bar'
assert x_placeable.tail == 'baz'
# Test 2
xliffunit.set_rich_target([StringElem([u'foo', u'baz', G(id='oof', sub=[G(id='zab', sub=[u'bar', u'rab'])])])], u'fr')
target_dom_node = xliffunit.getlanguageNode(None, 1)
g_placeable = target_dom_node[0]
nested_g_placeable = g_placeable[0]
assert target_dom_node.text == u'foobaz'
assert g_placeable.tag == u'g'
print 'g_placeable.text: %s (%s)' % (g_placeable.text, type(g_placeable.text))
assert g_placeable.text is None
assert g_placeable.attrib[u'id'] == u'oof'
assert g_placeable.tail is None
assert nested_g_placeable.tag == u'g'
assert nested_g_placeable.text == u'barrab'
assert nested_g_placeable.attrib[u'id'] == u'zab'
assert nested_g_placeable.tail is None
xliffunit.rich_target[0].print_tree(2)
assert xliffunit.rich_target == [StringElem([u'foobaz', G(id='oof', sub=[G(id='zab', sub=[u'barrab'])])])]
示例3: test_target
def test_target(self):
xlifffile = xliff.xlifffile()
xliffunit = xlifffile.addsourceunit("Concept")
xliffunit.target = "Konsep"
newfile = xliff.xlifffile.parsestring(str(xlifffile))
print str(xlifffile)
assert newfile.findunit("Concept").target == "Konsep"
示例4: test_source
def test_source(self):
xlifffile = xliff.xlifffile()
xliffunit = xlifffile.addsourceunit("Concept")
xliffunit.source = "Term"
newfile = xliff.xlifffile.parsestring(str(xlifffile))
print str(xlifffile)
assert newfile.findunit("Concept") is None
assert newfile.findunit("Term") is not None
示例5: mergexliff
def mergexliff(self, templatesource, inputsource):
"""merges the sources of the given files and returns a new xlifffile object"""
templatefile = wStringIO.StringIO(templatesource)
inputfile = wStringIO.StringIO(inputsource)
outputfile = wStringIO.StringIO()
assert pomerge.mergestore(inputfile, outputfile, templatefile)
outputxliffstring = outputfile.getvalue()
print "Generated XML:"
print outputxliffstring
outputxlifffile = xliff.xlifffile(outputxliffstring)
return outputxlifffile
示例6: test_preserve_filename
def test_preserve_filename(self):
"""Ensures that the filename is preserved."""
xliffsource = self.xliffskeleton % '''<trans-unit xml:space="preserve">
<source>nonsense</source>
<target>matlhapolosa</target>
</trans-unit>'''
self.create_testfile("snippet.xlf", xliffsource)
xlifffile = xliff.xlifffile(self.open_testfile("snippet.xlf"))
assert xlifffile.filename.endswith("snippet.xlf")
xlifffile.parse(xliffsource)
assert xlifffile.filename.endswith("snippet.xlf")
示例7: test_basic
def test_basic(self):
xlifffile = xliff.xlifffile()
assert xlifffile.units == []
xlifffile.addsourceunit("Bla")
assert len(xlifffile.units) == 1
newfile = xliff.xlifffile.parsestring(str(xlifffile))
print str(xlifffile)
assert len(newfile.units) == 1
assert newfile.units[0].source == "Bla"
assert newfile.findunit("Bla").source == "Bla"
assert newfile.findunit("dit") is None
示例8: pretranslatexliff
def pretranslatexliff(self, input_source, template_source=None):
"""helper that converts strings to po source without requiring files"""
input_file = wStringIO.StringIO(input_source)
if template_source:
template_file = wStringIO.StringIO(template_source)
else:
template_file = None
output_file = wStringIO.StringIO()
pretranslate.pretranslate_file(input_file, output_file, template_file)
output_file.seek(0)
return xliff.xlifffile(output_file.read())
示例9: convertstore
def convertstore(self, theoofile, duplicatestyle="msgctxt"):
"""converts an entire oo file to a base class format (.po or XLIFF)"""
thetargetfile = xliff.xlifffile()
thetargetfile.setsourcelanguage(self.sourcelanguage)
thetargetfile.settargetlanguage(self.targetlanguage)
# create a header for the file
bug_url = 'http://qa.openoffice.org/issues/enter_bug.cgi' + ('''?subcomponent=ui&comment=&short_desc=Localization issue in file: %(filename)s&component=l10n&form_name=enter_issue''' % {"filename": theoofile.filename}).replace(" ", "%20").replace(":", "%3A")
# go through the oo and convert each element
for theoo in theoofile.units:
unitlist = self.convertelement(theoo)
for unit in unitlist:
thetargetfile.addunit(unit)
return thetargetfile
示例10: dump_xliff
def dump_xliff(project, locale, relative_path):
"""Dump .xliff file with relative path from database."""
locale_directory_path = get_locale_directory(project, locale)["path"]
path = os.path.join(locale_directory_path, relative_path)
resource = Resource.objects.filter(project=project, path=relative_path)
entities = Entity.objects.filter(resource=resource, obsolete=False)
with open(path, 'r+') as f:
xf = xliff.xlifffile(f)
# Update target-language attribute in file nodes
for node in xf.document.getroot().iterchildren(xf.namespaced("file")):
node.set("target-language", locale.code)
for unit in xf.units:
key = unit.getid()
try:
entity = Entity.objects.get(resource=resource, key=key)
except Entity.DoesNotExist as e:
log.error('%s: Entity "%s" does not exist' % (path, original))
continue
try:
translation = Translation.objects.filter(
entity=entity, locale=locale, approved=True) \
.latest('date').string
unit.settarget(translation)
except Translation.DoesNotExist as e:
# Remove "approved" attribute
try:
del unit.xmlelement.attrib['approved']
except KeyError:
pass
# Remove "target" element
target = unit.xmlelement.find(unit.namespaced("target"))
if target:
unit.xmlelement.remove(target)
# Erase file and then write, otherwise content gets appended
f.seek(0)
f.truncate()
f.writelines(xf.__str__())
log.debug("File updated: " + path)
示例11: test_alttrans
def test_alttrans(self):
"""Test xliff <alt-trans> accessors"""
xlifffile = xliff.xlifffile()
unit = xlifffile.addsourceunit("Testing")
unit.addalttrans("ginmi")
unit.addalttrans("shikenki")
alternatives = unit.getalttrans()
assert alternatives[0].source == "Testing"
assert alternatives[0].target == "ginmi"
assert alternatives[1].target == "shikenki"
assert not unit.target
unit.addalttrans("Tasting", origin="bob", lang="eng")
alternatives = unit.getalttrans()
assert alternatives[2].target == "Tasting"
alternatives = unit.getalttrans(origin="bob")
assert alternatives[0].target == "Tasting"
unit.delalttrans(alternatives[0])
assert len(unit.getalttrans(origin="bob")) == 0
alternatives = unit.getalttrans()
assert len(alternatives) == 2
assert alternatives[0].target == "ginmi"
assert alternatives[1].target == "shikenki"
#clean up:
alternatives = unit.getalttrans()
for alt in alternatives:
unit.delalttrans(alt)
unit.addalttrans("targetx", sourcetxt="sourcex")
# test that the source node is before the target node:
alt = unit.getalttrans()[0]
altformat = etree.tostring(alt.xmlelement)
if isinstance(altformat, bytes):
altformat=altformat.decode('utf-8')
print(altformat)
assert altformat.find("<source") < altformat.find("<target")
# test that a new target is still before alt-trans (bug 1098)
unit.target = "newester target"
unitformat = str(unit)
print(unitformat)
assert unitformat.find("<source") < unitformat.find("<target") < unitformat.find("<alt-trans")
示例12: mergexliff
def mergexliff(self, templatesource, inputsource, mergeblanks="yes",
mergefuzzy="yes",
mergecomments="yes"):
"""merges the sources of the given files and returns a new xlifffile
object"""
templatefile = wStringIO.StringIO(templatesource)
inputfile = wStringIO.StringIO(inputsource)
outputfile = wStringIO.StringIO()
assert pomerge.mergestore(inputfile, outputfile, templatefile,
mergeblanks=mergeblanks,
mergefuzzy=mergefuzzy,
mergecomments=mergecomments)
outputxliffstring = outputfile.getvalue()
print("Generated XML:")
print(outputxliffstring)
outputxlifffile = xliff.xlifffile(outputxliffstring)
return outputxlifffile
示例13: convertstore
def convertstore(self, theoofile, duplicatestyle="msgctxt"):
"""converts an entire oo file to a base class format (.po or XLIFF)"""
thetargetfile = xliff.xlifffile()
thetargetfile.setsourcelanguage(self.sourcelanguage)
thetargetfile.settargetlanguage(self.targetlanguage)
# create a header for the file
bug_url = 'http://qa.openoffice.org/issues/enter_bug.cgi?%s' % \
urlencode({"subcomponent": "ui",
"comment": "",
"short_desc": "Localization issue in file: %s" % \
theoofile.filename,
"component": "l10n",
"form_name": "enter_issue",
})
# go through the oo and convert each element
for theoo in theoofile.units:
unitlist = self.convertelement(theoo)
for unit in unitlist:
thetargetfile.addunit(unit)
return thetargetfile
示例14: test_rich_source
def test_rich_source(self):
xlifffile = xliff.xlifffile()
xliffunit = xlifffile.addsourceunit(u'')
# Test 1
xliffunit.rich_source = [StringElem([u'foo', X(id='bar'), u'baz'])]
source_dom_node = xliffunit.getlanguageNode(None, 0)
x_placeable = source_dom_node[0]
assert source_dom_node.text == 'foo'
assert x_placeable.tag == u'x'
assert x_placeable.attrib['id'] == 'bar'
assert x_placeable.tail == 'baz'
xliffunit.rich_source[0].print_tree(2)
print xliffunit.rich_source
assert xliffunit.rich_source == [StringElem([StringElem(u'foo'), X(id='bar'), StringElem(u'baz')])]
# Test 2
xliffunit.rich_source = [StringElem([u'foo', u'baz', G(id='oof', sub=[G(id='zab', sub=[u'bar', u'rab'])])])]
source_dom_node = xliffunit.getlanguageNode(None, 0)
g_placeable = source_dom_node[0]
nested_g_placeable = g_placeable[0]
assert source_dom_node.text == u'foobaz'
assert g_placeable.tag == u'g'
assert g_placeable.text is None
assert g_placeable.attrib[u'id'] == u'oof'
assert g_placeable.tail is None
assert nested_g_placeable.tag == u'g'
assert nested_g_placeable.text == u'barrab'
assert nested_g_placeable.attrib[u'id'] == u'zab'
assert nested_g_placeable.tail is None
rich_source = xliffunit.rich_source
rich_source[0].print_tree(2)
assert rich_source == [StringElem([u'foobaz', G(id='oof', sub=[G(id='zab', sub=[u'barrab'])])])]
示例15: test_multiple_filenodes
def test_multiple_filenodes(self):
xlfsource = '''<?xml version="1.0" encoding="utf-8"?>
<xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1">
<file original="file0" source-language="en" datatype="plaintext">
<body>
<trans-unit id="hello" approved="yes">
<source>Hello</source>
</trans-unit>
</body>
</file>
<file original="file1" source-language="en" datatype="plaintext">
<body>
<trans-unit id="world" approved="yes">
<source>World</source>
</trans-unit>
</body>
</file>
</xliff>'''
xfile = xliff.xlifffile.parsestring(xlfsource)
assert len(xfile.units) == 2
assert xfile.units[0].getid() == "file0\x04hello"
assert xfile.units[1].getid() == "file1\x04world"
xunit = xliff.xlifffile.UnitClass(source="goodbye")
xunit.setid("file2\x04goodbye")
xfile.addunit(xunit)
assert xfile.units[2].getid() == "file2\x04goodbye"
# if there is no file set it will use the active context
xunit = xliff.xlifffile.UnitClass(source="lastfile")
xunit.setid("lastfile")
xfile.addunit(xunit)
assert xfile.units[3].getid() == "file2\x04lastfile"
newxfile = xliff.xlifffile()
newxfile.addunit(xfile.units[0])
newxfile.addunit(xfile.units[1])
assert newxfile.units[0].getid() == "file0\x04hello"
assert newxfile.units[1].getid() == "file1\x04world"
assert newxfile.getfilenode("file0") is not None
assert newxfile.getfilenode("file1") is not None
assert not newxfile.getfilenode("foo")