Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2017 The Chromium Authors. All rights reserved. | 1 # Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 # This script generates a header containing a dictionary from locales to | 5 # This script generates a header containing a dictionary from locales to |
| 6 # accept language strings from chromium's .xtb files. It is not very | 6 # accept language strings from chromium's .xtb files. It is not very |
| 7 # robust at the moment, and makes some assumptions about the format of | 7 # robust at the moment, and makes some assumptions about the format of |
| 8 # the files, including at least the following: | 8 # the files, including at least the following: |
| 9 # * assumes necessary data is contained only with files of the form | 9 # * assumes necessary data is contained only with files of the form |
| 10 # components/strings/components_locale_settings_${LANG}.xtb | 10 # components/strings/components_locale_settings_${LANG}.xtb |
| 11 # * assumes ${LANG} is identified in the lang attribute of the root | 11 # * assumes ${LANG} is identified in the lang attribute of the root |
| 12 # element of the file's xml data | 12 # element of the file's xml data |
| 13 # * assumes that there is only one relevant element with the | 13 # * assumes that there is only one relevant element with the |
| 14 # IDS_ACCEPT_LANGUAGES attribute | 14 # IDS_ACCEPT_LANGUAGES attribute |
| 15 | 15 |
| 16 import os, re, sys | 16 import os, re, sys |
| 17 from xml.etree import ElementTree | 17 from xml.etree import ElementTree |
| 18 | 18 |
| 19 STRINGS_DIR = sys.argv[2] + 'components/strings/' | 19 STRINGS_DIR = sys.argv[2] + 'components/strings/' |
| 20 | 20 |
| 21 def extract_accept_langs(filename): | 21 def extract_accept_langs(filename): |
| 22 tree = ElementTree.parse(STRINGS_DIR + filename).getroot() | 22 tree = ElementTree.parse(STRINGS_DIR + filename).getroot() |
| 23 for child in tree: | 23 for child in tree: |
| 24 if child.get('id') == 'IDS_ACCEPT_LANGUAGES': | 24 if child.get('id') == 'IDS_ACCEPT_LANGUAGES': |
| 25 return tree.get('lang'), child.text | 25 return tree.get('lang'), child.text |
| 26 | 26 |
| 27 def gen_accept_langs_table(): | 27 def gen_accept_langs_table(): |
| 28 return dict(filter(None, (extract_accept_langs(filename) | 28 return dict(accept_langs for accept_langs in (extract_accept_langs(filename) |
| 29 for filename in os.listdir(STRINGS_DIR) | 29 for filename in os.listdir(STRINGS_DIR) |
| 30 if re.match(r'components_locale_settings_\S+.xtb', filename)))) | 30 if re.match(r'components_locale_settings_\S+.xtb', filename)) |
| 31 if accept_langs) | |
|
xunjieli
2017/06/27 17:51:45
This is getting rather long. Can you separate it o
| |
| 31 | 32 |
| 32 HEADER = "NSDictionary* acceptLangs = @{" | 33 HEADER = "NSDictionary* acceptLangs = @{" |
| 33 def LINE(locale, accept_langs): | 34 def LINE(locale, accept_langs): |
| 34 return ' @"' + locale + '" : @"' + accept_langs + '",' | 35 return ' @"' + locale + '" : @"' + accept_langs + '",' |
| 35 FOOTER = "};" | 36 FOOTER = "};" |
| 36 | 37 |
| 37 def main(): | 38 def main(): |
| 38 with open(sys.argv[1] + "/accept_languages_table.h", "w+") as f: | 39 with open(sys.argv[1] + "/accept_languages_table.h", "w+") as f: |
| 39 print >>f, HEADER | 40 print >>f, HEADER |
| 40 for (locale, accept_langs) in gen_accept_langs_table().items(): | 41 for (locale, accept_langs) in gen_accept_langs_table().items(): |
| 41 print >>f, LINE(locale, accept_langs) | 42 print >>f, LINE(locale, accept_langs) |
| 42 print >>f, FOOTER | 43 print >>f, FOOTER |
| 43 | 44 |
| 44 if __name__ == "__main__": | 45 if __name__ == "__main__": |
| 45 main() | 46 main() |
| OLD | NEW |