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: Created Aug. 21, 2015, 9:47 a.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 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 137 # Handle duplicated strings
141 if duplicate_string: 138 if default:
139 self._seen_defaults[(page, name)] = (default, comment)
140 else:
142 try: 141 try:
143 default = self._params["localedata"][name] 142 default, comment = self._seen_defaults[(page, name)]
144 except KeyError: 143 except KeyError:
145 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" %
146 (name, self._params["page"])) 145 (name, page))
147 elif is_default_locale:
148 self._params["localedata"][name] = default
149 146
150 # Extract tag attributes from default string 147 # Extract tag attributes from default string
151 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"])
152 149
153 # Get translation 150 # Get translation
154 if is_default_locale: 151 locale = self._params["locale"]
152 if locale == self._params["defaultlocale"]:
155 result = default 153 result = default
156 elif name in localedata: 154 elif name in localedata:
157 result = localedata[name].strip() 155 result = localedata[name].strip()
158 else: 156 else:
159 result = default 157 result = default
160 self.missing_translations += 1 158 self.missing_translations += 1
161 self.total_translations += 1 159 self.total_translations += 1
162 160
163 if not duplicate_string: 161 # Perform callback with the string if required, e.g. for the translations sc ript
164 # Perform callback with the string if required, 162 callback = self._params["localized_string_callback"]
165 # e.g. for the translations script 163 if callback:
166 callback = self._params["localized_string_callback"] 164 callback(page, locale, name, result, comment, fixed_strings)
167 if callback: 165
168 callback(page, locale, name, result, comment, fixed_strings)
169 166
170 # Insert fixed strings 167 # Insert fixed strings
171 for i, fixed_string in enumerate(fixed_strings, 1): 168 for i, fixed_string in enumerate(fixed_strings, 1):
172 result = result.replace("{%d}" % i, fixed_string) 169 result = result.replace("{%d}" % i, fixed_string)
173 170
174 # Insert attributes 171 # Insert attributes
175 result = escape(result) 172 result = escape(result)
176 def stringify_attribute((name, value)): 173 def stringify_attribute((name, value)):
177 return '%s="%s"' % ( 174 return '%s="%s"' % (
178 escape(name), 175 escape(name),
(...skipping 17 matching lines...) Expand all
196 result = re.sub( 193 result = re.sub(
197 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), 194 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)),
198 r"<%s>\1</%s>" % (tag, tag), 195 r"<%s>\1</%s>" % (tag, tag),
199 result, flags=re.S 196 result, flags=re.S
200 ) 197 )
201 return result 198 return result
202 199
203 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): 200 def insert_localized_strings(self, text, escapes, to_html=lambda s: s):
204 def lookup_string(match): 201 def lookup_string(match):
205 name, comment, default = match.groups() 202 name, comment, default = match.groups()
206 if default: 203 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
207 default = to_html(default).strip() 204 default = to_html(default).strip()
208 return self.localize_string(self._params["page"], name, default, 205 return self.localize_string(self._params["page"], name, default,
209 comment, self._params["localedata"], escapes) 206 comment, self._params["localedata"], escapes)
210 207
211 return re.sub( 208 return re.sub(
212 r"{{\s*" 209 r"{{\s*"
213 r"([\w\-]+)" # String ID 210 r"([\w\-]+)" # String ID
214 r"(?:(?:\[(.*?)\])?" # Optional comment 211 r"(?:(?:\[(.*?)\])?" # Optional comment
215 r"\s+" 212 r"\s+"
216 r"((?:(?!{{).|" # Translatable text 213 r"((?:(?!{{).|" # Translatable text
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 stack.pop() 417 stack.pop()
421 stack[-1]["subitems"].append(item) 418 stack[-1]["subitems"].append(item)
422 stack.append(item) 419 stack.append(item)
423 return structured 420 return structured
424 421
425 converters = { 422 converters = {
426 "html": RawConverter, 423 "html": RawConverter,
427 "md": MarkdownConverter, 424 "md": MarkdownConverter,
428 "tmpl": TemplateConverter, 425 "tmpl": TemplateConverter,
429 } 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