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

Delta Between Two Patch Sets: cms/converters.py

Issue 29324503: Issue 2936 - Support syntax for duplicate translatable strings (Closed)
Left Patch Set: Remove misleading variable Created Aug. 23, 2015, 3:28 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | 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 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
136 locale = self._params["locale"]
137 is_default_locale = locale == self._params["defaultlocale"]
138
139 # Handle duplicated strings 137 # Handle duplicated strings
140 if not default: 138 if default:
139 self._seen_defaults[(page, name)] = (default, comment)
140 else:
141 try: 141 try:
142 default = self._params["localedata"][name] 142 default, comment = self._seen_defaults[(page, name)]
Wladimir Palant 2015/08/24 11:07:17 This logic is wrong, the default value should neve
kzar 2015/08/24 14:29:45 Acknowledged.
143 except KeyError: 143 except KeyError:
144 raise Exception("Text not yet defined for string %s on page %s" % 144 raise Exception("Text not yet defined for string %s on page %s" %
145 (name, self._params["page"])) 145 (name, page))
146 elif is_default_locale:
147 self._params["localedata"][name] = default
Wladimir Palant 2015/08/24 11:07:17 1) This should be using localedata parameter inste
kzar 2015/08/24 14:29:46 Done.
148 146
149 # Extract tag attributes from default string 147 # Extract tag attributes from default string
150 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"])
151 149
152 # Get translation 150 # Get translation
153 if is_default_locale: 151 locale = self._params["locale"]
152 if locale == self._params["defaultlocale"]:
154 result = default 153 result = default
155 elif name in localedata: 154 elif name in localedata:
156 result = localedata[name].strip() 155 result = localedata[name].strip()
157 else: 156 else:
158 result = default 157 result = default
159 self.missing_translations += 1 158 self.missing_translations += 1
160 self.total_translations += 1 159 self.total_translations += 1
161 160
162 if default: 161 # Perform callback with the string if required, e.g. for the translations sc ript
Wladimir Palant 2015/08/24 11:07:17 Why add this check? The string is still there, reg
kzar 2015/08/24 14:29:45 Well as a consumer of this callback I would be sur
Wladimir Palant 2015/08/24 15:00:21 That's something one has to count on anyway.
163 # Perform callback with the string if required, 162 callback = self._params["localized_string_callback"]
164 # e.g. for the translations script 163 if callback:
165 callback = self._params["localized_string_callback"] 164 callback(page, locale, name, result, comment, fixed_strings)
166 if callback: 165
167 callback(page, locale, name, result, comment, fixed_strings)
168 166
169 # Insert fixed strings 167 # Insert fixed strings
170 for i, fixed_string in enumerate(fixed_strings, 1): 168 for i, fixed_string in enumerate(fixed_strings, 1):
171 result = result.replace("{%d}" % i, fixed_string) 169 result = result.replace("{%d}" % i, fixed_string)
172 170
173 # Insert attributes 171 # Insert attributes
174 result = escape(result) 172 result = escape(result)
175 def stringify_attribute((name, value)): 173 def stringify_attribute((name, value)):
176 return '%s="%s"' % ( 174 return '%s="%s"' % (
177 escape(name), 175 escape(name),
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 stack.pop() 417 stack.pop()
420 stack[-1]["subitems"].append(item) 418 stack[-1]["subitems"].append(item)
421 stack.append(item) 419 stack.append(item)
422 return structured 420 return structured
423 421
424 converters = { 422 converters = {
425 "html": RawConverter, 423 "html": RawConverter,
426 "md": MarkdownConverter, 424 "md": MarkdownConverter,
427 "tmpl": TemplateConverter, 425 "tmpl": TemplateConverter,
428 } 426 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld