Skip to content

Commit

Permalink
normalize text nodes before implicitly translating them as a whole
Browse files Browse the repository at this point in the history
before translation nodes were created word by word, which does not work very well for most languages

also note that leading and trailing white space is preserved
  • Loading branch information
witsch committed Mar 26, 2013
1 parent 78ab3a4 commit e0869aa
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions src/chameleon/zpt/program.py
Expand Up @@ -603,29 +603,17 @@ def visit_text(self, node):
if not translation:
return nodes.Text(node)

seq = []
while node:
m = re.search('\s+', node)
if m is not None:
s = m.start()
if s:
t = node[:s]
seq.append(nodes.Translate(t, nodes.Text(t)))

e = m.end()
whitespace = nodes.Text(node[s:e])
seq.append(whitespace)

if e < len(node):
node = node[e:]
continue

else:
seq.append(nodes.Translate(node, nodes.Text(node)))

break

return nodes.Sequence(seq)
match = re.search(r'(\s*)(.*\S)(\s*)', node)
if match is not None:
prefix, text, suffix = match.groups()
normalized = re.sub('\s+', ' ', text)
return nodes.Sequence([
nodes.Text(prefix),
nodes.Translate(normalized, nodes.Text(normalized)),
nodes.Text(suffix),
])
else:
return nodes.Text(node)

def _pop_defaults(self, kwargs, *attributes):
for attribute in attributes:
Expand Down

0 comments on commit e0869aa

Please sign in to comment.