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 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 """ Returns the sublanguage ID for the given |language|. """ | 535 """ Returns the sublanguage ID for the given |language|. """ |
536 lang = _LANGUAGE_MAP[language] | 536 lang = _LANGUAGE_MAP[language] |
537 return _LANGUAGE_SUB[lang[2]] | 537 return _LANGUAGE_SUB[lang[2]] |
538 | 538 |
539 | 539 |
540 def IsRtlLanguage(language): | 540 def IsRtlLanguage(language): |
541 return language in _RTL_LANGUAGES; | 541 return language in _RTL_LANGUAGES; |
542 | 542 |
543 | 543 |
544 def NormalizeLanguageCode(language): | 544 def NormalizeLanguageCode(language): |
545 return language.replace('_', '-', 1) | 545 lang = language.replace('_', '-', 1) |
| 546 if lang == 'en-US': |
| 547 lang = 'en' |
| 548 return lang |
546 | 549 |
547 | 550 |
548 def GetDataPackageSuffix(language): | 551 def GetDataPackageSuffix(language): |
549 lang = NormalizeLanguageCode(language) | 552 lang = NormalizeLanguageCode(language) |
550 if lang == 'en': | 553 if lang == 'en': |
551 lang = 'en-US' | 554 lang = 'en-US' |
552 return lang | 555 return lang |
553 | 556 |
554 | 557 |
555 def GetJsonSuffix(language): | 558 def GetJsonSuffix(language): |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
632 | 635 |
633 def MakeSelectLanguage(self): | 636 def MakeSelectLanguage(self): |
634 """ Returns a function that can be used to select the current language. """ | 637 """ Returns a function that can be used to select the current language. """ |
635 return lambda language: self.SelectLanguage(language) | 638 return lambda language: self.SelectLanguage(language) |
636 | 639 |
637 def MakeGetText(self): | 640 def MakeGetText(self): |
638 """ Returns a function that can be used to retrieve a localized message. """ | 641 """ Returns a function that can be used to retrieve a localized message. """ |
639 return lambda message: self.GetText(message) | 642 return lambda message: self.GetText(message) |
640 | 643 |
641 | 644 |
| 645 # Use '@' as a delimiter for string templates instead of '$' to avoid unintended |
| 646 # expansion when passing the string from GYP. |
| 647 class GypTemplate(Template): |
| 648 delimiter = '@' |
| 649 |
| 650 |
642 def Localize(source, locales, options): | 651 def Localize(source, locales, options): |
643 # Set the list of languages to use. | 652 # Set the list of languages to use. |
644 languages = map(NormalizeLanguageCode, locales) | 653 languages = map(NormalizeLanguageCode, locales) |
645 context = { 'languages' : languages } | 654 context = { 'languages' : languages } |
646 | 655 |
647 # Load the localized messages. | 656 # Load the localized messages. |
648 message_map = MessageMap(languages, options.locale_dir) | 657 message_map = MessageMap(languages, options.locale_dir) |
649 | 658 |
650 # Add OFFICIAL_BUILD variable the same way chrome/tools/build/version.py | 659 # Add OFFICIAL_BUILD variable the same way chrome/tools/build/version.py |
651 # does. | 660 # does. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 env.globals['IsRtlLanguage'] = IsRtlLanguage | 703 env.globals['IsRtlLanguage'] = IsRtlLanguage |
695 env.globals['SelectLanguage'] = message_map.MakeSelectLanguage() | 704 env.globals['SelectLanguage'] = message_map.MakeSelectLanguage() |
696 env.install_gettext_callables(message_map.MakeGetText(), | 705 env.install_gettext_callables(message_map.MakeGetText(), |
697 message_map.MakeGetText()); | 706 message_map.MakeGetText()); |
698 | 707 |
699 template = env.get_template(template_name) | 708 template = env.get_template(template_name) |
700 | 709 |
701 # Generate a separate file per each locale if requested. | 710 # Generate a separate file per each locale if requested. |
702 outputs = [] | 711 outputs = [] |
703 if options.locale_output: | 712 if options.locale_output: |
704 target = Template(options.locale_output) | 713 target = GypTemplate(options.locale_output) |
705 for lang in languages: | 714 for lang in languages: |
706 context['languages'] = [ lang ] | 715 context['languages'] = [ lang ] |
707 context['language'] = lang | 716 context['language'] = lang |
708 context['pak_suffix'] = GetDataPackageSuffix(lang) | 717 context['pak_suffix'] = GetDataPackageSuffix(lang) |
709 context['json_suffix'] = GetJsonSuffix(lang) | 718 context['json_suffix'] = GetJsonSuffix(lang) |
| 719 message_map.SelectLanguage(lang) |
710 | 720 |
711 template_file_name = target.safe_substitute(context) | 721 template_file_name = target.safe_substitute(context) |
712 outputs.append(template_file_name) | 722 outputs.append(template_file_name) |
713 if not options.print_only: | 723 if not options.print_only: |
714 WriteIfChanged(template_file_name, template.render(context), | 724 WriteIfChanged(template_file_name, template.render(context), |
715 options.encoding) | 725 options.encoding) |
716 else: | 726 else: |
717 outputs.append(options.output) | 727 outputs.append(options.output) |
718 if not options.print_only: | 728 if not options.print_only: |
719 WriteIfChanged(options.output, template.render(context), options.encoding) | 729 WriteIfChanged(options.output, template.render(context), options.encoding) |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
764 parser.error( | 774 parser.error( |
765 'Either --output or --locale_output must be specified but not both') | 775 'Either --output or --locale_output must be specified but not both') |
766 if not options.template and not options.print_only: | 776 if not options.template and not options.print_only: |
767 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') |
768 | 778 |
769 return Localize(options.template, locales, options) | 779 return Localize(options.template, locales, options) |
770 | 780 |
771 if __name__ == '__main__': | 781 if __name__ == '__main__': |
772 sys.exit(DoMain(sys.argv[1:])) | 782 sys.exit(DoMain(sys.argv[1:])) |
773 | 783 |
OLD | NEW |