示例#1
0
    def preprocess(self):
        """
        Preprocess (if necessary) a translatable file before passing it to
        xgettext GNU gettext utility.
        """
        if not self.is_templatized:
            return

        with open(self.path, encoding="utf-8") as fp:
            src_data = fp.read()

        if self.domain == "djangojs":
            content = prepare_js_for_gettext(src_data)
        elif self.domain == "django":
            content = templatize(src_data, origin=self.path[2:])

        with open(self.work_path, "w", encoding="utf-8") as fp:
            fp.write(content)
示例#2
0
    def preprocess(self):
        """
        Preprocess (if necessary) a translatable file before passing it to
        xgettext GNU gettext utility.
        """
        if not self.is_templatized:
            return

        with io.open(self.path, 'r', encoding=settings.FILE_CHARSET) as fp:
            src_data = fp.read()

        if self.domain == 'djangojs':
            content = prepare_js_for_gettext(src_data)
        elif self.domain == 'django':
            content = templatize(src_data, self.path[2:])

        with io.open(self.work_path, 'w', encoding='utf-8') as fp:
            fp.write(content)
示例#3
0
    def preprocess(self):
        """
        Preprocess (if necessary) a translatable file before passing it to
        xgettext GNU gettext utility.
        """
        if not self.is_templatized:
            return

        with io.open(self.path, 'r', encoding=settings.FILE_CHARSET) as fp:
            src_data = fp.read()

        if self.domain == 'djangojs':
            content = prepare_js_for_gettext(src_data)
        elif self.domain == 'django':
            content = templatize(src_data, self.path[2:])

        with io.open(self.work_path, 'w', encoding='utf-8') as fp:
            fp.write(content)
示例#4
0
def process_file(file, dirpath, potfile, domain, verbosity, extensions, wrap,
                 location):
    """
    Extract translatable literals from :param file: for :param domain:
    creating or updating the :param potfile: POT file.

    Uses the xgettext GNU gettext utility.
    """

    from django.utils.translation import templatize

    if verbosity > 1:
        sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
    _, file_ext = os.path.splitext(file)
    if domain == 'djangojs' and file_ext in extensions:
        is_templatized = True
        orig_file = os.path.join(dirpath, file)
        src_data = open(orig_file).read()
        src_data = prepare_js_for_gettext(src_data)
        thefile = '%s.c' % file
        work_file = os.path.join(dirpath, thefile)
        f = open(work_file, "w")
        try:
            f.write(src_data)
        finally:
            f.close()
        cmd = ('xgettext -d %s -L C %s %s --keyword=gettext_noop '
               '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
               '--keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 '
               '--from-code UTF-8 --add-comments=Translators -o - "%s"' %
               (domain, wrap, location, work_file))
    elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
        thefile = file
        orig_file = os.path.join(dirpath, file)
        is_templatized = file_ext in extensions
        if is_templatized:
            src_data = open(orig_file, "rU").read()
            thefile = '%s.py' % file
            content = templatize(src_data, orig_file[2:])
            f = open(os.path.join(dirpath, thefile), "w")
            try:
                f.write(content)
            finally:
                f.close()
        work_file = os.path.join(dirpath, thefile)
        cmd = ('xgettext -d %s -L Python %s %s --keyword=gettext_noop '
               '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
               '--keyword=ugettext_noop --keyword=ugettext_lazy '
               '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
               '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
               '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 '
               '--add-comments=Translators -o - "%s"' %
               (domain, wrap, location, work_file))
    else:
        return
    msgs, errors = _popen(cmd)
    if errors:
        if is_templatized:
            os.unlink(work_file)
        if os.path.exists(potfile):
            os.unlink(potfile)
        raise CommandError("errors happened while running xgettext on %s\n%s" %
                           (file, errors))
    if msgs:
        write_pot_file(potfile, msgs, orig_file, work_file, is_templatized)
    if is_templatized:
        os.unlink(work_file)
示例#5
0
    def process(self, command, potfile, domain, keep_pot=False):
        """
        Extract translatable literals from self.file for :param domain:
        creating or updating the :param potfile: POT file.

        Uses the xgettext GNU gettext utility.
        """

        from django.utils.translation import templatize

        if command.verbosity > 1:
            command.stdout.write('processing file %s in %s\n' % (self.file, self.dirpath))
        _, file_ext = os.path.splitext(self.file)
        if domain == 'djangojs' and file_ext in command.extensions:
            is_templatized = True
            orig_file = os.path.join(self.dirpath, self.file)
            with open(orig_file) as fp:
                src_data = fp.read()
            src_data = prepare_js_for_gettext(src_data)
            thefile = '%s.c' % self.file
            work_file = os.path.join(self.dirpath, thefile)
            with open(work_file, "w") as fp:
                fp.write(src_data)
            cmd = (
                'xgettext -d %s -L C %s %s --keyword=gettext_noop '
                '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
                '--keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 '
                '--from-code UTF-8 --add-comments=Translators -o - "%s"' %
                (domain, command.wrap, command.location, work_file))
        elif domain == 'django' and (file_ext == '.py' or file_ext in command.extensions):
            thefile = self.file
            orig_file = os.path.join(self.dirpath, self.file)
            is_templatized = file_ext in command.extensions
            if is_templatized:
                with open(orig_file, "rU") as fp:
                    src_data = fp.read()
                thefile = '%s.py' % self.file
                content = templatize(src_data, orig_file[2:])
                with open(os.path.join(self.dirpath, thefile), "w") as fp:
                    fp.write(content)
            work_file = os.path.join(self.dirpath, thefile)
            cmd = (
                'xgettext -d %s -L Python %s %s --keyword=gettext_noop '
                '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
                '--keyword=ugettext_noop --keyword=ugettext_lazy '
                '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
                '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
                '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 '
                '--add-comments=Translators -o - "%s"' %
                (domain, command.wrap, command.location, work_file))
        else:
            return
        msgs, errors, status = _popen(cmd)
        if errors:
            if status != STATUS_OK:
                if is_templatized:
                    os.unlink(work_file)
                if not keep_pot and os.path.exists(potfile):
                    os.unlink(potfile)
                raise CommandError(
                    "errors happened while running xgettext on %s\n%s" %
                    (self.file, errors))
            elif command.verbosity > 0:
                # Print warnings
                command.stdout.write(errors)
        if msgs:
            if is_templatized:
                old = '#: ' + work_file[2:]
                new = '#: ' + orig_file[2:]
                msgs = msgs.replace(old, new)
            write_pot_file(potfile, msgs)
        if is_templatized:
            os.unlink(work_file)
示例#6
0
    def process(self, command, potfile, domain, keep_pot=False):
        """
        Extract translatable literals from self.file for :param domain:
        creating or updating the :param potfile: POT file.

        Uses the xgettext GNU gettext utility.
        """

        from django.utils.translation import templatize

        if command.verbosity > 1:
            command.stdout.write('processing file %s in %s\n' %
                                 (self.file, self.dirpath))
        _, file_ext = os.path.splitext(self.file)
        if domain == 'djangojs' and file_ext in command.extensions:
            is_templatized = True
            orig_file = os.path.join(self.dirpath, self.file)
            with open(orig_file) as fp:
                src_data = fp.read()
            src_data = prepare_js_for_gettext(src_data)
            thefile = '%s.c' % self.file
            work_file = os.path.join(self.dirpath, thefile)
            with open(work_file, "w") as fp:
                fp.write(src_data)
            cmd = ('xgettext -d %s -L C %s %s --keyword=gettext_noop '
                   '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
                   '--keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 '
                   '--from-code UTF-8 --add-comments=Translators -o - "%s"' %
                   (domain, command.wrap, command.location, work_file))
        elif domain == 'django' and (file_ext == '.py'
                                     or file_ext in command.extensions):
            thefile = self.file
            orig_file = os.path.join(self.dirpath, self.file)
            is_templatized = file_ext in command.extensions
            if is_templatized:
                with open(orig_file, "rU") as fp:
                    src_data = fp.read()
                thefile = '%s.py' % self.file
                content = templatize(src_data, orig_file[2:])
                with open(os.path.join(self.dirpath, thefile), "w") as fp:
                    fp.write(content)
            work_file = os.path.join(self.dirpath, thefile)
            cmd = ('xgettext -d %s -L Python %s %s --keyword=gettext_noop '
                   '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
                   '--keyword=ugettext_noop --keyword=ugettext_lazy '
                   '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
                   '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
                   '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 '
                   '--add-comments=Translators -o - "%s"' %
                   (domain, command.wrap, command.location, work_file))
        else:
            return
        msgs, errors, status = _popen(cmd)
        if errors:
            if status != STATUS_OK:
                if is_templatized:
                    os.unlink(work_file)
                if not keep_pot and os.path.exists(potfile):
                    os.unlink(potfile)
                raise CommandError(
                    "errors happened while running xgettext on %s\n%s" %
                    (self.file, errors))
            elif command.verbosity > 0:
                # Print warnings
                command.stdout.write(errors)
        if msgs:
            if is_templatized:
                old = '#: ' + work_file[2:]
                new = '#: ' + orig_file[2:]
                msgs = msgs.replace(old, new)
            write_pot_file(potfile, msgs)
        if is_templatized:
            os.unlink(work_file)
示例#7
0
def process_file(file, dirpath, potfile, domain, verbosity,
                 extensions, wrap, location, stdout=sys.stdout):
    """
    Extract translatable literals from :param file: for :param domain:
    creating or updating the :param potfile: POT file.

    Uses the xgettext GNU gettext utility.
    """

    from django.utils.translation import templatize

    if verbosity > 1:
        stdout.write('processing file %s in %s\n' % (file, dirpath))
    _, file_ext = os.path.splitext(file)
    if domain == 'djangojs' and file_ext in extensions:
        is_templatized = True
        orig_file = os.path.join(dirpath, file)
        src_data = open(orig_file).read()
        src_data = prepare_js_for_gettext(src_data)
        thefile = '%s.c' % file
        work_file = os.path.join(dirpath, thefile)
        f = open(work_file, "w")
        try:
            f.write(src_data)
        finally:
            f.close()
        cmd = (
            'xgettext -d %s -L C %s %s --keyword=gettext_noop '
            '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
            '--keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 '
            '--from-code UTF-8 --add-comments=Translators -o - "%s"' %
            (domain, wrap, location, work_file))
    elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
        thefile = file
        orig_file = os.path.join(dirpath, file)
        is_templatized = file_ext in extensions
        if is_templatized:
            src_data = open(orig_file, "rU").read()
            thefile = '%s.py' % file
            content = templatize(src_data, orig_file[2:])
            f = open(os.path.join(dirpath, thefile), "w")
            try:
                f.write(content)
            finally:
                f.close()
        work_file = os.path.join(dirpath, thefile)
        cmd = (
            'xgettext -d %s -L Python %s %s --keyword=gettext_noop '
            '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
            '--keyword=ugettext_noop --keyword=ugettext_lazy '
            '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
            '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
            '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 '
            '--add-comments=Translators -o - "%s"' %
            (domain, wrap, location, work_file))
    else:
        return
    msgs, errors = _popen(cmd)
    if errors:
        if is_templatized:
            os.unlink(work_file)
        if os.path.exists(potfile):
            os.unlink(potfile)
        raise CommandError(
            "errors happened while running xgettext on %s\n%s" %
            (file, errors))
    if msgs:
        write_pot_file(potfile, msgs, orig_file, work_file, is_templatized)
    if is_templatized:
        os.unlink(work_file)
示例#8
0
 def test_func(self):
     self.assertMultiLineEqual(prepare_js_for_gettext(js), c)
示例#9
0
    def process(self, command, domain):
        """
        Extract translatable literals from self.file for :param domain:,
        creating or updating the POT file.

        Uses the xgettext GNU gettext utility.
        """

        from django.conf import settings
        from django.utils.translation import templatize

        if command.verbosity > 1:
            command.stdout.write('processing file %s in %s\n' %
                                 (self.file, self.dirpath))
        _, file_ext = os.path.splitext(self.file)
        if domain == 'djangojs' and file_ext in command.extensions:
            is_templatized = True
            orig_file = os.path.join(self.dirpath, self.file)
            with io.open(orig_file, encoding=settings.FILE_CHARSET) as fp:
                src_data = fp.read()
            src_data = prepare_js_for_gettext(src_data)
            thefile = '%s.c' % self.file
            work_file = os.path.join(self.dirpath, thefile)
            with io.open(work_file, "w", encoding='utf-8') as fp:
                fp.write(src_data)
            args = [
                'xgettext', '-d', domain, '--language=C',
                '--keyword=gettext_noop', '--keyword=gettext_lazy',
                '--keyword=ngettext_lazy:1,2', '--keyword=pgettext:1c,2',
                '--keyword=npgettext:1c,2,3', '--output=-'
            ] + command.xgettext_options
            args.append(work_file)
        elif domain == 'django' and (file_ext == '.py'
                                     or file_ext in command.extensions):
            thefile = self.file
            orig_file = os.path.join(self.dirpath, self.file)
            is_templatized = file_ext in command.extensions
            if is_templatized:
                with io.open(orig_file, 'r',
                             encoding=settings.FILE_CHARSET) as fp:
                    src_data = fp.read()
                thefile = '%s.py' % self.file
                content = templatize(src_data, orig_file[2:])
                with io.open(os.path.join(self.dirpath, thefile),
                             "w",
                             encoding='utf-8') as fp:
                    fp.write(content)
            work_file = os.path.join(self.dirpath, thefile)
            args = [
                'xgettext', '-d', domain, '--language=Python',
                '--keyword=gettext_noop', '--keyword=gettext_lazy',
                '--keyword=ngettext_lazy:1,2', '--keyword=ugettext_noop',
                '--keyword=ugettext_lazy', '--keyword=ungettext_lazy:1,2',
                '--keyword=pgettext:1c,2', '--keyword=npgettext:1c,2,3',
                '--keyword=pgettext_lazy:1c,2',
                '--keyword=npgettext_lazy:1c,2,3', '--output=-'
            ] + command.xgettext_options
            args.append(work_file)
        else:
            return
        msgs, errors, status = popen_wrapper(args)
        if errors:
            if status != STATUS_OK:
                if is_templatized:
                    os.unlink(work_file)
                raise CommandError(
                    "errors happened while running xgettext on %s\n%s" %
                    (self.file, errors))
            elif command.verbosity > 0:
                # Print warnings
                command.stdout.write(errors)
        if msgs:
            if six.PY2:
                msgs = msgs.decode('utf-8')
            # Write/append messages to pot file
            potfile = os.path.join(self.locale_dir, '%s.pot' % str(domain))
            if is_templatized:
                # Remove '.py' suffix
                if os.name == 'nt':
                    # Preserve '.\' prefix on Windows to respect gettext behavior
                    old = '#: ' + work_file
                    new = '#: ' + orig_file
                else:
                    old = '#: ' + work_file[2:]
                    new = '#: ' + orig_file[2:]
                msgs = msgs.replace(old, new)
            write_pot_file(potfile, msgs)

        if is_templatized:
            os.unlink(work_file)
from __future__ import unicode_literals
示例#11
0
    def process(self, command, domain):
        """
        Extract translatable literals from self.file for :param domain:,
        creating or updating the POT file.

        Uses the xgettext GNU gettext utility.
        """
        from django.utils.translation import templatize

        if command.verbosity > 1:
            command.stdout.write('processing file %s in %s\n' % (self.file, self.dirpath))
        file_ext = os.path.splitext(self.file)[1]
        if domain == 'djangojs':
            orig_file = os.path.join(self.dirpath, self.file)
            work_file = orig_file
            is_templatized = command.gettext_version < (0, 18, 3)
            if is_templatized:
                with io.open(orig_file, 'r', encoding=settings.FILE_CHARSET) as fp:
                    src_data = fp.read()
                src_data = prepare_js_for_gettext(src_data)
                work_file = os.path.join(self.dirpath, '%s.c' % self.file)
                with io.open(work_file, "w", encoding='utf-8') as fp:
                    fp.write(src_data)
            args = [
                'xgettext',
                '-d', domain,
                '--language=%s' % ('C' if is_templatized else 'JavaScript',),
                '--keyword=gettext_noop',
                '--keyword=gettext_lazy',
                '--keyword=ngettext_lazy:1,2',
                '--keyword=pgettext:1c,2',
                '--keyword=npgettext:1c,2,3',
                '--output=-'
            ] + command.xgettext_options
            args.append(work_file)
        elif domain == 'django':
            orig_file = os.path.join(self.dirpath, self.file)
            work_file = orig_file
            is_templatized = file_ext != '.py'
            if is_templatized:
                with io.open(orig_file, encoding=settings.FILE_CHARSET) as fp:
                    src_data = fp.read()
                content = templatize(src_data, orig_file[2:])
                work_file = os.path.join(self.dirpath, '%s.py' % self.file)
                with io.open(work_file, "w", encoding='utf-8') as fp:
                    fp.write(content)
            args = [
                'xgettext',
                '-d', domain,
                '--language=Python',
                '--keyword=gettext_noop',
                '--keyword=gettext_lazy',
                '--keyword=ngettext_lazy:1,2',
                '--keyword=ugettext_noop',
                '--keyword=ugettext_lazy',
                '--keyword=ungettext_lazy:1,2',
                '--keyword=pgettext:1c,2',
                '--keyword=npgettext:1c,2,3',
                '--keyword=pgettext_lazy:1c,2',
                '--keyword=npgettext_lazy:1c,2,3',
                '--output=-'
            ] + command.xgettext_options
            args.append(work_file)
        else:
            return
        msgs, errors, status = gettext_popen_wrapper(args)
        if errors:
            if status != STATUS_OK:
                if is_templatized:
                    os.unlink(work_file)
                raise CommandError(
                    "errors happened while running xgettext on %s\n%s" %
                    (self.file, errors))
            elif command.verbosity > 0:
                # Print warnings
                command.stdout.write(errors)
        if msgs:
            # Write/append messages to pot file
            potfile = os.path.join(self.locale_dir, '%s.pot' % str(domain))
            if is_templatized:
                # Remove '.py' suffix
                if os.name == 'nt':
                    # Preserve '.\' prefix on Windows to respect gettext behavior
                    old = '#: ' + work_file
                    new = '#: ' + orig_file
                else:
                    old = '#: ' + work_file[2:]
                    new = '#: ' + orig_file[2:]
                msgs = msgs.replace(old, new)
            write_pot_file(potfile, msgs)

        if is_templatized:
            os.unlink(work_file)
