本文整理汇总了Python中translate.lang.data.forceunicode函数的典型用法代码示例。如果您正苦于以下问题:Python forceunicode函数的具体用法?Python forceunicode怎么用?Python forceunicode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了forceunicode函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: format_suggestions
def format_suggestions(self, id, response):
"""clean up open tran suggestion and use the same format as tmserver"""
suggestions = self._loads_safe(response)
if not suggestions:
return []
id = data.forceunicode(id)
self.last_suggestions = suggestions
results = []
for suggestion in suggestions:
#check for fuzzyness at the 'flag' member:
for project in suggestion['projects']:
if project['flags'] == 0:
break
else:
continue
result = {}
result['target'] = data.forceunicode(suggestion['text'])
result['tmsource'] = suggestion['projects'][0]['name']
result['source'] = data.forceunicode(suggestion['projects'][0]['orig_phrase'])
#open-tran often gives too many results with many which can't really be
#considered to be suitable for translation memory
result['quality'] = self.comparer.similarity(id, result['source'], self.min_similarity)
if result['quality'] >= self.min_similarity:
results.append(result)
results.sort(key=lambda match: match['quality'], reverse=True)
results = results[:self.max_candidates]
return results
示例2: get_xml_text_value
def get_xml_text_value(self, xmltarget):
if (len(xmltarget) == 0):
# There are no html markups, so unescaping it as plain text.
return self.unescape(xmltarget.text)
else:
# There are html markups, so clone it to perform unescaping for all elements.
cloned_target = copy.deepcopy(xmltarget)
# Unescaping texts.
if (cloned_target.text is not None):
cloned_target.text = self.unescape(cloned_target.text)
for xmlelement in cloned_target.iterdescendants():
if (xmlelement.text is not None):
xmlelement.text = self.unescape(xmlelement.text)
if (xmlelement.tail is not None):
xmlelement.tail = self.unescape(xmlelement.tail)
# Grab root text (using a temporary xml element for text escaping)
if (cloned_target.text is not None):
tmp_element = etree.Element('t')
tmp_element.text = cloned_target.text
target = data.forceunicode(etree.tostring(tmp_element, encoding='utf-8')[3:-4])
else:
target = u''
# Include markup as well
target += u''.join([data.forceunicode(etree.tostring(child, encoding='utf-8'))
for child in cloned_target.iterchildren()])
return target
示例3: settarget
def settarget(self, text):
# This is a fairly destructive implementation. Don't assume that this
# is necessarily correct in all regards, but it does deal with a lot of
# cases. It is hard to deal with plurals.
#
# Firstly deal with reinitialising to None or setting to identical
# string.
self._rich_target = None
if self.gettarget() == text:
return
strings = []
if isinstance(text, multistring):
strings = text.strings
elif isinstance(text, list):
strings = text
else:
strings = [text]
targetnode = self._gettargetnode()
type = targetnode.get("type")
targetnode.clear()
if type:
targetnode.set("type", type)
if self.hasplural() or len(strings) > 1:
self.xmlelement.set("numerus", "yes")
for string in strings:
numerus = etree.SubElement(targetnode, self.namespaced("numerusform"))
numerus.text = data.forceunicode(string) or ""
# manual, nasty pretty printing. See bug 1420.
numerus.tail = "\n "
else:
targetnode.text = data.forceunicode(text) or ""
targetnode.tail = "\n "
示例4: target
def target(self):
targetnode = self._gettargetnode()
if targetnode is None:
etree.SubElement(self.xmlelement, self.namespaced("translation"))
return None
if self.hasplural():
numerus_nodes = targetnode.findall(self.namespaced("numerusform"))
return multistring([data.forceunicode(node.text) or u"" for node in numerus_nodes])
else:
return data.forceunicode(targetnode.text) or u""
示例5: fixup
def fixup(source, response):
source = data.forceunicode(source)
response = data.forceunicode(response)
from translate.filters.autocorrect import correct
tmp = correct(source, response)
if tmp:
response = tmp
response = response.replace(u" __::__ ", "\n")
# and again for the sake of \n\n:
response = response.replace(u"__::__ ", "\n")
response = response.replace(u"( ", u"(")
for c, repl in punc_tuples:
response = response.replace(repl, c)
return response
示例6: setsource
def setsource(self, source):
self._rich_source = None
source = data.forceunicode(source)
if self.personality == "mozilla" or self.personality == "skype":
self.value = quote.mozillapropertiesencode(source or u"")
else:
self.value = quote.javapropertiesencode(source or u"")
示例7: settarget
def settarget(self, target):
self._rich_target = None
target = data.forceunicode(target)
if self.personality == "mozilla" or self.personality == "skype":
self.translation = quote.mozillapropertiesencode(target or u"")
else:
self.translation = quote.javapropertiesencode(target or u"")
示例8: addnote
def addnote(self, text, origin=None, position="append"):
if origin in ['programmer', 'developer', 'source code', None]:
text = data.forceunicode(text)
self.comments.append(text)
else:
return super(propunit, self).addnote(text, origin=origin,
position=position)
示例9: addnote
def addnote(self, text, origin=None, position="append"):
"""This is modeled on the XLIFF method.
See :meth:`translate.storage.xliff.xliffunit.addnote`
"""
# ignore empty strings and strings without non-space characters
if not (text and text.strip()):
return
text = data.forceunicode(text)
commentlist = self.othercomments
linestart = "# "
autocomments = False
if origin in ["programmer", "developer", "source code"]:
autocomments = True
commentlist = self.automaticcomments
linestart = "#. "
text = text.split("\n")
newcomments = [linestart + line + "\n" for line in text]
if position == "append":
newcomments = commentlist + newcomments
elif position == "prepend":
newcomments = newcomments + commentlist
if autocomments:
self.automaticcomments = newcomments
else:
self.othercomments = newcomments
示例10: settarget
def settarget(self, text, lang='xx', append=False):
"""Sets the "target" string (second language), or alternatively
appends to the list"""
#XXX: we really need the language - can't really be optional, and we
# need to propagate it
if self._rich_target is not None:
self._rich_target = None
text = data.forceunicode(text)
# Firstly deal with reinitialising to None or setting to identical
# string
if self.gettarget() == text:
return
languageNode = self.get_target_dom(None)
if not text is None:
if languageNode is None:
languageNode = self.createlanguageNode(lang, text, "target")
self.set_target_dom(languageNode, append)
else:
'''
if self.textNode:
terms = languageNode.iter(self.namespaced(self.textNode))
try:
languageNode = next(terms)
except StopIteration as e:
pass
languageNode.text = text
'''
# try to update all content including tags in targt node,
# but here only suits/tested for iws files, so need more checks.
# later could need to use a more better method to update target node.
self.xmlelement.remove(languageNode)
languageNode = self.createlanguageNode(lang, text, "target")
self.set_target_dom(languageNode, append)
else:
self.set_target_dom(None, False)
示例11: getsource
def getsource(self):
# TODO: support <byte>. See bug 528.
sourcenode = self._getsourcenode()
if self.hasplural():
return multistring([sourcenode.text])
else:
return data.forceunicode(sourcenode.text)
示例12: settarget
def settarget(self, text, lang='xx', append=False):
"""Sets the "target" string (second language), or alternatively
appends to the list"""
#XXX: we really need the language - can't really be optional, and we
# need to propagate it
if self._rich_target is not None:
self._rich_target = None
text = data.forceunicode(text)
# Firstly deal with reinitialising to None or setting to identical
# string
if self.gettarget() == text:
return
languageNode = self.get_target_dom(None)
if not text is None:
if languageNode is None:
languageNode = self.createlanguageNode(lang, text, "target")
self.set_target_dom(languageNode, append)
else:
if self.textNode:
terms = languageNode.iter(self.namespaced(self.textNode))
try:
languageNode = terms.next()
except StopIteration, e:
pass
languageNode.text = text
示例13: gettarget
def gettarget(self, lang=None):
# Grab inner text
target = self.unescape(self.xmlelement.text or u"")
# Include markup as well
target += u"".join(
[data.forceunicode(etree.tostring(child, encoding="utf-8")) for child in self.xmlelement.iterchildren()]
)
return target
示例14: target
def target(self, target):
# Firstly deal with reinitialising to None or setting to identical
# string.
self._rich_target = None
if self.target == target:
return
targetnode = self._gettargetnode()
targetnode.clear()
targetnode.text = data.forceunicode(target) or u""
示例15: select_match
def select_match(self, match_data):
"""Handle a match-selection event.
(This method is used as View-Controller communications)"""
unit_controller = self.main_controller.unit_controller
target_n = unit_controller.view.focused_target_n
old_text = unit_controller.view.get_target_n(target_n)
textbox = unit_controller.view.targets[target_n]
self.main_controller.undo_controller.push_current_text(textbox)
unit_controller.set_unit_target(target_n, forceunicode(match_data['target']))