Ejemplo n.º 1
0
def main():
    logging.basicConfig(stream=sys.stdout, level=logging.INFO)
    create_dir_if_necessary(LOCALE_DIR)
    source_msgs_dir = CONFIGURATION.source_messages_dir
    remove_file(source_msgs_dir.joinpath('django.po'))

    # Extract strings from mako templates.
    babel_mako_cmd = 'pybabel extract -F %s -c "Translators:" . -o %s' % (BABEL_CONFIG, BABEL_OUT)
    execute(babel_mako_cmd, working_directory=BASE_DIR)

    makemessages = "django-admin.py makemessages -l en"
    ignores = " ".join('--ignore="{}/*"'.format(d) for d in CONFIGURATION.ignore_dirs)
    if ignores:
        makemessages += " " + ignores

    # Extract strings from django source files, including .py files.
    make_django_cmd = makemessages + ' --extension html'
    execute(make_django_cmd, working_directory=BASE_DIR)

    # Extract strings from Javascript source files.
    make_djangojs_cmd = makemessages + ' -d djangojs --extension js'
    execute(make_djangojs_cmd, working_directory=BASE_DIR)

    # makemessages creates 'django.po'. This filename is hardcoded.
    # Rename it to django-partial.po to enable merging into django.po later.
    os.rename(
        source_msgs_dir.joinpath('django.po'),
        source_msgs_dir.joinpath('django-partial.po')
    )

    # makemessages creates 'djangojs.po'. This filename is hardcoded.
    # Rename it to djangojs-partial.po to enable merging into djangojs.po later.
    os.rename(
        source_msgs_dir.joinpath('djangojs.po'),
        source_msgs_dir.joinpath('djangojs-partial.po')
    )

    # Segment the generated files.
    segmented_files = segment_pofiles("en")

    # Finish each file.
    for filename in segmented_files:
        LOG.info('Cleaning %s' % filename)
        po = pofile(source_msgs_dir.joinpath(filename))
        # replace default headers with edX headers
        fix_header(po)
        # replace default metadata with edX metadata
        fix_metadata(po)
        # remove key strings which belong in messages.po
        strip_key_strings(po)
        po.save()
Ejemplo n.º 2
0
def main():
    logging.basicConfig(stream=sys.stdout, level=logging.INFO)
    create_dir_if_necessary(LOCALE_DIR)
    source_msgs_dir = CONFIGURATION.source_messages_dir
    remove_file(source_msgs_dir.joinpath('django.po'))

    makemessages = "django-admin.py makemessages -l en"
    ignores = " ".join('--ignore="{}/*"'.format(d)
                       for d in CONFIGURATION.ignore_dirs)
    if ignores:
        makemessages += " " + ignores

    # Extract strings from mako templates.
    babel_mako_cmd = 'pybabel extract -F %s -c "Translators:" . -o %s' % (
        BABEL_CONFIG, BABEL_OUT)
    execute(babel_mako_cmd, working_directory=BASE_DIR)

    # Extract strings from django source files, including .py files.
    make_django_cmd = makemessages + ' --extension html'
    execute(make_django_cmd, working_directory=BASE_DIR)

    # Extract strings from Javascript source files.
    make_djangojs_cmd = makemessages + ' -d djangojs --extension js'
    execute(make_djangojs_cmd, working_directory=BASE_DIR)

    # makemessages creates 'django.po'. This filename is hardcoded.
    # Rename it to django-partial.po to enable merging into django.po later.
    os.rename(source_msgs_dir.joinpath('django.po'),
              source_msgs_dir.joinpath('django-partial.po'))

    # Segment the generated files.
    segmented_files = segment_pofiles("en")

    # Finish each file.
    for filename in segmented_files:
        LOG.info('Cleaning %s' % filename)
        po = pofile(source_msgs_dir.joinpath(filename))
        # replace default headers with edX headers
        fix_header(po)
        # replace default metadata with edX metadata
        fix_metadata(po)
        # remove key strings which belong in messages.po
        strip_key_strings(po)
        po.save()
Ejemplo n.º 3
0
def main():
    logging.basicConfig(stream=sys.stdout, level=logging.INFO)
    create_dir_if_necessary(LOCALE_DIR)
    source_msgs_dir = CONFIGURATION.source_messages_dir
    remove_file(source_msgs_dir.joinpath('django.po'))

    # Extract strings from mako templates.
    babel_mako_cmd = 'pybabel extract -k ugettext_lazy -k _u -F {config} -c "Translators:" . -o {output}'
    babel_mako_cmd = babel_mako_cmd.format(
        config=base(LOCALE_DIR, 'babel_mako.cfg'),
        output=base(CONFIGURATION.source_messages_dir, 'mako.po'),
    )
    execute(babel_mako_cmd, working_directory=BASE_DIR)

    makemessages = "django-admin.py makemessages -l en"
    ignores = " ".join('--ignore="{}/*"'.format(d) for d in CONFIGURATION.ignore_dirs)
    if ignores:
        makemessages += " " + ignores

    # Extract strings from django source files, including .py files.
    make_django_cmd = makemessages + ' --extension html'
    execute(make_django_cmd, working_directory=BASE_DIR)

    # Extract strings from Javascript source files.
    make_djangojs_cmd = makemessages + ' -d djangojs --extension js'
    execute(make_djangojs_cmd, working_directory=BASE_DIR)

    # makemessages creates 'django.po'. This filename is hardcoded.
    # Rename it to django-partial.po to enable merging into django.po later.
    os.rename(
        source_msgs_dir.joinpath('django.po'),
        source_msgs_dir.joinpath('django-partial.po')
    )

    # makemessages creates 'djangojs.po'. This filename is hardcoded.
    # Rename it to djangojs-partial.po to enable merging into djangojs.po later.
    os.rename(
        source_msgs_dir.joinpath('djangojs.po'),
        source_msgs_dir.joinpath('djangojs-partial.po')
    )

    files_to_clean = set()

    # Extract strings from third-party applications.
    for app_name in CONFIGURATION.third_party:
        # Import the app to find out where it is.  Then use pybabel to extract
        # from that directory.
        app_module = importlib.import_module(app_name)
        app_dir = path(app_module.__file__).dirname().dirname()
        output_file = source_msgs_dir / (app_name + ".po")
        files_to_clean.add(output_file)

        babel_cmd = 'pybabel extract -F {config} -c "Translators:" {app} -o {output}'
        babel_cmd = babel_cmd.format(
            config=LOCALE_DIR / 'babel_third_party.cfg',
            app=app_name,
            output=output_file,
        )
        execute(babel_cmd, working_directory=app_dir)

    # Segment the generated files.
    segmented_files = segment_pofiles("en")
    files_to_clean.update(segmented_files)

    # Finish each file.
    for filename in files_to_clean:
        LOG.info('Cleaning %s' % filename)
        po = pofile(source_msgs_dir.joinpath(filename))
        # replace default headers with edX headers
        fix_header(po)
        # replace default metadata with edX metadata
        fix_metadata(po)
        # remove key strings which belong in messages.po
        strip_key_strings(po)
        po.save()
Ejemplo n.º 4
0
    def run(self, args):
        """
        Main entry point of script
        """
        logging.basicConfig(stream=sys.stdout, level=logging.INFO)
        configuration = self.configuration
        configuration.locale_dir.parent.makedirs_p()
        # pylint: disable=attribute-defined-outside-init
        self.source_msgs_dir = configuration.source_messages_dir

        # The extraction process clobbers django.po and djangojs.po.
        # Save them so that it won't do that.
        self.rename_source_file('django.po', 'django-saved.po')
        self.rename_source_file('djangojs.po', 'djangojs-saved.po')

        # Extract strings from mako templates.
        verbosity_map = {
            0: "-q",
            1: "",
            2: "-v",
        }
        babel_verbosity = verbosity_map.get(args.verbose, "")

        if args.verbose:
            stderr = None
        else:
            stderr = DEVNULL

        # --keyword informs Babel that `interpolate()` is an expected
        # gettext function, which is necessary because the `tokenize` function
        # in the `markey` module marks it as such and passes it to Babel.
        # (These functions are called in the django-babel-underscore module.)
        babel_cmd_template = (
            'pybabel {verbosity} extract --mapping={config} '
            '--add-comments="Translators:" --keyword="interpolate" '
            '. --output={output}')

        babel_mako_cfg = self.base(configuration.locale_dir, 'babel_mako.cfg')
        if babel_mako_cfg.exists():
            babel_mako_cmd = babel_cmd_template.format(
                verbosity=babel_verbosity,
                config=babel_mako_cfg,
                output=self.base(configuration.source_messages_dir, 'mako.po'),
            )

            execute(babel_mako_cmd,
                    working_directory=configuration.root_dir,
                    stderr=stderr)

        babel_underscore_cfg = self.base(configuration.locale_dir,
                                         'babel_underscore.cfg')
        if babel_underscore_cfg.exists():
            babel_underscore_cmd = babel_cmd_template.format(
                verbosity=babel_verbosity,
                config=babel_underscore_cfg,
                output=self.base(configuration.source_messages_dir,
                                 'underscore.po'),
            )

            execute(babel_underscore_cmd,
                    working_directory=configuration.root_dir,
                    stderr=stderr)

        makemessages = "django-admin.py makemessages -l en -v{}".format(
            args.verbose)
        ignores = " ".join('--ignore="{}/*"'.format(d)
                           for d in configuration.ignore_dirs)
        if ignores:
            makemessages += " " + ignores

        # Extract strings from django source files (*.py, *.html, *.txt).
        make_django_cmd = makemessages + ' -d django'
        execute(make_django_cmd,
                working_directory=configuration.root_dir,
                stderr=stderr)

        # Extract strings from Javascript source files (*.js, *jsx).
        make_djangojs_cmd = makemessages + ' -d djangojs -e js,jsx'
        execute(make_djangojs_cmd,
                working_directory=configuration.root_dir,
                stderr=stderr)

        # makemessages creates 'django.po'. This filename is hardcoded.
        # Rename it to django-partial.po to enable merging into django.po later.
        self.rename_source_file('django.po', 'django-partial.po')

        # makemessages creates 'djangojs.po'. This filename is hardcoded.
        # Rename it to djangojs-partial.po to enable merging into djangojs.po later.
        self.rename_source_file('djangojs.po', 'djangojs-partial.po')

        files_to_clean = set()

        # Extract strings from third-party applications.
        for app_name in configuration.third_party:
            # Import the app to find out where it is.  Then use pybabel to extract
            # from that directory.
            app_module = importlib.import_module(app_name)
            app_dir = Path(app_module.__file__).dirname().dirname()  # pylint: disable=no-value-for-parameter
            output_file = self.source_msgs_dir / (app_name + ".po")
            files_to_clean.add(output_file)

            babel_cmd = 'pybabel {verbosity} extract -F {config} -c "Translators:" {app} -o {output}'
            babel_cmd = babel_cmd.format(
                verbosity=babel_verbosity,
                config=configuration.locale_dir / 'babel_third_party.cfg',
                app=app_name,
                output=output_file,
            )
            execute(babel_cmd, working_directory=app_dir, stderr=stderr)

        # Segment the generated files.
        segmented_files = segment_pofiles(configuration,
                                          configuration.source_locale)
        files_to_clean.update(segmented_files)

        # Finish each file.
        for filename in files_to_clean:
            LOG.info('Cleaning %s', filename)
            pofile = polib.pofile(self.source_msgs_dir.joinpath(filename))
            # replace default headers with edX headers
            fix_header(pofile)
            # replace default metadata with edX metadata
            fix_metadata(pofile)
            # remove key strings which belong in messages.po
            strip_key_strings(pofile)
            pofile.save()

        # Restore the saved .po files.
        self.rename_source_file('django-saved.po', 'django.po')
        self.rename_source_file('djangojs-saved.po', 'djangojs.po')
Ejemplo n.º 5
0
def main(verbosity=1):
    """
    Main entry point of script
    """
    logging.basicConfig(stream=sys.stdout, level=logging.INFO)
    LOCALE_DIR.parent.makedirs_p()
    source_msgs_dir = CONFIGURATION.source_messages_dir
    remove_file(source_msgs_dir.joinpath('django.po'))

    # Extract strings from mako templates.
    verbosity_map = {
        0: "-q",
        1: "",
        2: "-v",
    }
    babel_verbosity = verbosity_map.get(verbosity, "")

    babel_mako_cmd = 'pybabel {verbosity} extract -F {config} -c "Translators:" . -o {output}'
    babel_mako_cmd = babel_mako_cmd.format(
        verbosity=babel_verbosity,
        config=base(LOCALE_DIR, 'babel_mako.cfg'),
        output=base(CONFIGURATION.source_messages_dir, 'mako.po'),
    )
    if verbosity:
        stderr = None
    else:
        stderr = DEVNULL

    execute(babel_mako_cmd, working_directory=BASE_DIR, stderr=stderr)

    makemessages = "django-admin.py makemessages -l en -v{}".format(verbosity)
    ignores = " ".join('--ignore="{}/*"'.format(d) for d in CONFIGURATION.ignore_dirs)
    if ignores:
        makemessages += " " + ignores

    # Extract strings from django source files, including .py files.
    make_django_cmd = makemessages + ' --extension html'
    execute(make_django_cmd, working_directory=BASE_DIR, stderr=stderr)

    # Extract strings from Javascript source files.
    make_djangojs_cmd = makemessages + ' -d djangojs --extension js'
    execute(make_djangojs_cmd, working_directory=BASE_DIR, stderr=stderr)

    # Extract and megre strings from underscore files
    extract_and_merge_underscore()

    # makemessages creates 'django.po'. This filename is hardcoded.
    # Rename it to django-partial.po to enable merging into django.po later.
    os.rename(
        source_msgs_dir.joinpath('django.po'),
        source_msgs_dir.joinpath('django-partial.po')
    )

    # makemessages creates 'djangojs.po'. This filename is hardcoded.
    # Rename it to djangojs-partial.po to enable merging into djangojs.po later.
    os.rename(
        source_msgs_dir.joinpath('djangojs.po'),
        source_msgs_dir.joinpath('djangojs-partial.po')
    )

    files_to_clean = set()

    # Extract strings from third-party applications.
    for app_name in CONFIGURATION.third_party:
        # Import the app to find out where it is.  Then use pybabel to extract
        # from that directory.
        app_module = importlib.import_module(app_name)
        app_dir = path(app_module.__file__).dirname().dirname()
        output_file = source_msgs_dir / (app_name + ".po")
        files_to_clean.add(output_file)

        babel_cmd = 'pybabel {verbosity} extract -F {config} -c "Translators:" {app} -o {output}'
        babel_cmd = babel_cmd.format(
            verbosity=babel_verbosity,
            config=LOCALE_DIR / 'babel_third_party.cfg',
            app=app_name,
            output=output_file,
        )
        execute(babel_cmd, working_directory=app_dir, stderr=stderr)

    # Segment the generated files.
    segmented_files = segment_pofiles("en")
    files_to_clean.update(segmented_files)

    # Finish each file.
    for filename in files_to_clean:
        LOG.info('Cleaning %s' % filename)
        po = pofile(source_msgs_dir.joinpath(filename))
        # replace default headers with edX headers
        fix_header(po)
        # replace default metadata with edX metadata
        fix_metadata(po)
        # remove key strings which belong in messages.po
        strip_key_strings(po)
        po.save()
Ejemplo n.º 6
0
    def run(self, args):
        """
        Main entry point of script
        """
        logging.basicConfig(stream=sys.stdout, level=logging.INFO)
        config.LOCALE_DIR.parent.makedirs_p()
        source_msgs_dir = config.CONFIGURATION.source_messages_dir
        remove_file(source_msgs_dir.joinpath('django.po'))

        # Extract strings from mako templates.
        verbosity_map = {
            0: "-q",
            1: "",
            2: "-v",
        }
        babel_verbosity = verbosity_map.get(args.verbose, "")

        babel_mako_cfg = base(config.LOCALE_DIR, 'babel_mako.cfg')
        if args.verbose:
            stderr = None
        else:
            stderr = DEVNULL

        if babel_mako_cfg.exists():
            babel_mako_cmd = 'pybabel {verbosity} extract -F {config} -c "Translators:" . -o {output}'
            babel_mako_cmd = babel_mako_cmd.format(
                verbosity=babel_verbosity,
                config=babel_mako_cfg,
                output=base(config.CONFIGURATION.source_messages_dir, 'mako.po'),
            )

            execute(babel_mako_cmd, working_directory=config.BASE_DIR, stderr=stderr)

        makemessages = "django-admin.py makemessages -l en -v{}".format(args.verbose)
        ignores = " ".join('--ignore="{}/*"'.format(d) for d in config.CONFIGURATION.ignore_dirs)
        if ignores:
            makemessages += " " + ignores

        # Extract strings from django source files, including .py files.
        make_django_cmd = makemessages + ' --extension html'
        execute(make_django_cmd, working_directory=config.BASE_DIR, stderr=stderr)

        # Extract strings from Javascript source files.
        make_djangojs_cmd = makemessages + ' -d djangojs --extension js'
        execute(make_djangojs_cmd, working_directory=config.BASE_DIR, stderr=stderr)

        # makemessages creates 'django.po'. This filename is hardcoded.
        # Rename it to django-partial.po to enable merging into django.po later.
        os.rename(
            source_msgs_dir.joinpath('django.po'),
            source_msgs_dir.joinpath('django-partial.po')
        )

        # makemessages creates 'djangojs.po'. This filename is hardcoded.
        # Rename it to djangojs-partial.po to enable merging into djangojs.po later.
        os.rename(
            source_msgs_dir.joinpath('djangojs.po'),
            source_msgs_dir.joinpath('djangojs-partial.po')
        )

        files_to_clean = set()

        # Extract strings from third-party applications.
        for app_name in config.CONFIGURATION.third_party:
            # Import the app to find out where it is.  Then use pybabel to extract
            # from that directory.
            app_module = importlib.import_module(app_name)
            app_dir = path(app_module.__file__).dirname().dirname()
            output_file = source_msgs_dir / (app_name + ".po")
            files_to_clean.add(output_file)

            babel_cmd = 'pybabel {verbosity} extract -F {config} -c "Translators:" {app} -o {output}'
            babel_cmd = babel_cmd.format(
                verbosity=babel_verbosity,
                config=config.LOCALE_DIR / 'babel_third_party.cfg',
                app=app_name,
                output=output_file,
            )
            execute(babel_cmd, working_directory=app_dir, stderr=stderr)

        # Segment the generated files.
        segmented_files = segment_pofiles("en")
        files_to_clean.update(segmented_files)

        # Finish each file.
        for filename in files_to_clean:
            LOG.info('Cleaning %s', filename)
            pofile = polib.pofile(source_msgs_dir.joinpath(filename))
            # replace default headers with edX headers
            fix_header(pofile)
            # replace default metadata with edX metadata
            fix_metadata(pofile)
            # remove key strings which belong in messages.po
            strip_key_strings(pofile)
            pofile.save()
Ejemplo n.º 7
0
def main():
    logging.basicConfig(stream=sys.stdout, level=logging.INFO)
    create_dir_if_necessary(LOCALE_DIR)
    source_msgs_dir = CONFIGURATION.source_messages_dir
    remove_file(source_msgs_dir.joinpath('django.po'))

    # Extract strings from mako templates.
    babel_mako_cmd = 'pybabel extract -F {config} -c "Translators:" . -o {output}'
    babel_mako_cmd = babel_mako_cmd.format(
        config=base(LOCALE_DIR, 'babel_mako.cfg'),
        output=base(CONFIGURATION.source_messages_dir, 'mako.po'),
    )
    execute(babel_mako_cmd, working_directory=BASE_DIR)

    makemessages = "django-admin.py makemessages -l en"
    ignores = " ".join('--ignore="{}/*"'.format(d) for d in CONFIGURATION.ignore_dirs)
    if ignores:
        makemessages += " " + ignores

    # Extract strings from django source files, including .py files.
    make_django_cmd = makemessages + ' --extension html'
    execute(make_django_cmd, working_directory=BASE_DIR)

    # Extract strings from Javascript source files.
    make_djangojs_cmd = makemessages + ' -d djangojs --extension js'
    execute(make_djangojs_cmd, working_directory=BASE_DIR)

    # makemessages creates 'django.po'. This filename is hardcoded.
    # Rename it to django-partial.po to enable merging into django.po later.
    os.rename(
        source_msgs_dir.joinpath('django.po'),
        source_msgs_dir.joinpath('django-partial.po')
    )

    # makemessages creates 'djangojs.po'. This filename is hardcoded.
    # Rename it to djangojs-partial.po to enable merging into djangojs.po later.
    os.rename(
        source_msgs_dir.joinpath('djangojs.po'),
        source_msgs_dir.joinpath('djangojs-partial.po')
    )

    # Extract strings from third-party applications.
    for app_name in CONFIGURATION.third_party:
        # Import the app to find out where it is.  Then use pybabel to extract
        # from that directory.
        app_module = importlib.import_module(app_name)
        app_dir = path(app_module.__file__).dirname().dirname()
        babel_cmd = 'pybabel extract -F {config} -c "Translators:" {app} -o {output}'
        babel_cmd = babel_cmd.format(
            config=LOCALE_DIR / 'babel_third_party.cfg',
            app=app_name,
            output=source_msgs_dir / (app_name + ".po"),
        )
        execute(babel_cmd, working_directory=app_dir)

    # Segment the generated files.
    segmented_files = segment_pofiles("en")

    # Finish each file.
    for filename in segmented_files:
        LOG.info('Cleaning %s' % filename)
        po = pofile(source_msgs_dir.joinpath(filename))
        # replace default headers with edX headers
        fix_header(po)
        # replace default metadata with edX metadata
        fix_metadata(po)
        # remove key strings which belong in messages.po
        strip_key_strings(po)
        po.save()
Ejemplo n.º 8
0
    def run(self, args):
        """
        Main entry point of script
        """
        locales = config.CONFIGURATION.locales
        logging.basicConfig(stream=sys.stdout, level=logging.INFO)
        config.LOCALE_DIR.parent.makedirs_p()

        verbosity_map = {
            0: "-q",
            1: "",
            2: "-v",
        }
        babel_verbosity = verbosity_map.get(args.verbose, "")

        if args.verbose:
            stderr = None
        else:
            stderr = DEVNULL

        self.run_babel_extraction('mako', 'babel_mako.cfg', babel_verbosity,
                                  stderr)
        self.run_babel_extraction('underscore', 'babel_underscore.cfg',
                                  babel_verbosity, stderr)

        for locale in locales:
            # The extraction process clobbers django.po and djangojs.po.
            # Save them so that it won't do that.
            locale_msg_dir = config.CONFIGURATION.get_messages_dir(locale)
            self.rename_source_file('django.po', 'django-saved.po',
                                    locale_msg_dir)
            self.rename_source_file('djangojs.po', 'djangojs-saved.po',
                                    locale_msg_dir)

            if os.path.isfile(base(locale_msg_dir, 'django-partial.po')):
                merge(locale, 'django-partial.po', 'django')
            if os.path.isfile(base(locale_msg_dir, 'djangojs-partial.po')):
                merge(locale, 'django-partial.po', 'djangojs')

            makemessages = "django-admin.py makemessages -l {locale} -v{verbosity}" \
                .format(locale=locale, verbosity=args.verbose)
            ignores = " ".join('--ignore="{}/*"'.format(d)
                               for d in config.CONFIGURATION.ignore_dirs)
            if ignores:
                makemessages += " " + ignores

            # Extract strings from django source files (*.py, *.html, *.txt).
            make_django_cmd = makemessages + ' -d django'
            execute(make_django_cmd,
                    working_directory=config.BASE_DIR,
                    stderr=stderr)

            # Extract strings from Javascript source files (*.js).
            make_djangojs_cmd = makemessages + ' -d djangojs'
            execute(make_djangojs_cmd,
                    working_directory=config.BASE_DIR,
                    stderr=stderr)

            # makemessages creates 'django.po'. This filename is hardcoded.
            # Rename it to django-partial.po to enable merging into django.po later.
            self.rename_source_file('django.po', 'django-partial.po',
                                    locale_msg_dir)
            # makemessages creates 'djangojs.po'. This filename is hardcoded.
            # Rename it to djangojs-partial.po to enable merging into djangojs.po later.
            self.rename_source_file('djangojs.po', 'djangojs-partial.po',
                                    locale_msg_dir)

            files_to_clean = set()

            # Segment the generated files.
            segmented_files = segment_pofiles(locale)
            files_to_clean.update(segmented_files)

            # Finish each file.
            for filename in files_to_clean:
                LOG.info('Cleaning %s', filename)
                pofile = polib.pofile(locale_msg_dir.joinpath(filename))
                # replace default headers with edX headers
                fix_header(pofile)
                # replace default metadata with edX metadata
                fix_metadata(pofile)
                # remove key strings which belong in messages.po
                strip_key_strings(pofile)
                pofile.save()

            # Restore the saved .po files.
            self.rename_source_file('django-saved.po', 'django.po',
                                    locale_msg_dir)
            self.rename_source_file('djangojs-saved.po', 'djangojs.po',
                                    locale_msg_dir)
Ejemplo n.º 9
0
    def run(self, args):
        """
        Main entry point of script
        """
        logging.basicConfig(stream=sys.stdout, level=logging.INFO)
        config.LOCALE_DIR.parent.makedirs_p()
        source_msgs_dir = config.CONFIGURATION.source_messages_dir
        remove_file(source_msgs_dir.joinpath("django.po"))

        # Extract strings from mako templates.
        verbosity_map = {0: "-q", 1: "", 2: "-v"}
        babel_verbosity = verbosity_map.get(args.verbose, "")

        if args.verbose:
            stderr = None
        else:
            stderr = DEVNULL

        # --keyword informs Babel that `interpolate()` is an expected
        # gettext function, which is necessary because the `tokenize` function
        # in the `markey` module marks it as such and passes it to Babel.
        # (These functions are called in the django-babel-underscore module.)
        babel_cmd_template = (
            "pybabel {verbosity} extract --mapping={config} "
            '--add-comments="Translators:" --keyword="interpolate" '
            ". --output={output}"
        )

        babel_mako_cfg = base(config.LOCALE_DIR, "babel_mako.cfg")
        if babel_mako_cfg.exists():
            babel_mako_cmd = babel_cmd_template.format(
                verbosity=babel_verbosity,
                config=babel_mako_cfg,
                output=base(config.CONFIGURATION.source_messages_dir, "mako.po"),
            )

            execute(babel_mako_cmd, working_directory=config.BASE_DIR, stderr=stderr)

        babel_underscore_cfg = base(config.LOCALE_DIR, "babel_underscore.cfg")
        if babel_underscore_cfg.exists():
            babel_underscore_cmd = babel_cmd_template.format(
                verbosity=babel_verbosity,
                config=babel_underscore_cfg,
                output=base(config.CONFIGURATION.source_messages_dir, "underscore.po"),
            )

            execute(babel_underscore_cmd, working_directory=config.BASE_DIR, stderr=stderr)

        makemessages = "django-admin.py makemessages -l en -v{}".format(args.verbose)
        ignores = " ".join('--ignore="{}/*"'.format(d) for d in config.CONFIGURATION.ignore_dirs)
        if ignores:
            makemessages += " " + ignores

        # Extract strings from django source files, including .py files.
        make_django_cmd = makemessages + " --extension html"
        execute(make_django_cmd, working_directory=config.BASE_DIR, stderr=stderr)

        # Extract strings from Javascript source files.
        make_djangojs_cmd = makemessages + " -d djangojs --extension js"
        execute(make_djangojs_cmd, working_directory=config.BASE_DIR, stderr=stderr)

        # makemessages creates 'django.po'. This filename is hardcoded.
        # Rename it to django-partial.po to enable merging into django.po later.
        os.rename(source_msgs_dir.joinpath("django.po"), source_msgs_dir.joinpath("django-partial.po"))

        # makemessages creates 'djangojs.po'. This filename is hardcoded.
        # Rename it to djangojs-partial.po to enable merging into djangojs.po later.
        os.rename(source_msgs_dir.joinpath("djangojs.po"), source_msgs_dir.joinpath("djangojs-partial.po"))

        files_to_clean = set()

        # Extract strings from third-party applications.
        for app_name in config.CONFIGURATION.third_party:
            # Import the app to find out where it is.  Then use pybabel to extract
            # from that directory.
            app_module = importlib.import_module(app_name)
            app_dir = path(app_module.__file__).dirname().dirname()  # pylint: disable=no-value-for-parameter
            output_file = source_msgs_dir / (app_name + ".po")
            files_to_clean.add(output_file)

            babel_cmd = 'pybabel {verbosity} extract -F {config} -c "Translators:" {app} -o {output}'
            babel_cmd = babel_cmd.format(
                verbosity=babel_verbosity,
                config=config.LOCALE_DIR / "babel_third_party.cfg",
                app=app_name,
                output=output_file,
            )
            execute(babel_cmd, working_directory=app_dir, stderr=stderr)

        # Segment the generated files.
        segmented_files = segment_pofiles("en")
        files_to_clean.update(segmented_files)

        # Finish each file.
        for filename in files_to_clean:
            LOG.info("Cleaning %s", filename)
            pofile = polib.pofile(source_msgs_dir.joinpath(filename))
            # replace default headers with edX headers
            fix_header(pofile)
            # replace default metadata with edX metadata
            fix_metadata(pofile)
            # remove key strings which belong in messages.po
            strip_key_strings(pofile)
            pofile.save()
Ejemplo n.º 10
0
def extract_theme_tos():
    segment.segment_pofile = segment_pofile_lazy
    files = segment.segment_pofiles(CONFIG, CONFIG.source_locale)
    return files
Ejemplo n.º 11
0
    def run(self, args):
        """
        Main entry point of script
        """
        logging.basicConfig(stream=sys.stdout, level=logging.INFO)
        configuration = self.configuration
        configuration.locale_dir.parent.makedirs_p()
        # pylint: disable=attribute-defined-outside-init
        self.source_msgs_dir = configuration.source_messages_dir

        # The extraction process clobbers django.po and djangojs.po.
        # Save them so that it won't do that.
        self.rename_source_file('django.po', 'django-saved.po')
        self.rename_source_file('djangojs.po', 'djangojs-saved.po')

        # Extract strings from mako templates.
        verbosity_map = {
            0: "-q",
            1: "",
            2: "-v",
        }
        babel_verbosity = verbosity_map.get(args.verbose, "")

        if args.verbose:
            stderr = None
        else:
            stderr = DEVNULL

        # --keyword informs Babel that `interpolate()` is an expected
        # gettext function, which is necessary because the `tokenize` function
        # in the `markey` module marks it as such and passes it to Babel.
        # (These functions are called in the django-babel-underscore module.)
        babel_cmd_template = (
            'pybabel {verbosity} extract --mapping={config} '
            '--add-comments="Translators:" --keyword="interpolate" '
            '. --output={output}'
        )

        babel_mako_cfg = self.base(configuration.locale_dir, 'babel_mako.cfg')
        if babel_mako_cfg.exists():
            babel_mako_cmd = babel_cmd_template.format(
                verbosity=babel_verbosity,
                config=babel_mako_cfg,
                output=self.base(configuration.source_messages_dir, 'mako.po'),
            )

            execute(babel_mako_cmd, working_directory=configuration.root_dir, stderr=stderr)

        babel_underscore_cfg = self.base(configuration.locale_dir, 'babel_underscore.cfg')
        if babel_underscore_cfg.exists():
            babel_underscore_cmd = babel_cmd_template.format(
                verbosity=babel_verbosity,
                config=babel_underscore_cfg,
                output=self.base(configuration.source_messages_dir, 'underscore.po'),
            )

            execute(babel_underscore_cmd, working_directory=configuration.root_dir, stderr=stderr)

        makemessages = "django-admin.py makemessages -l en -v{}".format(args.verbose)
        ignores = " ".join('--ignore="{}/*"'.format(d) for d in configuration.ignore_dirs)
        if ignores:
            makemessages += " " + ignores

        # Extract strings from django source files (*.py, *.html, *.txt).
        make_django_cmd = makemessages + ' -d django'
        execute(make_django_cmd, working_directory=configuration.root_dir, stderr=stderr)

        # Extract strings from Javascript source files (*.js, *jsx).
        make_djangojs_cmd = makemessages + ' -d djangojs -e js,jsx'
        execute(make_djangojs_cmd, working_directory=configuration.root_dir, stderr=stderr)

        # makemessages creates 'django.po'. This filename is hardcoded.
        # Rename it to django-partial.po to enable merging into django.po later.
        self.rename_source_file('django.po', 'django-partial.po')

        # makemessages creates 'djangojs.po'. This filename is hardcoded.
        # Rename it to djangojs-partial.po to enable merging into djangojs.po later.
        self.rename_source_file('djangojs.po', 'djangojs-partial.po')

        files_to_clean = set()

        # Extract strings from third-party applications.
        for app_name in configuration.third_party:
            # Import the app to find out where it is.  Then use pybabel to extract
            # from that directory.
            app_module = importlib.import_module(app_name)
            app_dir = Path(app_module.__file__).dirname().dirname()  # pylint: disable=no-value-for-parameter
            output_file = self.source_msgs_dir / (app_name + ".po")
            files_to_clean.add(output_file)

            babel_cmd = 'pybabel {verbosity} extract -F {config} -c "Translators:" {app} -o {output}'
            babel_cmd = babel_cmd.format(
                verbosity=babel_verbosity,
                config=configuration.locale_dir / 'babel_third_party.cfg',
                app=app_name,
                output=output_file,
            )
            execute(babel_cmd, working_directory=app_dir, stderr=stderr)

        # Segment the generated files.
        segmented_files = segment_pofiles(configuration, configuration.source_locale)
        files_to_clean.update(segmented_files)

        # Finish each file.
        for filename in files_to_clean:
            LOG.info('Cleaning %s', filename)
            pofile = polib.pofile(self.source_msgs_dir.joinpath(filename))
            # replace default headers with edX headers
            fix_header(pofile)
            # replace default metadata with edX metadata
            fix_metadata(pofile)
            # remove key strings which belong in messages.po
            strip_key_strings(pofile)
            pofile.save()

        # Restore the saved .po files.
        self.rename_source_file('django-saved.po', 'django.po')
        self.rename_source_file('djangojs-saved.po', 'djangojs.po')