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

Side by Side Diff: cms/converters.py

Issue 29324503: Issue 2936 - Support syntax for duplicate translatable strings (Closed)
Patch Set: Store comment for previously seen strings Created Aug. 24, 2015, 3:11 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 110
111 class Converter: 111 class Converter:
112 whitelist = {"a", "em", "strong", "code", "span"} 112 whitelist = {"a", "em", "strong", "code", "span"}
113 missing_translations = 0 113 missing_translations = 0
114 total_translations = 0 114 total_translations = 0
115 115
116 def __init__(self, params, key="pagedata"): 116 def __init__(self, params, key="pagedata"):
117 self._params = params 117 self._params = params
118 self._key = key 118 self._key = key
119 self._attribute_parser = AttributeParser(self.whitelist) 119 self._attribute_parser = AttributeParser(self.whitelist)
120 self._seen_defaults = {}
120 121
121 # Read in any parameters specified at the beginning of the file 122 # Read in any parameters specified at the beginning of the file
122 lines = params[key].splitlines(True) 123 lines = params[key].splitlines(True)
123 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]): 124 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]):
124 name, value = lines.pop(0).split("=", 1) 125 name, value = lines.pop(0).split("=", 1)
125 params[name.strip()] = value.strip() 126 params[name.strip()] = value.strip()
126 params[key] = "".join(lines) 127 params[key] = "".join(lines)
127 128
128 def localize_string(self, page, name, default, comment, localedata, escapes): 129 def localize_string(self, page, name, default, comment, localedata, escapes):
129 def escape(s): 130 def escape(s):
130 return re.sub(r".", 131 return re.sub(r".",
131 lambda match: escapes.get(match.group(0), match.group(0)), 132 lambda match: escapes.get(match.group(0), match.group(0)),
132 s, flags=re.S) 133 s, flags=re.S)
133 def re_escape(s): 134 def re_escape(s):
134 return re.escape(escape(s)) 135 return re.escape(escape(s))
135 136
137 # Handle duplicated strings
138 if default:
139 self._seen_defaults[(page, name)] = (default, comment)
140 else:
141 try:
142 default, comment = self._seen_defaults[(page, name)]
143 except KeyError:
144 raise Exception("Text not yet defined for string %s on page %s" %
145 (name, page))
146
136 # Extract tag attributes from default string 147 # Extract tag attributes from default string
137 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"]) 148 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"])
138 149
139 # Get translation 150 # Get translation
140 locale = self._params["locale"] 151 locale = self._params["locale"]
141 if locale == self._params["defaultlocale"]: 152 if locale == self._params["defaultlocale"]:
142 result = default 153 result = default
143 elif name in localedata: 154 elif name in localedata:
144 result = localedata[name].strip() 155 result = localedata[name].strip()
145 else: 156 else:
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 result = re.sub( 193 result = re.sub(
183 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), 194 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)),
184 r"<%s>\1</%s>" % (tag, tag), 195 r"<%s>\1</%s>" % (tag, tag),
185 result, flags=re.S 196 result, flags=re.S
186 ) 197 )
187 return result 198 return result
188 199
189 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): 200 def insert_localized_strings(self, text, escapes, to_html=lambda s: s):
190 def lookup_string(match): 201 def lookup_string(match):
191 name, comment, default = match.groups() 202 name, comment, default = match.groups()
192 default = to_html(default).strip() 203 if default:
193 204 default = to_html(default).strip()
194 return self.localize_string(self._params["page"], name, default, 205 return self.localize_string(self._params["page"], name, default,
195 comment, self._params["localedata"], escapes) 206 comment, self._params["localedata"], escapes)
196 207
197 return re.sub( 208 return re.sub(
198 r"{{\s*" 209 r"{{\s*"
199 r"([\w\-]+)" # String ID 210 r"([\w\-]+)" # String ID
200 r"(?:\[(.*?)\])?" # Optional comment 211 r"(?:(?:\[(.*?)\])?" # Optional comment
201 r"\s+" 212 r"\s+"
202 r"((?:(?!{{).|" # Translatable text 213 r"((?:(?!{{).|" # Translatable text
203 r"{{(?:(?!}}).)*}}" # Nested translation 214 r"{{(?:(?!}}).)*}}" # Nested translation
204 r")*?)" 215 r")*?)"
216 r")?"
205 r"}}", 217 r"}}",
206 lookup_string, 218 lookup_string,
207 text, 219 text,
208 flags=re.S 220 flags=re.S
209 ) 221 )
210 222
211 def process_links(self, text): 223 def process_links(self, text):
212 def process_link(match): 224 def process_link(match):
213 pre, attr, url, post = match.groups() 225 pre, attr, url, post = match.groups()
214 url = jinja2.Markup(url).unescape() 226 url = jinja2.Markup(url).unescape()
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 stack.pop() 417 stack.pop()
406 stack[-1]["subitems"].append(item) 418 stack[-1]["subitems"].append(item)
407 stack.append(item) 419 stack.append(item)
408 return structured 420 return structured
409 421
410 converters = { 422 converters = {
411 "html": RawConverter, 423 "html": RawConverter,
412 "md": MarkdownConverter, 424 "md": MarkdownConverter,
413 "tmpl": TemplateConverter, 425 "tmpl": TemplateConverter,
414 } 426 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld