Beispiel #1
0
 def build_potfiles(self):
     """
     Build pot files and apply msguniq to them.
     """
     file_list = self.find_files(".")
     self.remove_potfiles()
     self.process_files(file_list)
     potfiles = []
     for path in self.locale_paths:
         potfile = os.path.join(path, '%s.pot' % self.domain)
         if not os.path.exists(potfile):
             continue
         args = ['msguniq'] + self.msguniq_options + [potfile]
         msgs, errors, status = popen_wrapper(args)
         if errors:
             if status != STATUS_OK:
                 raise CommandError(
                     "errors happened while running msguniq\n%s" % errors)
             elif self.verbosity > 0:
                 self.stdout.write(errors)
         msgs = normalize_eols(msgs)
         with open(potfile, 'w', encoding='utf-8') as fp:
             fp.write(msgs)
         potfiles.append(potfile)
     return potfiles
Beispiel #2
0
    def write_po_file(self, potfile, locale):
        """
        Create or update the PO file for self.domain and `locale`.
        Use contents of the existing `potfile`.

        Use msgmerge and msgattrib GNU gettext utilities.
        """
        basedir = os.path.join(os.path.dirname(potfile), locale, 'LC_MESSAGES')
        if not os.path.isdir(basedir):
            os.makedirs(basedir)
        pofile = os.path.join(basedir, '%s.po' % self.domain)

        if os.path.exists(pofile):
            args = ['msgmerge'] + self.msgmerge_options + [pofile, potfile]
            msgs, errors, status = popen_wrapper(args)
            if errors:
                if status != STATUS_OK:
                    raise CommandError(
                        "errors happened while running msgmerge\n%s" % errors)
                elif self.verbosity > 0:
                    self.stdout.write(errors)
        else:
            with open(potfile, 'r', encoding='utf-8') as fp:
                msgs = fp.read()
            if not self.invoked_for_django:
                msgs = self.copy_plural_forms(msgs, locale)
        msgs = normalize_eols(msgs)
        msgs = msgs.replace(
            "#. #-#-#-#-#  %s.pot (PACKAGE VERSION)  #-#-#-#-#\n" %
            self.domain, "")
        with open(pofile, 'w', encoding='utf-8') as fp:
            fp.write(msgs)

        if self.no_obsolete:
            args = ['msgattrib'
                    ] + self.msgattrib_options + ['-o', pofile, pofile]
            msgs, errors, status = popen_wrapper(args)
            if errors:
                if status != STATUS_OK:
                    raise CommandError(
                        "errors happened while running msgattrib\n%s" % errors)
                elif self.verbosity > 0:
                    self.stdout.write(errors)
Beispiel #3
0
 def gettext_version(self):
     # Gettext tools will output system-encoded bytestrings instead of UTF-8,
     # when looking up the version. It's especially a problem on Windows.
     out, err, status = popen_wrapper(
         ['xgettext', '--version'],
         stdout_encoding=DEFAULT_LOCALE_ENCODING,
     )
     m = re.search(r'(\d+)\.(\d+)\.?(\d+)?', out)
     if m:
         return tuple(int(d) for d in m.groups() if d is not None)
     else:
         raise CommandError(
             "Unable to get gettext version. Is it installed?")
Beispiel #4
0
    def process_locale_dir(self, locale_dir, files):
        """
        Extract translatable literals from the specified files, creating or
        updating the POT file for a given locale directory.

        Use the xgettext GNU gettext utility.
        """
        build_files = []
        for translatable in files:
            if self.verbosity > 1:
                self.stdout.write('processing file %s in %s\n' %
                                  (translatable.file, translatable.dirpath))
            if self.domain not in ('djangojs', 'djmodels'):
                continue
            build_file = self.build_file_class(self, self.domain, translatable)
            try:
                build_file.preprocess()
            except UnicodeDecodeError as e:
                self.stdout.write(
                    'UnicodeDecodeError: skipped file %s in %s (reason: %s)' %
                    (
                        translatable.file,
                        translatable.dirpath,
                        e,
                    ))
                continue
            build_files.append(build_file)

        if self.domain == 'djangojs':
            is_templatized = build_file.is_templatized
            args = [
                'xgettext',
                '-d',
                self.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=-',
            ]
        elif self.domain == 'djmodels':
            args = [
                'xgettext',
                '-d',
                self.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=-',
            ]
        else:
            return

        input_files = [bf.work_path for bf in build_files]
        with NamedTemporaryFile(mode='w+') as input_files_list:
            input_files_list.write(('\n'.join(input_files)))
            input_files_list.flush()
            args.extend(['--files-from', input_files_list.name])
            args.extend(self.xgettext_options)
            msgs, errors, status = popen_wrapper(args)

        if errors:
            if status != STATUS_OK:
                for build_file in build_files:
                    build_file.cleanup()
                raise CommandError(
                    'errors happened while running xgettext on %s\n%s' %
                    ('\n'.join(input_files), errors))
            elif self.verbosity > 0:
                # Print warnings
                self.stdout.write(errors)

        if msgs:
            if locale_dir is NO_LOCALE_DIR:
                file_path = os.path.normpath(build_files[0].path)
                raise CommandError(
                    'Unable to find a locale path to store translations for '
                    'file %s' % file_path)
            for build_file in build_files:
                msgs = build_file.postprocess_messages(msgs)
            potfile = os.path.join(locale_dir, '%s.pot' % self.domain)
            write_pot_file(potfile, msgs)

        for build_file in build_files:
            build_file.cleanup()
Beispiel #5
0
 def test_no_existent_external_program(self):
     msg = 'Error executing a_42_command_that_doesnt_exist_42'
     with self.assertRaisesMessage(CommandError, msg):
         popen_wrapper(['a_42_command_that_doesnt_exist_42'])