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: Created June 15, 2015, 2:12 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, 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
151 if locale == self._params["defaultlocale"]:
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
153 # Append a note about fixed strings to the string comment for translators
kzar 2015/06/15 14:24:28 Without this translators would have way to know wh
154 if fixed_strings:
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
150 # Insert fixed strings 161 # Insert fixed strings
151 for i, fixed_string in enumerate(fixed_strings, 1): 162 for i, fixed_string in enumerate(fixed_strings, 1):
152 result = result.replace("{%d}" % i, fixed_string) 163 result = result.replace("{%d}" % i, fixed_string)
153 164
154 # Insert attributes 165 # Insert attributes
155 result = escape(result) 166 result = escape(result)
156 def stringify_attribute((name, value)): 167 def stringify_attribute((name, value)):
157 return '%s="%s"' % ( 168 return '%s="%s"' % (
158 escape(name), 169 escape(name),
159 escape(self.insert_localized_strings(value, {})) 170 escape(self.insert_localized_strings(value, {}))
(...skipping 18 matching lines...) Expand all
178 r"<%s>\1</%s>" % (tag, tag), 189 r"<%s>\1</%s>" % (tag, tag),
179 result, flags=re.S 190 result, flags=re.S
180 ) 191 )
181 return result 192 return result
182 193
183 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): 194 def insert_localized_strings(self, text, escapes, to_html=lambda s: s):
184 def lookup_string(match): 195 def lookup_string(match):
185 name, comment, default = match.groups() 196 name, comment, default = match.groups()
186 default = to_html(default).strip() 197 default = to_html(default).strip()
187 198
188 # Note: We currently ignore the comment, it is only relevant when 199 # Note: The comment, it is only relevant when uploading translations
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.
189 # generating the master translation. 200 return self.localize_string(name, default, comment, self._params["localeda ta"], escapes)
190 return self.localize_string(name, default, self._params["localedata"], esc apes)
191 201
192 return re.sub( 202 return re.sub(
193 r"{{\s*" 203 r"{{\s*"
194 r"([\w\-]+)" # String ID 204 r"([\w\-]+)" # String ID
195 r"(?:\[(.*?)\])?" # Optional comment 205 r"(?:\[(.*?)\])?" # Optional comment
196 r"\s+" 206 r"\s+"
197 r"((?:(?!{{).|" # Translatable text 207 r"((?:(?!{{).|" # Translatable text
198 r"{{(?:(?!}}).)*}}" # Nested translation 208 r"{{(?:(?!}}).)*}}" # Nested translation
199 r")*?)" 209 r")*?)"
200 r"}}", 210 r"}}",
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 module = template.make_module(self._params) 355 module = template.make_module(self._params)
346 for key, value in module.__dict__.iteritems(): 356 for key, value in module.__dict__.iteritems():
347 if not key.startswith("_"): 357 if not key.startswith("_"):
348 self._params[key] = value 358 self._params[key] = value
349 359
350 result = unicode(module) 360 result = unicode(module)
351 result = self.process_links(result) 361 result = self.process_links(result)
352 return result 362 return result
353 363
354 def translate(self, default, name, comment=None): 364 def translate(self, default, name, comment=None):
355 # Note: We currently ignore the comment, it is only relevant when 365 # Note: The comment, it is only relevant when uploading translations
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.
356 # generating the master translation.
357 localedata = self._params["localedata"] 366 localedata = self._params["localedata"]
358 return jinja2.Markup(self.localize_string(name, default, localedata, html_es capes)) 367 return jinja2.Markup(self.localize_string(name, default, comment, localedata , html_escapes))
359 368
360 def get_string(self, name, page): 369 def get_string(self, name, page):
361 localedata = self._params["source"].read_locale(self._params["locale"], page ) 370 default = self._params["source"].read_locale(self._params["locale"], page)[n ame]
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.
362 default = localedata[name] 371 localedata = self._params["localedata"]
363 return jinja2.Markup(self.localize_string(name, default, localedata, html_es capes)) 372 return jinja2.Markup(self.localize_string(name, default, "", localedata, htm l_escapes))
364 373
365 def get_page_content(self, page, locale=None): 374 def get_page_content(self, page, locale=None):
366 from cms.utils import get_page_params 375 from cms.utils import get_page_params
367 376
368 if locale is None: 377 if locale is None:
369 locale = self._params["locale"] 378 locale = self._params["locale"]
370 return get_page_params(self._params["source"], locale, page) 379 return get_page_params(self._params["source"], locale, page)
371 380
372 def linkify(self, page, locale=None, **attrs): 381 def linkify(self, page, locale=None, **attrs):
373 if locale is None: 382 if locale is None:
(...skipping 24 matching lines...) Expand all
398 stack.pop() 407 stack.pop()
399 stack[-1]["subitems"].append(item) 408 stack[-1]["subitems"].append(item)
400 stack.append(item) 409 stack.append(item)
401 return structured 410 return structured
402 411
403 converters = { 412 converters = {
404 "html": RawConverter, 413 "html": RawConverter,
405 "md": MarkdownConverter, 414 "md": MarkdownConverter,
406 "tmpl": TemplateConverter, 415 "tmpl": TemplateConverter,
407 } 416 }
OLDNEW

Powered by Google App Engine
This is Rietveld