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