示例#12
0
def make_messages(locale=None,
                  domain='django',
                  verbosity='1',
                  all=False,
                  extensions=None,
                  symlinks=False,
                  ignore_patterns=[],
                  no_wrap=False,
                  no_obsolete=False):
    """
    Uses the locale directory from the Django SVN tree or an application/
    project to process all
    """
    # Need to ensure that the i18n framework is enabled
    from django.conf import settings
    if settings.configured:
        settings.USE_I18N = True
    else:
        settings.configure(USE_I18N=True)

    from django.utils.translation import templatize

    invoked_for_django = False
    if os.path.isdir(os.path.join('conf', 'locale')):
        localedir = os.path.abspath(os.path.join('conf', 'locale'))
        invoked_for_django = True
        # Ignoring all contrib apps
        ignore_patterns += ['contrib/*']
    elif os.path.isdir('locale'):
        localedir = os.path.abspath('locale')
    else:
        raise CommandError(
            "This script should be run from the Django SVN tree or your project or app tree. If you did indeed run it from the SVN checkout or your project or application, maybe you are just missing the conf/locale (in the django tree) or locale (for project and application) directory? It is not created automatically, you have to create it by hand if you want to enable i18n for your project or application."
        )

    if domain not in ('django', 'djangojs'):
        raise CommandError(
            "currently makemessages only supports domains 'django' and 'djangojs'"
        )

    if (locale is None and not all) or domain is None:
        message = "Type '%s help %s' for usage information." % (
            os.path.basename(sys.argv[0]), sys.argv[1])
        raise CommandError(message)

    # We require gettext version 0.15 or newer.
    output = _popen('xgettext --version')[0]
    match = re.search(r'(?P<major>\d+)\.(?P<minor>\d+)', output)
    if match:
        xversion = (int(match.group('major')), int(match.group('minor')))
        if xversion < (0, 15):
            raise CommandError(
                "Django internationalization requires GNU gettext 0.15 or newer. You are using version %s, please upgrade your gettext toolset."
                % match.group())

    languages = []
    if locale is not None:
        languages.append(locale)
    elif all:
        locale_dirs = filter(os.path.isdir, glob.glob('%s/*' % localedir))
        languages = [os.path.basename(l) for l in locale_dirs]

    wrap = no_wrap and '--no-wrap' or ''

    for locale in languages:
        if verbosity > 0:
            print "processing language", locale
        basedir = os.path.join(localedir, locale, 'LC_MESSAGES')
        if not os.path.isdir(basedir):
            os.makedirs(basedir)

        pofile = os.path.join(basedir, '%s.po' % domain)
        potfile = os.path.join(basedir, '%s.pot' % domain)

        if os.path.exists(potfile):
            os.unlink(potfile)

        for dirpath, file in find_files(".",
                                        ignore_patterns,
                                        verbosity,
                                        symlinks=symlinks):
            file_base, file_ext = os.path.splitext(file)
            if domain == 'djangojs' and file_ext in extensions:
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' %
                                     (file, dirpath))
                src = open(os.path.join(dirpath, file), "rU").read()
                src = prepare_js_for_gettext(src)
                thefile = '%s.c' % file
                f = open(os.path.join(dirpath, thefile), "w")
                try:
                    f.write(src)
                finally:
                    f.close()
                cmd = (
                    'xgettext -d %s -L C %s --keyword=gettext_noop '
                    '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
                    '--keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 '
                    '--from-code UTF-8 --add-comments=Translators -o - "%s"' %
                    (domain, wrap, os.path.join(dirpath, thefile)))
                msgs, errors = _popen(cmd)
                if errors:
                    os.unlink(os.path.join(dirpath, thefile))
                    if os.path.exists(potfile):
                        os.unlink(potfile)
                    raise CommandError(
                        "errors happened while running xgettext on %s\n%s" %
                        (file, errors))
                if msgs:
                    old = '#: ' + os.path.join(dirpath, thefile)[2:]
                    new = '#: ' + os.path.join(dirpath, file)[2:]
                    msgs = msgs.replace(old, new)
                    if os.path.exists(potfile):
                        # Strip the header
                        msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
                    else:
                        msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
                    f = open(potfile, 'ab')
                    try:
                        f.write(msgs)
                    finally:
                        f.close()
                os.unlink(os.path.join(dirpath, thefile))
            elif domain == 'django' and (file_ext == '.py'
                                         or file_ext in extensions):
                thefile = file
                orig_file = os.path.join(dirpath, file)
                if file_ext in extensions:
                    src = open(orig_file, "rU").read()
                    thefile = '%s.py' % file
                    f = open(os.path.join(dirpath, thefile), "w")
                    try:
                        f.write(templatize(src, orig_file[2:]))
                    finally:
                        f.close()
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' %
                                     (file, dirpath))
                cmd = (
                    'xgettext -d %s -L Python %s --keyword=gettext_noop '
                    '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
                    '--keyword=ugettext_noop --keyword=ugettext_lazy '
                    '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
                    '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
                    '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 '
                    '--add-comments=Translators -o - "%s"' %
                    (domain, wrap, os.path.join(dirpath, thefile)))
                msgs, errors = _popen(cmd)
                if errors:
                    if thefile != file:
                        os.unlink(os.path.join(dirpath, thefile))
                    if os.path.exists(potfile):
                        os.unlink(potfile)
                    raise CommandError(
                        "errors happened while running xgettext on %s\n%s" %
                        (file, errors))
                if msgs:
                    if thefile != file:
                        old = '#: ' + os.path.join(dirpath, thefile)[2:]
                        new = '#: ' + orig_file[2:]
                        msgs = msgs.replace(old, new)
                    if os.path.exists(potfile):
                        # Strip the header
                        msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
                    else:
                        msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
                    f = open(potfile, 'ab')
                    try:
                        f.write(msgs)
                    finally:
                        f.close()
                if thefile != file:
                    os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            msgs, errors = _popen('msguniq %s --to-code=utf-8 "%s"' %
                                  (wrap, potfile))
            if errors:
                os.unlink(potfile)
                raise CommandError(
                    "errors happened while running msguniq\n%s" % errors)
            if os.path.exists(pofile):
                f = open(potfile, 'w')
                try:
                    f.write(msgs)
                finally:
                    f.close()
                msgs, errors = _popen('msgmerge %s -q "%s" "%s"' %
                                      (wrap, pofile, potfile))
                if errors:
                    os.unlink(potfile)
                    raise CommandError(
                        "errors happened while running msgmerge\n%s" % errors)
            elif not invoked_for_django:
                msgs = copy_plural_forms(msgs, locale, domain, verbosity)
            msgs = msgs.replace(
                "#. #-#-#-#-#  %s.pot (PACKAGE VERSION)  #-#-#-#-#\n" % domain,
                "")
            f = open(pofile, 'wb')
            try:
                f.write(msgs)
            finally:
                f.close()
            os.unlink(potfile)
            if no_obsolete:
                msgs, errors = _popen(
                    'msgattrib %s -o "%s" --no-obsolete "%s"' %
                    (wrap, pofile, pofile))
                if errors:
                    raise CommandError(
                        "errors happened while running msgattrib\n%s" % errors)
示例#13
0
def process_file(file, dirpath, potfile, domain, verbosity,
                 extensions, wrap, location, stdout=sys.stdout):
    """
    Extract translatable literals from :param file: for :param domain:
    creating or updating the :param potfile: POT file.

    Uses the xgettext GNU gettext utility.
    """

    from django.utils.translation import templatize

    _, file_ext = os.path.splitext(file)

    # DJANGO_CHANGE(cpauya):
    # Handlebars templates must be processed like a Django template but
    # must be saved into `djangojs.po` so we refactor the if conditions below.

    is_file_ext_in_extensions = file_ext in extensions

    is_handlebars = False
    if is_file_ext_in_extensions and 'handlebars' in file_ext:
        is_handlebars = True

    if is_handlebars and domain != 'djangojs':
        stdout.write('You must set the domain to "djangojs" like `-d djangojs` for handlebars templates!')

    # DJANGO_CHANGE(cpauya):
    # Check handlebars extension are processed like a Django template but saved in `djangojs` domain.
    if domain == 'djangojs' and is_file_ext_in_extensions and not is_handlebars:
        is_templatized = True
        orig_file = os.path.join(dirpath, file)
        with open(orig_file) as fp:
            src_data = fp.read()
        src_data = prepare_js_for_gettext(src_data)
        thefile = '%s.c' % file
        work_file = os.path.join(dirpath, thefile)
        with open(work_file, "w") as fp:
            fp.write(src_data)
        cmd = (
            'xgettext -d %s -L C %s %s --keyword=gettext_noop '
            '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
            '--keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 '
            '--from-code UTF-8 --add-comments=Translators -o - "%s"' %
            (domain, wrap, location, work_file))
    # DJANGO_CHANGE(cpauya):
    # Check handlebars extension are processed like a Django template but saved in `djangojs` domain.
    elif (domain == 'django' and (file_ext == '.py' or is_file_ext_in_extensions) and not is_handlebars) or \
            (domain == 'djangojs' and is_handlebars):
        thefile = file
        orig_file = os.path.join(dirpath, file)
        # DJANGO_CHANGE(cpauya):
        is_templatized = is_file_ext_in_extensions
        if is_templatized:
            with open(orig_file, "rU") as fp:
                src_data = fp.read()
            thefile = '%s.py' % file
            content = templatize(src_data, orig_file[2:])
            with open(os.path.join(dirpath, thefile), "w") as fp:
                fp.write(content)
        work_file = os.path.join(dirpath, thefile)
        cmd = (
            'xgettext -d %s -L Python %s %s --keyword=gettext_noop '
            '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
            '--keyword=ugettext_noop --keyword=ugettext_lazy '
            '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
            '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
            '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 '
            '--add-comments=Translators -o - "%s"' %
            (domain, wrap, location, work_file))
    else:
        # KA-LITE-MOD - show when ignoring files!!
        if verbosity > 1:
            stdout.write('ignoring file %s in %s\n' % (file, dirpath))
        return

    # KA-LITE-MOD - only show processing... when you've confirmed you're not ignoring!
    if verbosity > 1:
        stdout.write('processing file %s in %s\n' % (file, dirpath))
    msgs, errors, status = _popen(cmd)

    if errors:
        if status != STATUS_OK:
            if is_templatized:
                os.unlink(work_file)
            if os.path.exists(potfile):
                os.unlink(potfile)
            raise CommandError(
                "errors happened while running xgettext on %s\n%s" %
                (file, errors))
        elif verbosity > 0:
            # Print warnings
            stdout.write(errors)
    if msgs:
        write_pot_file(potfile, msgs, orig_file, work_file, is_templatized)
    if is_templatized:
        os.unlink(work_file)
示例#14
0
def process_file(file,
                 dirpath,
                 potfile,
                 domain,
                 verbosity,
                 extensions,
                 wrap,
                 location,
                 stdout=sys.stdout):
    """
    Extract translatable literals from :param file: for :param domain:
    creating or updating the :param potfile: POT file.

    Uses the xgettext GNU gettext utility.
    """

    from django.utils.translation import templatize

    _, file_ext = os.path.splitext(file)

    # DJANGO_CHANGE(cpauya):
    # Handlebars templates must be processed like a Django template but
    # must be saved into `djangojs.po` so we refactor the if conditions below.

    is_file_ext_in_extensions = file_ext in extensions

    is_handlebars = False
    if is_file_ext_in_extensions and 'handlebars' in file_ext:
        is_handlebars = True

    if is_handlebars and domain != 'djangojs':
        stdout.write(
            'You must set the domain to "djangojs" like `-d djangojs` for handlebars templates!'
        )

    # DJANGO_CHANGE(cpauya):
    # Check handlebars extension are processed like a Django template but saved in `djangojs` domain.
    if domain == 'djangojs' and is_file_ext_in_extensions and not is_handlebars:
        is_templatized = True
        orig_file = os.path.join(dirpath, file)
        with open(orig_file) as fp:
            src_data = fp.read()
        src_data = prepare_js_for_gettext(src_data)
        thefile = '%s.c' % file
        work_file = os.path.join(dirpath, thefile)
        with open(work_file, "w") as fp:
            fp.write(src_data)
        cmd = ('xgettext -d %s -L C %s %s --keyword=gettext_noop '
               '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
               '--keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 '
               '--from-code UTF-8 --add-comments=Translators -o - "%s"' %
               (domain, wrap, location, work_file))
    # DJANGO_CHANGE(cpauya):
    # Check handlebars extension are processed like a Django template but saved in `djangojs` domain.
    elif (domain == 'django' and (file_ext == '.py' or is_file_ext_in_extensions) and not is_handlebars) or \
            (domain == 'djangojs' and is_handlebars):
        thefile = file
        orig_file = os.path.join(dirpath, file)
        # DJANGO_CHANGE(cpauya):
        is_templatized = is_file_ext_in_extensions
        if is_templatized:
            with open(orig_file, "rU") as fp:
                src_data = fp.read()
            thefile = '%s.py' % file
            content = templatize(src_data, orig_file[2:])
            with open(os.path.join(dirpath, thefile), "w") as fp:
                fp.write(content)
        work_file = os.path.join(dirpath, thefile)
        cmd = ('xgettext -d %s -L Python %s %s --keyword=gettext_noop '
               '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
               '--keyword=ugettext_noop --keyword=ugettext_lazy '
               '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
               '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
               '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 '
               '--add-comments=Translators -o - "%s"' %
               (domain, wrap, location, work_file))
    else:
        # KA-LITE-MOD - show when ignoring files!!
        if verbosity > 1:
            stdout.write('ignoring file %s in %s\n' % (file, dirpath))
        return

    # KA-LITE-MOD - only show processing... when you've confirmed you're not ignoring!
    if verbosity > 1:
        stdout.write('processing file %s in %s\n' % (file, dirpath))
    msgs, errors, status = _popen(cmd)

    if errors:
        if status != STATUS_OK:
            if is_templatized:
                os.unlink(work_file)
            if os.path.exists(potfile):
                os.unlink(potfile)
            raise CommandError(
                "errors happened while running xgettext on %s\n%s" %
                (file, errors))
        elif verbosity > 0:
            # Print warnings
            stdout.write(errors)
    if msgs:
        write_pot_file(potfile, msgs, orig_file, work_file, is_templatized)
    if is_templatized:
        os.unlink(work_file)
示例#15
0
def process_file(file,
                 dirpath,
                 potfile,
                 domain,
                 verbosity,
                 extensions,
                 wrap,
                 location,
                 stdout=sys.stdout):
    """
    Extract translatable literals from :param file: for :param domain:
    creating or updating the :param potfile: POT file.

    Uses the xgettext GNU gettext utility.
    """

    from django.utils.translation import templatize

    _, file_ext = os.path.splitext(file)
    if domain == 'djangojs' and file_ext in extensions:
        is_templatized = True
        orig_file = os.path.join(dirpath, file)
        with open(orig_file) as fp:
            src_data = fp.read()
        src_data = prepare_js_for_gettext(src_data)
        thefile = '%s.c' % file
        work_file = os.path.join(dirpath, thefile)
        with open(work_file, "w") as fp:
            fp.write(src_data)
        cmd = ('xgettext -d %s -L C %s %s --keyword=gettext_noop '
               '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
               '--keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 '
               '--from-code UTF-8 --add-comments=Translators -o - "%s"' %
               (domain, wrap, location, work_file))
    elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
        thefile = file
        orig_file = os.path.join(dirpath, file)
        is_templatized = file_ext in extensions
        if is_templatized:
            with open(orig_file, "rU") as fp:
                src_data = fp.read()
            thefile = '%s.py' % file
            content = templatize(src_data, orig_file[2:])
            with open(os.path.join(dirpath, thefile), "w") as fp:
                fp.write(content)
        work_file = os.path.join(dirpath, thefile)
        cmd = ('xgettext -d %s -L Python %s %s --keyword=gettext_noop '
               '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
               '--keyword=ugettext_noop --keyword=ugettext_lazy '
               '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
               '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
               '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 '
               '--add-comments=Translators -o - "%s"' %
               (domain, wrap, location, work_file))
    else:
        # KA-LITE-MOD - show when ignoring files!!
        if verbosity > 1:
            stdout.write('ignoring file %s in %s\n' % (file, dirpath))
        return

    # KA-LITE-MOD - only show processing... when you've confirmed you're not ignoring!
    if verbosity > 1:
        stdout.write('processing file %s in %s\n' % (file, dirpath))
    msgs, errors, status = _popen(cmd)

    if errors:
        if status != STATUS_OK:
            if is_templatized:
                os.unlink(work_file)
            if os.path.exists(potfile):
                os.unlink(potfile)
            raise CommandError(
                "errors happened while running xgettext on %s\n%s" %
                (file, errors))
        elif verbosity > 0:
            # Print warnings
            stdout.write(errors)
    if msgs:
        write_pot_file(potfile, msgs, orig_file, work_file, is_templatized)
    if is_templatized:
        os.unlink(work_file)
示例#16
0
    def process(self, command, potfile, domain, keep_pot=False):
        """
        Extract translatable literals from self.file for :param domain:
        creating or updating the :param potfile: POT file.

        Uses the xgettext GNU gettext utility.
        """

        from django.utils.translation import templatize

        if command.verbosity > 1:
            command.stdout.write("processing file %s in %s\n" % (self.file, self.dirpath))
        _, file_ext = os.path.splitext(self.file)
        if domain == "djangojs" and file_ext in command.extensions:
            is_templatized = True
            orig_file = os.path.join(self.dirpath, self.file)
            with open(orig_file) as fp:
                src_data = fp.read()
            src_data = prepare_js_for_gettext(src_data)
            thefile = "%s.c" % self.file
            work_file = os.path.join(self.dirpath, thefile)
            with open(work_file, "w") as fp:
                fp.write(src_data)
            args = [
                "xgettext",
                "-d",
                domain,
                "--language=C",
                "--keyword=gettext_noop",
                "--keyword=gettext_lazy",
                "--keyword=ngettext_lazy:1,2",
                "--keyword=pgettext:1c,2",
                "--keyword=npgettext:1c,2,3",
                "--from-code=UTF-8",
                "--add-comments=Translators",
                "--output=-",
            ]
            if command.wrap:
                args.append(command.wrap)
            if command.location:
                args.append(command.location)
            args.append(work_file)
        elif domain == "django" and (file_ext == ".py" or file_ext in command.extensions):
            thefile = self.file
            orig_file = os.path.join(self.dirpath, self.file)
            is_templatized = file_ext in command.extensions
            if is_templatized:
                with open(orig_file, "rU") as fp:
                    src_data = fp.read()
                thefile = "%s.py" % self.file
                content = templatize(src_data, orig_file[2:])
                with open(os.path.join(self.dirpath, thefile), "w") as fp:
                    fp.write(content)
            work_file = os.path.join(self.dirpath, thefile)
            args = [
                "xgettext",
                "-d",
                domain,
                "--language=Python",
                "--keyword=gettext_noop",
                "--keyword=gettext_lazy",
                "--keyword=ngettext_lazy:1,2",
                "--keyword=ugettext_noop",
                "--keyword=ugettext_lazy",
                "--keyword=ungettext_lazy:1,2",
                "--keyword=pgettext:1c,2",
                "--keyword=npgettext:1c,2,3",
                "--keyword=pgettext_lazy:1c,2",
                "--keyword=npgettext_lazy:1c,2,3",
                "--from-code=UTF-8",
                "--add-comments=Translators",
                "--output=-",
            ]
            if command.wrap:
                args.append(command.wrap)
            if command.location:
                args.append(command.location)
            args.append(work_file)
        else:
            return
        msgs, errors, status = popen_wrapper(args)
        if errors:
            if status != STATUS_OK:
                if is_templatized:
                    os.unlink(work_file)
                if not keep_pot and os.path.exists(potfile):
                    os.unlink(potfile)
                raise CommandError("errors happened while running xgettext on %s\n%s" % (self.file, errors))
            elif command.verbosity > 0:
                # Print warnings
                command.stdout.write(errors)
        if msgs:
            if is_templatized:
                old = "#: " + work_file[2:]
                new = "#: " + orig_file[2:]
                msgs = msgs.replace(old, new)
            write_pot_file(potfile, msgs)
        if is_templatized:
            os.unlink(work_file)
示例#17
0
def process_file(file, dirpath, potfile, domain, verbosity,
                 extensions, wrap, location, stdout=sys.stdout):
    """
    Extract translatable literals from :param file: for :param domain:
    creating or updating the :param potfile: POT file.

    Uses the xgettext GNU gettext utility.
    """

    from django.utils.translation import templatize

    _, file_ext = os.path.splitext(file)
    if domain == 'djangojs' and file_ext in extensions:
        is_templatized = True
        orig_file = os.path.join(dirpath, file)
        with open(orig_file) as fp:
            src_data = fp.read()
        src_data = prepare_js_for_gettext(src_data)
        thefile = '%s.c' % file
        work_file = os.path.join(dirpath, thefile)
        with open(work_file, "w") as fp:
            fp.write(src_data)
        cmd = (
            'xgettext -d %s -L C %s %s --keyword=gettext_noop '
            '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
            '--keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 '
            '--from-code UTF-8 --add-comments=Translators -o - "%s"' %
            (domain, wrap, location, work_file))
    elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
        thefile = file
        orig_file = os.path.join(dirpath, file)
        is_templatized = file_ext in extensions
        if is_templatized:
            with open(orig_file, "rU") as fp:
                src_data = fp.read()
            thefile = '%s.py' % file
            content = templatize(src_data, orig_file[2:])
            with open(os.path.join(dirpath, thefile), "w") as fp:
                fp.write(content)
        work_file = os.path.join(dirpath, thefile)
        cmd = (
            'xgettext -d %s -L Python %s %s --keyword=gettext_noop '
            '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
            '--keyword=ugettext_noop --keyword=ugettext_lazy '
            '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
            '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
            '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 '
            '--add-comments=Translators -o - "%s"' %
            (domain, wrap, location, work_file))
    else:
        # KA-LITE-MOD - show when ignoring files!!
        if verbosity > 1:
            stdout.write('ignoring file %s in %s\n' % (file, dirpath))
        return

    # KA-LITE-MOD - only show processing... when you've confirmed you're not ignoring!
    if verbosity > 1:
        stdout.write('processing file %s in %s\n' % (file, dirpath))
    msgs, errors, status = _popen(cmd)

    if errors:
        if status != STATUS_OK:
            if is_templatized:
                os.unlink(work_file)
            if os.path.exists(potfile):
                os.unlink(potfile)
            raise CommandError(
                "errors happened while running xgettext on %s\n%s" %
                (file, errors))
        elif verbosity > 0:
            # Print warnings
            stdout.write(errors)
    if msgs:
        write_pot_file(potfile, msgs, orig_file, work_file, is_templatized)
    if is_templatized:
        os.unlink(work_file)
