OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """ | 6 """ |
7 localize.py -- Generates an output file from the given template replacing | 7 localize.py -- Generates an output file from the given template replacing |
8 variables and localizing strings. | 8 variables and localizing strings. |
9 | 9 |
10 The script uses Jinja2 template processing library (src/third_party/jinja2). | 10 The script uses Jinja2 template processing library (src/third_party/jinja2). |
(...skipping 702 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
713 target = GypTemplate(options.locale_output) | 713 target = GypTemplate(options.locale_output) |
714 for lang in languages: | 714 for lang in languages: |
715 context['languages'] = [ lang ] | 715 context['languages'] = [ lang ] |
716 context['language'] = lang | 716 context['language'] = lang |
717 context['pak_suffix'] = GetDataPackageSuffix(lang) | 717 context['pak_suffix'] = GetDataPackageSuffix(lang) |
718 context['json_suffix'] = GetJsonSuffix(lang) | 718 context['json_suffix'] = GetJsonSuffix(lang) |
719 message_map.SelectLanguage(lang) | 719 message_map.SelectLanguage(lang) |
720 | 720 |
721 template_file_name = target.safe_substitute(context) | 721 template_file_name = target.safe_substitute(context) |
722 outputs.append(template_file_name) | 722 outputs.append(template_file_name) |
723 if not options.print_only: | 723 if not options.print_only and not options.print_to_filelist: |
724 WriteIfChanged(template_file_name, template.render(context), | 724 WriteIfChanged(template_file_name, template.render(context), |
725 options.encoding) | 725 options.encoding) |
726 else: | 726 else: |
727 outputs.append(options.output) | 727 outputs.append(options.output) |
728 if not options.print_only: | 728 if not options.print_only: |
729 WriteIfChanged(options.output, template.render(context), options.encoding) | 729 WriteIfChanged(options.output, template.render(context), options.encoding) |
730 | 730 |
731 if options.print_only: | 731 if options.print_only: |
732 # Quote each element so filename spaces don't mess up gyp's attempt to parse | 732 # Quote each element so filename spaces don't mess up gyp's attempt to parse |
733 # it into a list. | 733 # it into a list. |
734 return " ".join(['"%s"' % x for x in outputs]) | 734 return " ".join(['"%s"' % x for x in outputs]) |
735 | 735 |
736 if options.print_to_filelist: | |
737 # Strip off the quotes from each filename when writing into a listfile. | |
738 content = u'\n'.join([x.strip('"') for x in outputs]) | |
739 WriteIfChanged(options.print_to_filelist, content, options.encoding) | |
740 | |
736 return | 741 return |
737 | 742 |
738 | 743 |
739 def DoMain(argv): | 744 def DoMain(argv): |
740 usage = "Usage: localize [options] locales" | 745 usage = "Usage: localize [options] locales" |
741 parser = OptionParser(usage=usage) | 746 parser = OptionParser(usage=usage) |
742 parser.add_option( | 747 parser.add_option( |
743 '-d', '--define', dest='define', action='append', type='string', | 748 '-d', '--define', dest='define', action='append', type='string', |
744 help='define a variable (NAME=VALUE).') | 749 help='define a variable (NAME=VALUE).') |
745 parser.add_option( | 750 parser.add_option( |
746 '--encoding', dest='encoding', type='string', default='utf-8', | 751 '--encoding', dest='encoding', type='string', default='utf-8', |
747 help="set the encoding of <output>. 'utf-8' is the default.") | 752 help="set the encoding of <output>. 'utf-8' is the default.") |
748 parser.add_option( | 753 parser.add_option( |
749 '--jinja2', dest='jinja2', type='string', | 754 '--jinja2', dest='jinja2', type='string', |
750 help="specifies path to the jinja2 library.") | 755 help="specifies path to the jinja2 library.") |
751 parser.add_option( | 756 parser.add_option( |
752 '--locale_dir', dest='locale_dir', type='string', | 757 '--locale_dir', dest='locale_dir', type='string', |
753 help="set path to localized message files.") | 758 help="set path to localized message files.") |
754 parser.add_option( | 759 parser.add_option( |
755 '--locale_output', dest='locale_output', type='string', | 760 '--locale_output', dest='locale_output', type='string', |
756 help='specify the per-locale output file name.') | 761 help='specify the per-locale output file name.') |
757 parser.add_option( | 762 parser.add_option( |
758 '-o', '--output', dest='output', type='string', | 763 '-o', '--output', dest='output', type='string', |
759 help="specify the output file name.") | 764 help="specify the output file name.") |
760 parser.add_option( | 765 parser.add_option( |
761 '--print_only', dest='print_only', action='store_true', | 766 '--print_only', dest='print_only', action='store_true', |
762 default=False, help='print the output file names only.') | 767 default=False, help='print the output file names only.') |
763 parser.add_option( | 768 parser.add_option( |
769 '--print_to_filelist', dest='print_to_filelist', type='string', | |
Jamie
2015/02/05 19:26:20
print_to_filelist sounds like it should be a boole
garykac
2015/02/06 02:13:35
Done.
| |
770 help='print the output file names into a file.') | |
771 parser.add_option( | |
764 '-t', '--template', dest='template', type='string', | 772 '-t', '--template', dest='template', type='string', |
765 help="specify the template file name.") | 773 help="specify the template file name.") |
766 parser.add_option( | 774 parser.add_option( |
767 '--variables', dest='variables', action='append', type='string', | 775 '--variables', dest='variables', action='append', type='string', |
768 help='read variables (NAME=VALUE) from file.') | 776 help='read variables (NAME=VALUE) from file.') |
769 | 777 |
770 options, locales = parser.parse_args(argv) | 778 options, locales = parser.parse_args(argv) |
771 if not locales: | 779 if not locales: |
772 parser.error('At least one locale must be specified') | 780 parser.error('At least one locale must be specified') |
773 if bool(options.output) == bool(options.locale_output): | 781 if bool(options.output) == bool(options.locale_output): |
774 parser.error( | 782 parser.error( |
775 'Either --output or --locale_output must be specified but not both') | 783 'Either --output or --locale_output must be specified but not both') |
776 if not options.template and not options.print_only: | 784 if (not options.template and |
777 parser.error('The template name is required unless --print_only is used') | 785 not options.print_only and not options.print_to_filelist): |
786 parser.error('The template name is required unless either --print_only ' | |
787 'or --print_to_filelist is used') | |
778 | 788 |
779 return Localize(options.template, locales, options) | 789 return Localize(options.template, locales, options) |
780 | 790 |
781 if __name__ == '__main__': | 791 if __name__ == '__main__': |
782 sys.exit(DoMain(sys.argv[1:])) | 792 sys.exit(DoMain(sys.argv[1:])) |
783 | |
OLD | NEW |