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: Simplified local_locales assignment Created Sept. 16, 2015, 8:01 p.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("_", "-")
134 elif locale in supported_locales:
135 crowdin_locale = locale
135 else: 136 else:
136 if locale in supported_locales:
137 required_locales[locale] = locale
138 continue
139 crowdin_locale = "%s-%s" % (locale, locale.upper()) 137 crowdin_locale = "%s-%s" % (locale, locale.upper())
140 138
141 if crowdin_locale in supported_locales: 139 if crowdin_locale in supported_locales:
142 required_locales[locale] = crowdin_locale 140 required_locales[locale] = crowdin_locale
143 else: 141 else:
144 skipped_locales.append(locale) 142 logger.warning("Ignoring locale '%s', which Crowdin doesn't support",
145 143 locale)
146 if skipped_locales:
147 logger.warning("Ignoring locales that Crowdin doesn't support: %s",
148 ", ".join(skipped_locales))
149 144
150 required_crowdin_locales = set(required_locales.values()) 145 required_crowdin_locales = set(required_locales.values())
151 if not required_crowdin_locales.issubset(enabled_locales): 146 if not required_crowdin_locales.issubset(enabled_locales):
152 logger.info("Enabling the required locales for the Crowdin project...") 147 logger.info("Enabling the required locales for the Crowdin project...")
153 crowdin_api.request( 148 crowdin_api.request(
154 "POST", "edit-project", 149 "POST", "edit-project",
155 data={"languages": enabled_locales | required_crowdin_locales} 150 data={"languages": enabled_locales | required_crowdin_locales}
156 ) 151 )
157 152
158 return required_locales 153 return required_locales
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 for member in archive.namelist(): 255 for member in archive.namelist():
261 path, file_name = posixpath.split(member) 256 path, file_name = posixpath.split(member)
262 ext = posixpath.splitext(file_name)[1] 257 ext = posixpath.splitext(file_name)[1]
263 path_parts = path.split(posixpath.sep) 258 path_parts = path.split(posixpath.sep)
264 locale, file_path = path_parts[0], path_parts[1:] 259 locale, file_path = path_parts[0], path_parts[1:]
265 if ext.lower() == ".json" and locale in inverted_required_locales: 260 if ext.lower() == ".json" and locale in inverted_required_locales:
266 output_path = os.path.join( 261 output_path = os.path.join(
267 locale_path, inverted_required_locales[locale], 262 locale_path, inverted_required_locales[locale],
268 *file_path + [file_name] 263 *file_path + [file_name]
269 ) 264 )
270 with archive.open(member) as f, open(output_path, "wb") as f_target: 265 with archive.open(member) as source_file, \
271 shutil.copyfileobj(f, f_target) 266 open(output_path, "wb") as target_file:
267 shutil.copyfileobj(source_file, target_file)
272 268
273 def crowdin_sync(source_dir, crowdin_api_key): 269 def crowdin_sync(source_dir, crowdin_api_key):
274 with FileSource(source_dir) as source: 270 with FileSource(source_dir) as source:
275 config = source.read_config() 271 config = source.read_config()
276 defaultlocale = config.get("general", "defaultlocale") 272 defaultlocale = config.get("general", "defaultlocale")
277 crowdin_project_name = config.get("general", "crowdin-project-name") 273 crowdin_project_name = config.get("general", "crowdin-project-name")
278 274
279 crowdin_api = CrowdinAPI(crowdin_api_key, crowdin_project_name) 275 crowdin_api = CrowdinAPI(crowdin_api_key, crowdin_project_name)
280 276
281 logger.info("Requesting project information...") 277 logger.info("Requesting project information...")
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 if __name__ == "__main__": 313 if __name__ == "__main__":
318 if len(sys.argv) < 3: 314 if len(sys.argv) < 3:
319 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]"
320 sys.exit(1) 316 sys.exit(1)
321 317
322 logging.basicConfig() 318 logging.basicConfig()
323 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)
324 320
325 source_dir, crowdin_api_key = sys.argv[1:3] 321 source_dir, crowdin_api_key = sys.argv[1:3]
326 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