示例#18
0
    def process(self, command, domain):
        """
        Extract translatable literals from self.file for :param domain:,
        creating or updating the POT file.

        Uses the xgettext GNU gettext utility.
        """
        from django.utils.translation import templatize

        if command.verbosity > 1:
            command.stdout.write('processing file %s in %s\n' %
                                 (self.file, self.dirpath))
        file_ext = os.path.splitext(self.file)[1]
        if domain == 'djangojs':
            orig_file = os.path.join(self.dirpath, self.file)
            work_file = orig_file
            is_templatized = command.gettext_version < (0, 18, 3)
            if is_templatized:
                with io.open(orig_file, 'r',
                             encoding=settings.FILE_CHARSET) as fp:
                    src_data = fp.read()
                src_data = prepare_js_for_gettext(src_data)
                work_file = os.path.join(self.dirpath, '%s.c' % self.file)
                with io.open(work_file, "w", encoding='utf-8') as fp:
                    fp.write(src_data)
            args = [
                'xgettext', '-d', domain,
                '--language=%s' % ('C' if is_templatized else 'JavaScript', ),
                '--keyword=gettext_noop', '--keyword=gettext_lazy',
                '--keyword=ngettext_lazy:1,2', '--keyword=pgettext:1c,2',
                '--keyword=npgettext:1c,2,3', '--output=-'
            ] + command.xgettext_options
            args.append(work_file)
        elif domain == 'django':
            orig_file = os.path.join(self.dirpath, self.file)
            work_file = orig_file
            is_templatized = file_ext != '.py'
            if is_templatized:
                with io.open(orig_file, encoding=settings.FILE_CHARSET) as fp:
                    src_data = fp.read()
                content = templatize(src_data, orig_file[2:])
                work_file = os.path.join(self.dirpath, '%s.py' % self.file)
                with io.open(work_file, "w", encoding='utf-8') as fp:
                    fp.write(content)
            args = [
                'xgettext', '-d', domain, '--language=Python',
                '--keyword=gettext_noop', '--keyword=gettext_lazy',
                '--keyword=ngettext_lazy:1,2', '--keyword=ugettext_noop',
                '--keyword=ugettext_lazy', '--keyword=ungettext_lazy:1,2',
                '--keyword=pgettext:1c,2', '--keyword=npgettext:1c,2,3',
                '--keyword=pgettext_lazy:1c,2',
                '--keyword=npgettext_lazy:1c,2,3', '--output=-'
            ] + command.xgettext_options
            args.append(work_file)
        else:
            return
        msgs, errors, status = gettext_popen_wrapper(args)
        if errors:
            if status != STATUS_OK:
                if is_templatized:
                    os.unlink(work_file)
                raise CommandError(
                    "errors happened while running xgettext on %s\n%s" %
                    (self.file, errors))
            elif command.verbosity > 0:
                # Print warnings
                command.stdout.write(errors)
        if msgs:
            # Write/append messages to pot file
            if self.locale_dir is NO_LOCALE_DIR:
                file_path = os.path.normpath(
                    os.path.join(self.dirpath, self.file))
                raise CommandError(
                    "Unable to find a locale path to store translations for file %s"
                    % file_path)
            potfile = os.path.join(self.locale_dir, '%s.pot' % str(domain))
            if is_templatized:
                # Remove '.py' suffix
                if os.name == 'nt':
                    # Preserve '.\' prefix on Windows to respect gettext behavior
                    old = '#: ' + work_file
                    new = '#: ' + orig_file
                else:
                    old = '#: ' + work_file[2:]
                    new = '#: ' + orig_file[2:]
                msgs = msgs.replace(old, new)
            write_pot_file(potfile, msgs)

        if is_templatized:
            os.unlink(work_file)
示例#19
0
def make_messages(locale=None, domain='django', verbosity='1', all=False,
        extensions=None, symlinks=False, ignore_patterns=[], no_wrap=False,
        no_obsolete=False):
    """
    Uses the locale directory from the Django SVN tree or an application/
    project to process all
    """
    # Need to ensure that the i18n framework is enabled
    from django.conf import settings
    if settings.configured:
        settings.USE_I18N = True
    else:
        settings.configure(USE_I18N = True)

    from django.utils.translation import templatize

    invoked_for_django = False
    if os.path.isdir(os.path.join('conf', 'locale')):
        localedir = os.path.abspath(os.path.join('conf', 'locale'))
        invoked_for_django = True
        # Ignoring all contrib apps
        ignore_patterns += ['contrib/*']
    elif os.path.isdir('locale'):
        localedir = os.path.abspath('locale')
    else:
        raise CommandError("This script should be run from the Django SVN tree or your project or app tree. If you did indeed run it from the SVN checkout or your project or application, maybe you are just missing the conf/locale (in the django tree) or locale (for project and application) directory? It is not created automatically, you have to create it by hand if you want to enable i18n for your project or application.")

    if domain not in ('django', 'djangojs'):
        raise CommandError("currently makemessages only supports domains 'django' and 'djangojs'")

    if (locale is None and not all) or domain is None:
        message = "Type '%s help %s' for usage information." % (os.path.basename(sys.argv[0]), sys.argv[1])
        raise CommandError(message)

    # We require gettext version 0.15 or newer.
    output = _popen('xgettext --version')[0]
    match = re.search(r'(?P<major>\d+)\.(?P<minor>\d+)', output)
    if match:
        xversion = (int(match.group('major')), int(match.group('minor')))
        if xversion < (0, 15):
            raise CommandError("Django internationalization requires GNU gettext 0.15 or newer. You are using version %s, please upgrade your gettext toolset." % match.group())

    languages = []
    if locale is not None:
        languages.append(locale)
    elif all:
        locale_dirs = filter(os.path.isdir, glob.glob('%s/*' % localedir))
        languages = [os.path.basename(l) for l in locale_dirs]

    wrap = no_wrap and '--no-wrap' or ''

    for locale in languages:
        if verbosity > 0:
            print "processing language", locale
        basedir = os.path.join(localedir, locale, 'LC_MESSAGES')
        if not os.path.isdir(basedir):
            os.makedirs(basedir)

        pofile = os.path.join(basedir, '%s.po' % domain)
        potfile = os.path.join(basedir, '%s.pot' % domain)

        if os.path.exists(potfile):
            os.unlink(potfile)

        for dirpath, file in find_files(".", ignore_patterns, verbosity, symlinks=symlinks):
            file_base, file_ext = os.path.splitext(file)
            if domain == 'djangojs' and file_ext in extensions:
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                src = open(os.path.join(dirpath, file), "rU").read()
                src = prepare_js_for_gettext(src)
                thefile = '%s.c' % file
                f = open(os.path.join(dirpath, thefile), "w")
                try:
                    f.write(src)
                finally:
                    f.close()
                cmd = (
                    'xgettext -d %s -L C %s --keyword=gettext_noop '
                    '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
                    '--keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 '
                    '--from-code UTF-8 --add-comments=Translators -o - "%s"' % (
                        domain, wrap, os.path.join(dirpath, thefile)
                    )
                )
                msgs, errors = _popen(cmd)
                if errors:
                    os.unlink(os.path.join(dirpath, thefile))
                    if os.path.exists(potfile):
                        os.unlink(potfile)
                    raise CommandError(
                        "errors happened while running xgettext on %s\n%s" %
                        (file, errors))
                if msgs:
                    old = '#: ' + os.path.join(dirpath, thefile)[2:]
                    new = '#: ' + os.path.join(dirpath, file)[2:]
                    msgs = msgs.replace(old, new)
                    if os.path.exists(potfile):
                        # Strip the header
                        msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
                    else:
                        msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
                    f = open(potfile, 'ab')
                    try:
                        f.write(msgs)
                    finally:
                        f.close()
                os.unlink(os.path.join(dirpath, thefile))
            elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
                thefile = file
                orig_file = os.path.join(dirpath, file)
                if file_ext in extensions:
                    src = open(orig_file, "rU").read()
                    thefile = '%s.py' % file
                    f = open(os.path.join(dirpath, thefile), "w")
                    try:
                        f.write(templatize(src, orig_file[2:]))
                    finally:
                        f.close()
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                cmd = (
                    'xgettext -d %s -L Python %s --keyword=gettext_noop '
                    '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
                    '--keyword=ugettext_noop --keyword=ugettext_lazy '
                    '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
                    '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
                    '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 '
                    '--add-comments=Translators -o - "%s"' % (
                        domain, wrap, os.path.join(dirpath, thefile))
                )
                msgs, errors = _popen(cmd)
                if errors:
                    if thefile != file:
                        os.unlink(os.path.join(dirpath, thefile))
                    if os.path.exists(potfile):
                        os.unlink(potfile)
                    raise CommandError(
                        "errors happened while running xgettext on %s\n%s" %
                        (file, errors))
                if msgs:
                    if thefile != file:
                        old = '#: ' + os.path.join(dirpath, thefile)[2:]
                        new = '#: ' + orig_file[2:]
                        msgs = msgs.replace(old, new)
                    if os.path.exists(potfile):
                        # Strip the header
                        msgs = '\n'.join(dropwhile(len, msgs.split('\n')))
                    else:
                        msgs = msgs.replace('charset=CHARSET', 'charset=UTF-8')
                    f = open(potfile, 'ab')
                    try:
                        f.write(msgs)
                    finally:
                        f.close()
                if thefile != file:
                    os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            msgs, errors = _popen('msguniq %s --to-code=utf-8 "%s"' %
                                  (wrap, potfile))
            if errors:
                os.unlink(potfile)
                raise CommandError(
                    "errors happened while running msguniq\n%s" % errors)
            if os.path.exists(pofile):
                f = open(potfile, 'w')
                try:
                    f.write(msgs)
                finally:
                    f.close()
                msgs, errors = _popen('msgmerge %s -q "%s" "%s"' %
                                      (wrap, pofile, potfile))
                if errors:
                    os.unlink(potfile)
                    raise CommandError(
                        "errors happened while running msgmerge\n%s" % errors)
            elif not invoked_for_django:
                msgs = copy_plural_forms(msgs, locale, domain, verbosity)
            msgs = msgs.replace(
                "#. #-#-#-#-#  %s.pot (PACKAGE VERSION)  #-#-#-#-#\n" % domain, "")
            f = open(pofile, 'wb')
            try:
                f.write(msgs)
            finally:
                f.close()
            os.unlink(potfile)
            if no_obsolete:
                msgs, errors = _popen('msgattrib %s -o "%s" --no-obsolete "%s"' %
                                      (wrap, pofile, pofile))
                if errors:
                    raise CommandError(
                        "errors happened while running msgattrib\n%s" % errors)
    def process(self, command, potfile, domain, keep_pot=False):
        """
        Extract translatable literals from self.file for :param domain:
        creating or updating the :param potfile: POT file.

        Uses the xgettext GNU gettext utility.
        """

        from django.utils.translation import templatize

        if command.verbosity > 1:
            command.stdout.write('processing file %s in %s\n' % (self.file, self.dirpath))
        _, file_ext = os.path.splitext(self.file)
        if domain == 'djangojs' and file_ext in command.extensions:
            is_templatized = True
            orig_file = os.path.join(self.dirpath, self.file)
            with open(orig_file) as fp:
                src_data = fp.read()
            src_data = prepare_js_for_gettext(src_data)
            thefile = '%s.c' % self.file
            work_file = os.path.join(self.dirpath, thefile)
            with open(work_file, "w") as fp:
                fp.write(src_data)
            args = [
                'xgettext',
                '-d', domain,
                '--language=C',
                '--keyword=gettext_noop',
                '--keyword=gettext_lazy',
                '--keyword=ngettext_lazy:1,2',
                '--keyword=pgettext:1c,2',
                '--keyword=npgettext:1c,2,3',
                '--from-code=UTF-8',
                '--add-comments=Translators',
                '--output=-'
            ]
            if command.wrap:
                args.append(command.wrap)
            if command.location:
                args.append(command.location)
            args.append(work_file)
        elif domain == 'django' and (file_ext == '.py' or file_ext in command.extensions):
            thefile = self.file
            orig_file = os.path.join(self.dirpath, self.file)
            is_templatized = file_ext in command.extensions
            if is_templatized:
                with open(orig_file, "rU") as fp:
                    src_data = fp.read()
                thefile = '%s.py' % self.file
                content = templatize(src_data, orig_file[2:])
                with open(os.path.join(self.dirpath, thefile), "w") as fp:
                    fp.write(content)
            work_file = os.path.join(self.dirpath, thefile)
            args = [
                'xgettext',
                '-d', domain,
                '--language=Python',
                '--keyword=gettext_noop',
                '--keyword=gettext_lazy',
                '--keyword=ngettext_lazy:1,2',
                '--keyword=ugettext_noop',
                '--keyword=ugettext_lazy',
                '--keyword=ungettext_lazy:1,2',
                '--keyword=pgettext:1c,2',
                '--keyword=npgettext:1c,2,3',
                '--keyword=pgettext_lazy:1c,2',
                '--keyword=npgettext_lazy:1c,2,3',
                '--from-code=UTF-8',
                '--add-comments=Translators',
                '--output=-'
            ]
            if command.wrap:
                args.append(command.wrap)
            if command.location:
                args.append(command.location)
            args.append(work_file)
        else:
            return
        msgs, errors, status = popen_wrapper(args)
        if errors:
            if status != STATUS_OK:
                if is_templatized:
                    os.unlink(work_file)
                if not keep_pot and os.path.exists(potfile):
                    os.unlink(potfile)
                raise CommandError(
                    "errors happened while running xgettext on %s\n%s" %
                    (self.file, errors))
            elif command.verbosity > 0:
                # Print warnings
                command.stdout.write(errors)
        if msgs:
            if is_templatized:
                # Remove '.py' suffix
                if os.name =='nt':
                    # Preserve '.\' prefix on Windows to respect gettext behavior
                    old = '#: ' + work_file
                    new = '#: ' + orig_file
                else:
                    old = '#: ' + work_file[2:]
                    new = '#: ' + orig_file[2:]
                msgs = msgs.replace(old, new)
            write_pot_file(potfile, msgs)
        if is_templatized:
            os.unlink(work_file)
 def test_func(self):
     self.assertEqual(prepare_js_for_gettext(js), c)