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 572 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 messages_file = io.open(file_name, encoding='utf-8-sig') | 583 messages_file = io.open(file_name, encoding='utf-8-sig') |
584 messages = json.load(messages_file) | 584 messages = json.load(messages_file) |
585 messages_file.close() | 585 messages_file.close() |
586 | 586 |
587 values = {} | 587 values = {} |
588 for key in messages.keys(): | 588 for key in messages.keys(): |
589 values[key] = unicode(messages[key]['message']); | 589 values[key] = unicode(messages[key]['message']); |
590 return values | 590 return values |
591 | 591 |
592 | 592 |
593 def WriteIfChanged(file_name, contents, encoding='utf-16'): | 593 def WriteIfChanged(file_name, contents, encoding): |
594 """ | 594 """ |
595 Writes the specified contents to the specified file_name | 595 Writes the specified contents to the specified file_name |
596 iff the contents are different than the current contents. | 596 iff the contents are different than the current contents. |
597 """ | 597 """ |
598 try: | 598 try: |
599 target = io.open(file_name, 'r') | 599 target = io.open(file_name, 'r') |
600 old_contents = target.read() | 600 old_contents = target.read() |
601 except EnvironmentError: | 601 except EnvironmentError: |
602 pass | 602 pass |
603 except UnicodeDecodeError: | 603 except UnicodeDecodeError: |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
736 return | 736 return |
737 | 737 |
738 | 738 |
739 def DoMain(argv): | 739 def DoMain(argv): |
740 usage = "Usage: localize [options] locales" | 740 usage = "Usage: localize [options] locales" |
741 parser = OptionParser(usage=usage) | 741 parser = OptionParser(usage=usage) |
742 parser.add_option( | 742 parser.add_option( |
743 '-d', '--define', dest='define', action='append', type='string', | 743 '-d', '--define', dest='define', action='append', type='string', |
744 help='define a variable (NAME=VALUE).') | 744 help='define a variable (NAME=VALUE).') |
745 parser.add_option( | 745 parser.add_option( |
746 '--encoding', dest='encoding', type='string', default='utf-16', | 746 '--encoding', dest='encoding', type='string', default='utf-8', |
747 help="set the encoding of <output>. 'utf-16' is the default.") | 747 help="set the encoding of <output>. 'utf-8' is the default.") |
748 parser.add_option( | 748 parser.add_option( |
749 '--jinja2', dest='jinja2', type='string', | 749 '--jinja2', dest='jinja2', type='string', |
750 help="specifies path to the jinja2 library.") | 750 help="specifies path to the jinja2 library.") |
751 parser.add_option( | 751 parser.add_option( |
752 '--locale_dir', dest='locale_dir', type='string', | 752 '--locale_dir', dest='locale_dir', type='string', |
753 help="set path to localized message files.") | 753 help="set path to localized message files.") |
754 parser.add_option( | 754 parser.add_option( |
755 '--locale_output', dest='locale_output', type='string', | 755 '--locale_output', dest='locale_output', type='string', |
756 help='specify the per-locale output file name.') | 756 help='specify the per-locale output file name.') |
757 parser.add_option( | 757 parser.add_option( |
(...skipping 16 matching lines...) Expand all Loading... |
774 parser.error( | 774 parser.error( |
775 'Either --output or --locale_output must be specified but not both') | 775 'Either --output or --locale_output must be specified but not both') |
776 if not options.template and not options.print_only: | 776 if not options.template and not options.print_only: |
777 parser.error('The template name is required unless --print_only is used') | 777 parser.error('The template name is required unless --print_only is used') |
778 | 778 |
779 return Localize(options.template, locales, options) | 779 return Localize(options.template, locales, options) |
780 | 780 |
781 if __name__ == '__main__': | 781 if __name__ == '__main__': |
782 sys.exit(DoMain(sys.argv[1:])) | 782 sys.exit(DoMain(sys.argv[1:])) |
783 | 783 |
OLD | NEW |