Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: cms/converters.py

Issue 6440550915899392: Issue 2139 - Allow nested translations for tag attributes. (Closed)
Patch Set: Created April 16, 2015, 10:53 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cms/converters.py
diff --git a/cms/converters.py b/cms/converters.py
index b853f78c4eb0625671a797a6640006c772929ad6..86c934b7fe70d7454bdf7f0cf3af76e3b8527a90 100644
--- a/cms/converters.py
+++ b/cms/converters.py
@@ -145,6 +145,7 @@ class Converter:
# Insert attributes
result = escape(result)
def stringify_attribute((name, value)):
+ value = self.insert_localized_strings(value, escapes)
if name == "href":
link_locale, link = self._params["source"].resolve_link(value, locale)
if link:
@@ -168,20 +169,40 @@ class Converter:
return result
def insert_localized_strings(self, text, escapes, to_html=lambda s: s):
- def lookup_string(match):
- name, comment, default = match.groups()
- default = to_html(default).strip()
-
- # Note: We currently ignore the comment, it is only relevant when
- # generating the master translation.
- return self.localize_string(name, default, self._params["localedata"], escapes)
-
- return re.sub(
- r"\{\{\s*([\w\-]+)(?:\[(.*?)\])?\s+(.*?)\}\}",
- lookup_string,
- text,
- flags=re.S
- )
+ # Find the positions for the top-level translatable strings
+ level = 0
+ start = None
+ positions = []
+ for m in re.finditer(r"(\{\{|\}\})", text):
+ if m.group(0) == "{{":
+ if level == 0:
+ start = m.start()
+ level += 1
+ else:
+ level -= 1
+ if level == 0:
+ positions.append((start, m.end()))
+
+ # Replace each string with it's translation
+ parts = []
+ last_end = 0
+ for start, end in positions:
+ # Append any text before this tag since the last one
+ parts.append(text[last_end:start])
+ # Append our localized string
+ name, comment, default = re.match(
+ r"\{\{\s*([\w\-]+)(?:\[(.*?)\])?\s+(.*)\}\}",
Wladimir Palant 2015/04/16 21:01:13 Only one nesting level is actually allowed here. S
kzar 2015/04/21 11:28:57 With a couple of tweaks I managed to get your rege
kzar 2015/04/21 14:44:53 OK I made another change to the regexp to hopefull
+ text[start:end],
+ flags=re.S
+ ).groups()
+ parts.append(self.localize_string(
+ name,
+ to_html(default).strip(),
+ self._params["localedata"],
+ escapes
+ ))
+ last_end = end
+ return "".join(parts) + text[last_end:]
def process_links(self, text):
def process_link(match):
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld