@@ -0,0 +1,3608 @@
+# Copyright 2017 Cisco Systems, Inc.%0A#%0A# Licensed under the Apache License, Version 2.0 (the %22License%22);%0A# you may not use this file except in compliance with the License.%0A# You may obtain a copy of the License at%0A#%0A#    http://www.apache.org/licenses/LICENSE-2.0%0A#%0A# Unless required by applicable law or agreed to in writing, software%0A# distributed under the License is distributed on an %22AS IS%22 BASIS,%0A# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A# See the License for the specific language governing permissions and%0A# limitations under the License.%0A%0Aimport os%0A%0Afrom django.core.management.base import BaseCommand%0Afrom django.utils import translation%0Aimport requests%0A%0AZANATA_LOCALES = requests.get(%22https://translate.openstack.org/rest/project%22%0A                              %22/horizon/version/master/locales%22).json()%0ALANGUAGE_CODES = %5Bx%5B'localeId'%5D for x in ZANATA_LOCALES%5D%0ADOMAINS = %5B'django', 'djangojs'%5D%0AMODULES = %5B'horizon', 'openstack_dashboard'%5D%0APROJECT = 'horizon'%0APOFILE = %22%7Bmodule%7D/locale/%7Blocale%7D/LC_MESSAGES/%7Bdomain%7D.po%22%0APOFILE_URL = (%22https://translate.openstack.org/rest/file/translation/%7Bproject%7D%22%0A              %22/master/%7Blanguage%7D/po?docId=%7Bmodule%7D%252Flocale%252F%7Bdomain%7D%22)%0A%0A%0Aclass Command(BaseCommand):%0A    help = (%22Pull a translation catalog from Zanata %22%0A            %22(https://translate.openstack.org) for all languages or a %22%0A            %22specified language%22)%0A%0A    def add_arguments(self, parser):%0A        parser.add_argument('-l', '--language', choices=LANGUAGE_CODES,%0A                            metavar='LANG',%0A                            default=LANGUAGE_CODES, nargs='+',%0A                            help=(%22The language code(s) to pull language %22%0A                                  %22catalogs for. The default is all %22%0A                                  %22languages. Available languages are: %25s%22%0A                                  %25 ', '.join(sorted(LANGUAGE_CODES))))%0A        parser.add_argument('-p', '--project', type=str, default=PROJECT,%0A                            help=(%22The name of the project to extract %22%0A                                  %22strings from e.g. 'horizon'. The default %22%0A                                  %22is 'horizon'%22))%0A        parser.add_argument('-m', '--module', type=str, nargs='+',%0A                            default=MODULES,%0A                            help=(%22The target python module(s) to extract %22%0A                                  %22strings from e.g. 'openstack_dashboard'. %22%0A                                  %22The default modules are 'horizon' and %22%0A                                  %22'openstack_dashboard'%22))%0A%0A    def handle(self, *args, **options):%0A        for module in options%5B'module'%5D:%0A            for domain in DOMAINS:%0A                for language in options%5B'language'%5D:%0A                    locale = translation.to_locale(language)%0A%0A                    pofile = POFILE.format(module=module,%0A                                           locale=locale,%0A                                           domain=domain)%0A%0A                    pofile_dir = os.path.dirname(pofile)%0A                    if not os.path.exists(pofile_dir):%0A                        os.makedirs(pofile_dir)%0A%0A                    new_po = requests.get((POFILE_URL).format(%0A                        language=language,%0A                        project=options%5B'project'%5D,%0A                        module=module,%0A                        domain=domain))%0A%0A                    # Ensure to use UTF-8 encoding%0A                    new_po.encoding = 'utf-8'%0A%0A                    with open(pofile, 'w+') as f:%0A                        f.write(new_po.text.encode('utf-8'))%0A

Add a new management cmd to pull translations