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

Delta Between Two Patch Sets: cms/bin/translate.py

Issue 29328085: Issue 3076 - [CMS] Map locale names to match Crowdin's expectations (Closed)
Left Patch Set: Simplify locale name conversion logic Created Sept. 17, 2015, 10:15 a.m.
Right Patch Set: Addressed nits Created Sept. 17, 2015, 2:43 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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 def configure_locales(crowdin_api, local_locales, enabled_locales, 121 def configure_locales(crowdin_api, local_locales, enabled_locales,
122 defaultlocale): 122 defaultlocale):
123 logger.info("Checking which locales are supported by Crowdin...") 123 logger.info("Checking which locales are supported by Crowdin...")
124 response = crowdin_api.request("GET", "supported-languages") 124 response = crowdin_api.request("GET", "supported-languages")
125 125
126 supported_locales = {l["crowdin_code"] for l in response} 126 supported_locales = {l["crowdin_code"] for l in response}
127 127
128 # We need to map the locale names we use to the ones that Crowdin is expecting 128 # We need to map the locale names we use to the ones that Crowdin is expecting
129 # and at the same time ensure that they are supported. 129 # and at the same time ensure that they are supported.
130 required_locales = {} 130 required_locales = {}
131 skipped_locales = []
132 for locale in local_locales: 131 for locale in local_locales:
133 if "_" in locale: 132 if "_" in locale:
134 crowdin_locale = locale.replace("_", "-") 133 crowdin_locale = locale.replace("_", "-")
135 elif locale in supported_locales: 134 elif locale in supported_locales:
136 crowdin_locale = locale 135 crowdin_locale = locale
137 else: 136 else:
138 crowdin_locale = "%s-%s" % (locale, locale.upper()) 137 crowdin_locale = "%s-%s" % (locale, locale.upper())
139 138
140 if crowdin_locale in supported_locales: 139 if crowdin_locale in supported_locales:
141 required_locales[locale] = crowdin_locale 140 required_locales[locale] = crowdin_locale
142 else: 141 else:
143 skipped_locales.append(locale) 142 logger.warning("Ignoring locale '%s', which Crowdin doesn't support",
Sebastian Noack 2015/09/17 11:03:24 This code past hasn't been tested, obviously. ;)
Sebastian Noack 2015/09/17 12:42:15 Never mind. I just realized you turned it into a l
kzar 2015/09/17 14:43:55 Done.
144 143 locale)
145 if skipped_locales:
146 logger.warning("Ignoring locales that Crowdin doesn't support: %s",
147 ", ".join(skipped_locales))
148 144
149 required_crowdin_locales = set(required_locales.values()) 145 required_crowdin_locales = set(required_locales.values())
150 if not required_crowdin_locales.issubset(enabled_locales): 146 if not required_crowdin_locales.issubset(enabled_locales):
151 logger.info("Enabling the required locales for the Crowdin project...") 147 logger.info("Enabling the required locales for the Crowdin project...")
152 crowdin_api.request( 148 crowdin_api.request(
153 "POST", "edit-project", 149 "POST", "edit-project",
154 data={"languages": enabled_locales | required_crowdin_locales} 150 data={"languages": enabled_locales | required_crowdin_locales}
155 ) 151 )
156 152
157 return required_locales 153 return required_locales
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 for member in archive.namelist(): 255 for member in archive.namelist():
260 path, file_name = posixpath.split(member) 256 path, file_name = posixpath.split(member)
261 ext = posixpath.splitext(file_name)[1] 257 ext = posixpath.splitext(file_name)[1]
262 path_parts = path.split(posixpath.sep) 258 path_parts = path.split(posixpath.sep)
263 locale, file_path = path_parts[0], path_parts[1:] 259 locale, file_path = path_parts[0], path_parts[1:]
264 if ext.lower() == ".json" and locale in inverted_required_locales: 260 if ext.lower() == ".json" and locale in inverted_required_locales:
265 output_path = os.path.join( 261 output_path = os.path.join(
266 locale_path, inverted_required_locales[locale], 262 locale_path, inverted_required_locales[locale],
267 *file_path + [file_name] 263 *file_path + [file_name]
268 ) 264 )
269 with archive.open(member) as f, open(output_path, "wb") as f_target: 265 with archive.open(member) as source_file, \
Sebastian Noack 2015/09/17 11:03:24 Nit: Please use consistent and more descriptive va
kzar 2015/09/17 14:43:55 Done.
270 shutil.copyfileobj(f, f_target) 266 open(output_path, "wb") as target_file:
267 shutil.copyfileobj(source_file, target_file)
271 268
272 def crowdin_sync(source_dir, crowdin_api_key): 269 def crowdin_sync(source_dir, crowdin_api_key):
273 with FileSource(source_dir) as source: 270 with FileSource(source_dir) as source:
274 config = source.read_config() 271 config = source.read_config()
275 defaultlocale = config.get("general", "defaultlocale") 272 defaultlocale = config.get("general", "defaultlocale")
276 crowdin_project_name = config.get("general", "crowdin-project-name") 273 crowdin_project_name = config.get("general", "crowdin-project-name")
277 274
278 crowdin_api = CrowdinAPI(crowdin_api_key, crowdin_project_name) 275 crowdin_api = CrowdinAPI(crowdin_api_key, crowdin_project_name)
279 276
280 logger.info("Requesting project information...") 277 logger.info("Requesting project information...")
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 if __name__ == "__main__": 313 if __name__ == "__main__":
317 if len(sys.argv) < 3: 314 if len(sys.argv) < 3:
318 print >>sys.stderr, "Usage: python -m cms.bin.translate www_directory crowdi n_project_api_key [logging_level]" 315 print >>sys.stderr, "Usage: python -m cms.bin.translate www_directory crowdi n_project_api_key [logging_level]"
319 sys.exit(1) 316 sys.exit(1)
320 317
321 logging.basicConfig() 318 logging.basicConfig()
322 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO) 319 logger.setLevel(sys.argv[3] if len(sys.argv) > 3 else logging.INFO)
323 320
324 source_dir, crowdin_api_key = sys.argv[1:3] 321 source_dir, crowdin_api_key = sys.argv[1:3]
325 crowdin_sync(source_dir, crowdin_api_key) 322 crowdin_sync(source_dir, crowdin_api_key)
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