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

Delta Between Two Patch Sets: cms/converters.py

Issue 29317015: Issue 2625 - [cms] Crowdin synchronisation script (Closed)
Left Patch Set: Created June 15, 2015, 2:12 p.m.
Right Patch Set: Give query_params a default value Created July 16, 2015, 12:47 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « cms/bin/translate.py ('k') | cms/sources.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 self._key = key 118 self._key = key
119 self._attribute_parser = AttributeParser(self.whitelist) 119 self._attribute_parser = AttributeParser(self.whitelist)
120 120
121 # Read in any parameters specified at the beginning of the file 121 # Read in any parameters specified at the beginning of the file
122 lines = params[key].splitlines(True) 122 lines = params[key].splitlines(True)
123 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]): 123 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]):
124 name, value = lines.pop(0).split("=", 1) 124 name, value = lines.pop(0).split("=", 1)
125 params[name.strip()] = value.strip() 125 params[name.strip()] = value.strip()
126 params[key] = "".join(lines) 126 params[key] = "".join(lines)
127 127
128 def localize_string(self, name, default, comment, localedata, escapes): 128 def localize_string(self, page, name, default, comment, localedata, escapes):
kzar 2015/06/15 14:24:28 We previously threw away string comments, now we n
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 # Extract tag attributes from default string 136 # Extract tag attributes from default string
137 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"]) 137 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"])
138 138
139 # Get translation 139 # Get translation
140 locale = self._params["locale"] 140 locale = self._params["locale"]
141 if locale == self._params["defaultlocale"]: 141 if locale == self._params["defaultlocale"]:
142 result = default 142 result = default
143 elif name in localedata: 143 elif name in localedata:
144 result = localedata[name].strip() 144 result = localedata[name].strip()
145 else: 145 else:
146 result = default 146 result = default
147 self.missing_translations += 1 147 self.missing_translations += 1
148 self.total_translations += 1 148 self.total_translations += 1
149 149
150 # Keep a record of the default translations 150 # Perform callback with the string if required, e.g. for the translations sc ript
151 if locale == self._params["defaultlocale"]: 151 callback = self._params["localized_string_callback"]
Wladimir Palant 2015/06/29 19:05:38 This case is already checked above (getting transl
kzar 2015/07/02 12:33:12 Done.
152 localedata[name] = result 152 if callback:
153 # Append a note about fixed strings to the string comment for translators 153 callback(page, locale, name, result, comment, fixed_strings)
kzar 2015/06/15 14:24:28 Without this translators would have way to know wh
154 if fixed_strings: 154
155 comment = comment + " " if comment else ""
156 self._params["localecomments"][name] = comment + (
157 "[" + ", ".join(["%d: %s" % (i, s) for i, s in
Wladimir Palant 2015/06/29 19:05:38 ", ".join("{%d}: %s" (i, s) for i, s in ...) In o
kzar 2015/07/02 12:33:13 Done.
158 enumerate(fixed_strings, 1)]) + "]"
159 )
Wladimir Palant 2015/06/29 19:05:37 And what about comments for strings without fixed
kzar 2015/07/02 12:33:13 Whoops, good point. Done.
160 155
161 # Insert fixed strings 156 # Insert fixed strings
162 for i, fixed_string in enumerate(fixed_strings, 1): 157 for i, fixed_string in enumerate(fixed_strings, 1):
163 result = result.replace("{%d}" % i, fixed_string) 158 result = result.replace("{%d}" % i, fixed_string)
164 159
165 # Insert attributes 160 # Insert attributes
166 result = escape(result) 161 result = escape(result)
167 def stringify_attribute((name, value)): 162 def stringify_attribute((name, value)):
168 return '%s="%s"' % ( 163 return '%s="%s"' % (
169 escape(name), 164 escape(name),
(...skipping 19 matching lines...) Expand all
189 r"<%s>\1</%s>" % (tag, tag), 184 r"<%s>\1</%s>" % (tag, tag),
190 result, flags=re.S 185 result, flags=re.S
191 ) 186 )
192 return result 187 return result
193 188
194 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): 189 def insert_localized_strings(self, text, escapes, to_html=lambda s: s):
195 def lookup_string(match): 190 def lookup_string(match):
196 name, comment, default = match.groups() 191 name, comment, default = match.groups()
197 default = to_html(default).strip() 192 default = to_html(default).strip()
198 193
199 # Note: The comment, it is only relevant when uploading translations 194 return self.localize_string(self._params["page"], name, default,
Wladimir Palant 2015/06/29 19:05:38 This is the wrong place for this comment, it is de
kzar 2015/07/02 12:33:13 Done.
200 return self.localize_string(name, default, comment, self._params["localeda ta"], escapes) 195 comment, self._params["localedata"], escapes)
201 196
202 return re.sub( 197 return re.sub(
203 r"{{\s*" 198 r"{{\s*"
204 r"([\w\-]+)" # String ID 199 r"([\w\-]+)" # String ID
205 r"(?:\[(.*?)\])?" # Optional comment 200 r"(?:\[(.*?)\])?" # Optional comment
206 r"\s+" 201 r"\s+"
207 r"((?:(?!{{).|" # Translatable text 202 r"((?:(?!{{).|" # Translatable text
208 r"{{(?:(?!}}).)*}}" # Nested translation 203 r"{{(?:(?!}}).)*}}" # Nested translation
209 r")*?)" 204 r")*?)"
210 r"}}", 205 r"}}",
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 module = template.make_module(self._params) 350 module = template.make_module(self._params)
356 for key, value in module.__dict__.iteritems(): 351 for key, value in module.__dict__.iteritems():
357 if not key.startswith("_"): 352 if not key.startswith("_"):
358 self._params[key] = value 353 self._params[key] = value
359 354
360 result = unicode(module) 355 result = unicode(module)
361 result = self.process_links(result) 356 result = self.process_links(result)
362 return result 357 return result
363 358
364 def translate(self, default, name, comment=None): 359 def translate(self, default, name, comment=None):
365 # Note: The comment, it is only relevant when uploading translations 360 return jinja2.Markup(self.localize_string(
Wladimir Palant 2015/06/29 19:05:38 Same here, this comment should be moved into funct
kzar 2015/07/02 12:33:13 Done.
366 localedata = self._params["localedata"] 361 self._params["page"], name, default, comment,
367 return jinja2.Markup(self.localize_string(name, default, comment, localedata , html_escapes)) 362 self._params["localedata"], html_escapes
363 ))
368 364
369 def get_string(self, name, page): 365 def get_string(self, name, page):
370 default = self._params["source"].read_locale(self._params["locale"], page)[n ame] 366 localedata = self._params["source"].read_locale(self._params["locale"], page )
kzar 2015/06/15 14:24:28 This change is required so that strings included w
Wladimir Palant 2015/06/29 19:05:38 This change also happens to be wrong. Each page in
kzar 2015/07/02 12:33:13 Done.
371 localedata = self._params["localedata"] 367 default = localedata[name]
372 return jinja2.Markup(self.localize_string(name, default, "", localedata, htm l_escapes)) 368 return jinja2.Markup(self.localize_string(
369 page, name, default, "", localedata, html_escapes
370 ))
373 371
374 def get_page_content(self, page, locale=None): 372 def get_page_content(self, page, locale=None):
375 from cms.utils import get_page_params 373 from cms.utils import get_page_params
376 374
377 if locale is None: 375 if locale is None:
378 locale = self._params["locale"] 376 locale = self._params["locale"]
379 return get_page_params(self._params["source"], locale, page) 377 return get_page_params(self._params["source"], locale, page)
380 378
381 def linkify(self, page, locale=None, **attrs): 379 def linkify(self, page, locale=None, **attrs):
382 if locale is None: 380 if locale is None:
(...skipping 24 matching lines...) Expand all
407 stack.pop() 405 stack.pop()
408 stack[-1]["subitems"].append(item) 406 stack[-1]["subitems"].append(item)
409 stack.append(item) 407 stack.append(item)
410 return structured 408 return structured
411 409
412 converters = { 410 converters = {
413 "html": RawConverter, 411 "html": RawConverter,
414 "md": MarkdownConverter, 412 "md": MarkdownConverter,
415 "tmpl": TemplateConverter, 413 "tmpl": TemplateConverter,
416 } 414 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld