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: Created Aug. 21, 2015, 9:47 a.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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 params[key] = "".join(lines) 126 params[key] = "".join(lines)
127 127
128 def localize_string(self, page, name, default, comment, localedata, escapes): 128 def localize_string(self, page, name, default, comment, localedata, escapes):
129 def escape(s): 129 def escape(s):
130 return re.sub(r".", 130 return re.sub(r".",
131 lambda match: escapes.get(match.group(0), match.group(0)), 131 lambda match: escapes.get(match.group(0), match.group(0)),
132 s, flags=re.S) 132 s, flags=re.S)
133 def re_escape(s): 133 def re_escape(s):
134 return re.escape(escape(s)) 134 return re.escape(escape(s))
135 135
136 locale = self._params["locale"]
137 duplicate_string = not default
Sebastian Noack 2015/08/23 14:24:12 Nit: I find this variable name kind misleading. Th
kzar 2015/08/23 15:28:53 Done.
138 is_default_locale = locale == self._params["defaultlocale"]
139
140 # Handle duplicated strings
141 if duplicate_string:
142 try:
143 default = self._params["localedata"][name]
144 except KeyError:
145 raise Exception("Text not yet defined for string %s on page %s" %
146 (name, self._params["page"]))
147 elif is_default_locale:
148 self._params["localedata"][name] = default
149
136 # Extract tag attributes from default string 150 # Extract tag attributes from default string
137 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"]) 151 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"])
138 152
139 # Get translation 153 # Get translation
140 locale = self._params["locale"] 154 if is_default_locale:
141 if locale == self._params["defaultlocale"]:
142 result = default 155 result = default
143 elif name in localedata: 156 elif name in localedata:
144 result = localedata[name].strip() 157 result = localedata[name].strip()
145 else: 158 else:
146 result = default 159 result = default
147 self.missing_translations += 1 160 self.missing_translations += 1
148 self.total_translations += 1 161 self.total_translations += 1
149 162
150 # Perform callback with the string if required, e.g. for the translations sc ript 163 if not duplicate_string:
151 callback = self._params["localized_string_callback"] 164 # Perform callback with the string if required,
152 if callback: 165 # e.g. for the translations script
153 callback(page, locale, name, result, comment, fixed_strings) 166 callback = self._params["localized_string_callback"]
154 167 if callback:
168 callback(page, locale, name, result, comment, fixed_strings)
155 169
156 # Insert fixed strings 170 # Insert fixed strings
157 for i, fixed_string in enumerate(fixed_strings, 1): 171 for i, fixed_string in enumerate(fixed_strings, 1):
158 result = result.replace("{%d}" % i, fixed_string) 172 result = result.replace("{%d}" % i, fixed_string)
159 173
160 # Insert attributes 174 # Insert attributes
161 result = escape(result) 175 result = escape(result)
162 def stringify_attribute((name, value)): 176 def stringify_attribute((name, value)):
163 return '%s="%s"' % ( 177 return '%s="%s"' % (
164 escape(name), 178 escape(name),
(...skipping 17 matching lines...) Expand all
182 result = re.sub( 196 result = re.sub(
183 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), 197 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)),
184 r"<%s>\1</%s>" % (tag, tag), 198 r"<%s>\1</%s>" % (tag, tag),
185 result, flags=re.S 199 result, flags=re.S
186 ) 200 )
187 return result 201 return result
188 202
189 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): 203 def insert_localized_strings(self, text, escapes, to_html=lambda s: s):
190 def lookup_string(match): 204 def lookup_string(match):
191 name, comment, default = match.groups() 205 name, comment, default = match.groups()
192 default = to_html(default).strip() 206 if default:
Sebastian Noack 2015/08/23 14:24:12 Nit: If I understand the code correctly default is
kzar 2015/08/23 15:28:53 I found that default is actually sometimes None du
193 207 default = to_html(default).strip()
194 return self.localize_string(self._params["page"], name, default, 208 return self.localize_string(self._params["page"], name, default,
195 comment, self._params["localedata"], escapes) 209 comment, self._params["localedata"], escapes)
196 210
197 return re.sub( 211 return re.sub(
198 r"{{\s*" 212 r"{{\s*"
199 r"([\w\-]+)" # String ID 213 r"([\w\-]+)" # String ID
200 r"(?:\[(.*?)\])?" # Optional comment 214 r"(?:(?:\[(.*?)\])?" # Optional comment
201 r"\s+" 215 r"\s+"
202 r"((?:(?!{{).|" # Translatable text 216 r"((?:(?!{{).|" # Translatable text
203 r"{{(?:(?!}}).)*}}" # Nested translation 217 r"{{(?:(?!}}).)*}}" # Nested translation
204 r")*?)" 218 r")*?)"
219 r")?"
205 r"}}", 220 r"}}",
206 lookup_string, 221 lookup_string,
207 text, 222 text,
208 flags=re.S 223 flags=re.S
209 ) 224 )
210 225
211 def process_links(self, text): 226 def process_links(self, text):
212 def process_link(match): 227 def process_link(match):
213 pre, attr, url, post = match.groups() 228 pre, attr, url, post = match.groups()
214 url = jinja2.Markup(url).unescape() 229 url = jinja2.Markup(url).unescape()
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 stack.pop() 420 stack.pop()
406 stack[-1]["subitems"].append(item) 421 stack[-1]["subitems"].append(item)
407 stack.append(item) 422 stack.append(item)
408 return structured 423 return structured
409 424
410 converters = { 425 converters = {
411 "html": RawConverter, 426 "html": RawConverter,
412 "md": MarkdownConverter, 427 "md": MarkdownConverter,
413 "tmpl": TemplateConverter, 428 "tmpl": TemplateConverter,
414 } 429 }
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