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

Side by Side Diff: cms/converters.py

Issue 29317015: Issue 2625 - [cms] Crowdin synchronisation script (Closed)
Patch Set: Addressed Wladimir's feedback Created July 2, 2015, 12:29 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
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 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, 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 # 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 # Store the default string if required, e.g. for the translations script
144 if self._params["record_default_strings"]:
Sebastian Noack 2015/07/08 13:03:20 Is there any particular reason we call that functi
Wladimir Palant 2015/07/08 23:11:08 That's assumptions, the kind that might not be tru
Sebastian Noack 2015/07/09 21:26:55 Or the other way around. As far as I see it this c
Wladimir Palant 2015/07/10 21:24:04 True. If we generalize the parameter name into som
kzar 2015/07/11 19:21:17 Done.
145 self._params["record_default_strings"](page, name, default,
146 comment, fixed_strings)
143 elif name in localedata: 147 elif name in localedata:
144 result = localedata[name].strip() 148 result = localedata[name].strip()
145 else: 149 else:
146 result = default 150 result = default
147 self.missing_translations += 1 151 self.missing_translations += 1
148 self.total_translations += 1 152 self.total_translations += 1
149 153
150 # Insert fixed strings 154 # Insert fixed strings
151 for i, fixed_string in enumerate(fixed_strings, 1): 155 for i, fixed_string in enumerate(fixed_strings, 1):
152 result = result.replace("{%d}" % i, fixed_string) 156 result = result.replace("{%d}" % i, fixed_string)
(...skipping 25 matching lines...) Expand all
178 r"<%s>\1</%s>" % (tag, tag), 182 r"<%s>\1</%s>" % (tag, tag),
179 result, flags=re.S 183 result, flags=re.S
180 ) 184 )
181 return result 185 return result
182 186
183 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): 187 def insert_localized_strings(self, text, escapes, to_html=lambda s: s):
184 def lookup_string(match): 188 def lookup_string(match):
185 name, comment, default = match.groups() 189 name, comment, default = match.groups()
186 default = to_html(default).strip() 190 default = to_html(default).strip()
187 191
188 # Note: We currently ignore the comment, it is only relevant when 192 return self.localize_string(self._params["page"], name, default,
189 # generating the master translation. 193 comment, self._params["localedata"], escapes)
190 return self.localize_string(name, default, self._params["localedata"], esc apes)
191 194
192 return re.sub( 195 return re.sub(
193 r"{{\s*" 196 r"{{\s*"
194 r"([\w\-]+)" # String ID 197 r"([\w\-]+)" # String ID
195 r"(?:\[(.*?)\])?" # Optional comment 198 r"(?:\[(.*?)\])?" # Optional comment
196 r"\s+" 199 r"\s+"
197 r"((?:(?!{{).|" # Translatable text 200 r"((?:(?!{{).|" # Translatable text
198 r"{{(?:(?!}}).)*}}" # Nested translation 201 r"{{(?:(?!}}).)*}}" # Nested translation
199 r")*?)" 202 r")*?)"
200 r"}}", 203 r"}}",
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 module = template.make_module(self._params) 348 module = template.make_module(self._params)
346 for key, value in module.__dict__.iteritems(): 349 for key, value in module.__dict__.iteritems():
347 if not key.startswith("_"): 350 if not key.startswith("_"):
348 self._params[key] = value 351 self._params[key] = value
349 352
350 result = unicode(module) 353 result = unicode(module)
351 result = self.process_links(result) 354 result = self.process_links(result)
352 return result 355 return result
353 356
354 def translate(self, default, name, comment=None): 357 def translate(self, default, name, comment=None):
355 # Note: We currently ignore the comment, it is only relevant when 358 return jinja2.Markup(self.localize_string(
356 # generating the master translation. 359 self._params["page"], name, default, comment,
357 localedata = self._params["localedata"] 360 self._params["localedata"], html_escapes
358 return jinja2.Markup(self.localize_string(name, default, localedata, html_es capes)) 361 ))
359 362
360 def get_string(self, name, page): 363 def get_string(self, name, page):
361 localedata = self._params["source"].read_locale(self._params["locale"], page ) 364 localedata = self._params["source"].read_locale(self._params["locale"], page )
362 default = localedata[name] 365 default = localedata[name]
363 return jinja2.Markup(self.localize_string(name, default, localedata, html_es capes)) 366 return jinja2.Markup(self.localize_string(
367 page, name, default, "", localedata, html_escapes
368 ))
364 369
365 def get_page_content(self, page, locale=None): 370 def get_page_content(self, page, locale=None):
366 from cms.utils import get_page_params 371 from cms.utils import get_page_params
367 372
368 if locale is None: 373 if locale is None:
369 locale = self._params["locale"] 374 locale = self._params["locale"]
370 return get_page_params(self._params["source"], locale, page) 375 return get_page_params(self._params["source"], locale, page)
371 376
372 def linkify(self, page, locale=None, **attrs): 377 def linkify(self, page, locale=None, **attrs):
373 if locale is None: 378 if locale is None:
(...skipping 24 matching lines...) Expand all
398 stack.pop() 403 stack.pop()
399 stack[-1]["subitems"].append(item) 404 stack[-1]["subitems"].append(item)
400 stack.append(item) 405 stack.append(item)
401 return structured 406 return structured
402 407
403 converters = { 408 converters = {
404 "html": RawConverter, 409 "html": RawConverter,
405 "md": MarkdownConverter, 410 "md": MarkdownConverter,
406 "tmpl": TemplateConverter, 411 "tmpl": TemplateConverter,
407 } 412 }
OLDNEW

Powered by Google App Engine
This is Rietveld