create_ui_locale_resources.py (2805B)
1 #!/usr/bin/env python3 2 # 3 # Copyright 2018 The Chromium Authors 4 # Use of this source code is governed by a BSD-style license that can be 5 # found in the LICENSE file. 6 7 """Generate a zip archive containing localized locale name Android resource 8 strings! 9 10 This script takes a list of input Chrome-specific locale names, as well as an 11 output zip file path. 12 13 Each output file will contain the definition of a single string resource, 14 named 'current_locale', whose value will be the matching Chromium locale name. 15 E.g. values-en-rUS/strings.xml will define 'current_locale' as 'en-US'. 16 """ 17 18 import argparse 19 import os 20 import sys 21 import zipfile 22 23 sys.path.insert( 24 0, 25 os.path.join( 26 os.path.dirname(__file__), '..', '..', '..', 'build', 'android', 'gyp')) 27 28 from util import build_utils 29 from util import resource_utils 30 import action_helpers # build_utils adds //build to sys.path. 31 import zip_helpers 32 33 34 # A small string template for the content of each strings.xml file. 35 # NOTE: The name is chosen to avoid any conflicts with other string defined 36 # by other resource archives. 37 _TEMPLATE = """\ 38 <?xml version="1.0" encoding="utf-8"?> 39 <resources> 40 <string name="current_detected_ui_locale_name">{resource_text}</string> 41 </resources> 42 """ 43 44 # The default Chrome locale value. 45 _DEFAULT_CHROME_LOCALE = 'en-US' 46 47 48 def _GenerateLocaleStringsXml(locale): 49 return _TEMPLATE.format(resource_text=locale) 50 51 52 def _AddLocaleResourceFileToZip(out_zip, android_locale, locale): 53 locale_data = _GenerateLocaleStringsXml(locale) 54 if android_locale: 55 zip_path = 'values-%s/strings.xml' % android_locale 56 else: 57 zip_path = 'values/strings.xml' 58 zip_helpers.add_to_zip_hermetic(out_zip, 59 zip_path, 60 data=locale_data, 61 compress=False) 62 63 64 def main(): 65 parser = argparse.ArgumentParser( 66 description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) 67 68 parser.add_argument( 69 '--locale-list', 70 required=True, 71 help='GN-list of Chrome-specific locale names.') 72 parser.add_argument( 73 '--output-zip', required=True, help='Output zip archive path.') 74 75 args = parser.parse_args() 76 77 locale_list = action_helpers.parse_gn_list(args.locale_list) 78 if not locale_list: 79 raise Exception('Locale list cannot be empty!') 80 81 with action_helpers.atomic_output(args.output_zip) as tmp_file: 82 with zipfile.ZipFile(tmp_file, 'w') as out_zip: 83 # First, write the default value, since aapt requires one. 84 _AddLocaleResourceFileToZip(out_zip, '', _DEFAULT_CHROME_LOCALE) 85 86 for locale in locale_list: 87 android_locale = resource_utils.ToAndroidLocaleName(locale) 88 _AddLocaleResourceFileToZip(out_zip, android_locale, locale) 89 90 91 if __name__ == '__main__': 92 main()