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

Side by Side Diff: cms/converters.py

Issue 29324503: Issue 2936 - Support syntax for duplicate translatable strings (Closed)
Patch Set: Re-do logic for storing "seen strings" Created Aug. 24, 2015, 2:28 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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
137 # Handle duplicated strings
138 if default:
139 self._seen_defaults[(page, name)] = default
140 else:
141 try:
142 default = self._seen_defaults[(page, name)]
143 except KeyError:
144 raise Exception("Text not yet defined for string %s on page %s" %
145 (name, page))
146
136 # Extract tag attributes from default string 147 # Extract tag attributes from default string
137 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"])
138 149
139 # Get translation 150 # Get translation
140 locale = self._params["locale"] 151 locale = self._params["locale"]
141 if locale == self._params["defaultlocale"]: 152 if locale == self._params["defaultlocale"]:
142 result = default 153 result = default
143 elif name in localedata: 154 elif name in localedata:
144 result = localedata[name].strip() 155 result = localedata[name].strip()
145 else: 156 else:
146 result = default 157 result = default
147 self.missing_translations += 1 158 self.missing_translations += 1
148 self.total_translations += 1 159 self.total_translations += 1
149 160
150 # Perform callback with the string if required, e.g. for the translations sc ript 161 if default:
151 callback = self._params["localized_string_callback"] 162 # Perform callback with the string if required,
152 if callback: 163 # e.g. for the translations script
153 callback(page, locale, name, result, comment, fixed_strings) 164 callback = self._params["localized_string_callback"]
154 165 if callback:
166 callback(page, locale, name, result, comment, fixed_strings)
155 167
156 # Insert fixed strings 168 # Insert fixed strings
157 for i, fixed_string in enumerate(fixed_strings, 1): 169 for i, fixed_string in enumerate(fixed_strings, 1):
158 result = result.replace("{%d}" % i, fixed_string) 170 result = result.replace("{%d}" % i, fixed_string)
159 171
160 # Insert attributes 172 # Insert attributes
161 result = escape(result) 173 result = escape(result)
162 def stringify_attribute((name, value)): 174 def stringify_attribute((name, value)):
163 return '%s="%s"' % ( 175 return '%s="%s"' % (
164 escape(name), 176 escape(name),
(...skipping 17 matching lines...) Expand all
182 result = re.sub( 194 result = re.sub(
183 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), 195 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)),
184 r"<%s>\1</%s>" % (tag, tag), 196 r"<%s>\1</%s>" % (tag, tag),
185 result, flags=re.S 197 result, flags=re.S
186 ) 198 )
187 return result 199 return result
188 200
189 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): 201 def insert_localized_strings(self, text, escapes, to_html=lambda s: s):
190 def lookup_string(match): 202 def lookup_string(match):
191 name, comment, default = match.groups() 203 name, comment, default = match.groups()
192 default = to_html(default).strip() 204 if default:
193 205 default = to_html(default).strip()
194 return self.localize_string(self._params["page"], name, default, 206 return self.localize_string(self._params["page"], name, default,
195 comment, self._params["localedata"], escapes) 207 comment, self._params["localedata"], escapes)
196 208
197 return re.sub( 209 return re.sub(
198 r"{{\s*" 210 r"{{\s*"
199 r"([\w\-]+)" # String ID 211 r"([\w\-]+)" # String ID
200 r"(?:\[(.*?)\])?" # Optional comment 212 r"(?:(?:\[(.*?)\])?" # Optional comment
201 r"\s+" 213 r"\s+"
202 r"((?:(?!{{).|" # Translatable text 214 r"((?:(?!{{).|" # Translatable text
203 r"{{(?:(?!}}).)*}}" # Nested translation 215 r"{{(?:(?!}}).)*}}" # Nested translation
204 r")*?)" 216 r")*?)"
217 r")?"
205 r"}}", 218 r"}}",
206 lookup_string, 219 lookup_string,
207 text, 220 text,
208 flags=re.S 221 flags=re.S
209 ) 222 )
210 223
211 def process_links(self, text): 224 def process_links(self, text):
212 def process_link(match): 225 def process_link(match):
213 pre, attr, url, post = match.groups() 226 pre, attr, url, post = match.groups()
214 url = jinja2.Markup(url).unescape() 227 url = jinja2.Markup(url).unescape()
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 stack.pop() 418 stack.pop()
406 stack[-1]["subitems"].append(item) 419 stack[-1]["subitems"].append(item)
407 stack.append(item) 420 stack.append(item)
408 return structured 421 return structured
409 422
410 converters = { 423 converters = {
411 "html": RawConverter, 424 "html": RawConverter,
412 "md": MarkdownConverter, 425 "md": MarkdownConverter,
413 "tmpl": TemplateConverter, 426 "tmpl": TemplateConverter,
414 } 427 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld