OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 3 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 """Creates a resources.zip for locale .pak files. |
| 7 |
| 8 Places the locale.pak files into appropriate resource configs |
| 9 (e.g. en-GB.pak -> res/raw-en/en_gb.pak). Also generates a locale_paks |
| 10 TypedArray so that resource files can be enumerated at runtime. |
| 11 """ |
| 12 |
| 13 import collections |
| 14 import optparse |
| 15 import os |
| 16 import sys |
| 17 import zipfile |
| 18 |
| 19 from util import build_utils |
| 20 |
| 21 |
| 22 # This should stay in sync with: |
| 23 # base/android/java/src/org/chromium/base/LocaleUtils.java |
| 24 _CHROME_TO_ANDROID_LOCALE_MAP = { |
| 25 'he': 'iw', |
| 26 'id': 'in', |
| 27 'fil': 'tl', |
| 28 } |
| 29 |
| 30 |
| 31 def CreateLocalePaksXml(names): |
| 32 """Creates the contents for the locale-paks.xml files.""" |
| 33 VALUES_FILE_TEMPLATE = '''<?xml version="1.0" encoding="utf-8"?> |
| 34 <resources> |
| 35 <array name="locale_paks">%s |
| 36 </array> |
| 37 </resources> |
| 38 ''' |
| 39 VALUES_ITEM_TEMPLATE = ''' |
| 40 <item>@raw/%s</item>''' |
| 41 |
| 42 res_names = (os.path.splitext(name)[0] for name in names) |
| 43 items = ''.join((VALUES_ITEM_TEMPLATE % name for name in res_names)) |
| 44 return VALUES_FILE_TEMPLATE % items |
| 45 |
| 46 |
| 47 def main(): |
| 48 parser = optparse.OptionParser() |
| 49 build_utils.AddDepfileOption(parser) |
| 50 parser.add_option('--locale-paks', help='List of files for res/raw-LOCALE') |
| 51 parser.add_option('--resources-zip', help='Path to output resources.zip') |
| 52 |
| 53 options, _ = parser.parse_args() |
| 54 build_utils.CheckOptions(options, parser, |
| 55 required=['locale_paks', 'resources_zip']) |
| 56 |
| 57 sources = build_utils.ParseGypList(options.locale_paks) |
| 58 |
| 59 if options.depfile: |
| 60 deps = sources + build_utils.GetPythonDependencies() |
| 61 build_utils.WriteDepfile(options.depfile, deps) |
| 62 |
| 63 with zipfile.ZipFile(options.resources_zip, 'w', zipfile.ZIP_STORED) as out: |
| 64 # e.g. "en" -> ["en_gb.pak"] |
| 65 lang_to_locale_map = collections.defaultdict(list) |
| 66 for src_path in sources: |
| 67 basename = os.path.basename(src_path) |
| 68 name = os.path.splitext(basename)[0] |
| 69 # Resources file names must consist of [a-z0-9_.]. |
| 70 res_compatible_name = basename.replace('-', '_').lower() |
| 71 if name == 'en-US': |
| 72 dest_dir = 'raw' |
| 73 else: |
| 74 # Chrome's uses different region mapping logic from Android, so include |
| 75 # all regions for each language. |
| 76 android_locale = _CHROME_TO_ANDROID_LOCALE_MAP.get(name, name) |
| 77 lang = android_locale[0:2] |
| 78 dest_dir = 'raw-' + lang |
| 79 lang_to_locale_map[lang].append(res_compatible_name) |
| 80 out.write(src_path, os.path.join(dest_dir, res_compatible_name)) |
| 81 |
| 82 # Create a String Arrays resource so ResourceExtractor can enumerate files. |
| 83 def WriteValuesFile(lang, names): |
| 84 dest_dir = 'values' |
| 85 if lang: |
| 86 dest_dir += '-' + lang |
| 87 # Always extract en-US.pak since it's the fallback. |
| 88 xml = CreateLocalePaksXml(names + ['en_us.pak']) |
| 89 out.writestr(os.path.join(dest_dir, 'locale-paks.xml'), xml) |
| 90 |
| 91 for lang, names in lang_to_locale_map.iteritems(): |
| 92 WriteValuesFile(lang, names) |
| 93 WriteValuesFile(None, []) |
| 94 |
| 95 |
| 96 if __name__ == '__main__': |
| 97 sys.exit(main()) |
OLD | NEW |