Exemple #1
0
    def test_templatize(self):
        from django.utils.translation import templatize

        # test regular Django tags
        output = templatize(
            '{% trans "Foo" %}{% blocktrans %}\nBar\n{% endblocktrans %}',
            origin='test.html')
        self.assertRegex(output, r"gettext\(u?'Foo'\)")
        self.assertRegex(output, r"gettext\(u?'\\nBar\\n'\)")

        # test Haml tags with HTML origin
        output = templatize('- trans "Foo"\n- blocktrans\n  Bar\n',
                            origin='test.haml')
        self.assertRegex(output, r"gettext\(u?'Foo'\)")
        self.assertRegex(output, r"gettext\(u?'\\n  Bar\\n'\)")

        # test Haml tags and HTML origin
        self.assertNotIn(
            'gettext',
            templatize('- trans "Foo"\n- blocktrans\n  Bar\n',
                       origin='test.html'))

        # test Haml tags and no origin
        self.assertNotIn('gettext',
                         templatize('- trans "Foo"\n- blocktrans\n  Bar\n'))
Exemple #2
0
 def copy_template_file(self, path):
     path = os.path.relpath(path, settings.PROJECT_ROOT)
     dest_path = os.path.join(self.tempdir, path)
     dest_dir = os.path.dirname(dest_path)
     if not os.path.exists(dest_dir):
         os.makedirs(dest_dir)
     with open(dest_path, 'w') as f_out:
         with open(path, 'r') as f_in:
             f_out.write(templatize(f_in.read()))
Exemple #3
0
def extract_template(fileobj, keywords, comment_tags, options):
    src = force_text(fileobj.read(), settings.FILE_CHARSET)
    if fileobj.name.endswith(".pug"):
        src = process(src, compiler=Compiler)
    src = templatize(src, "")
    if "gettext" in src:
        src = re.sub(r"\n\s+", "\n", src)  # Remove indentation
        return extract_python(io.StringIO(src.encode("utf8")), keywords, comment_tags, options)
    return ()
Exemple #4
0
def extract_template(fileobj, keywords, comment_tags, options):
    src = force_text(fileobj.read(), settings.FILE_CHARSET)
    if fileobj.name.endswith(".pug"):
        src = process(src, compiler=Compiler)
    src = templatize(src, "")
    if "gettext" in src:
        src = re.sub(r"\n\s+", "\n", src)  # Remove indentation
        return extract_python(io.StringIO(src.encode("utf8")), keywords,
                              comment_tags, options)
    return ()
Exemple #5
0
    def handle(self, *args, **options):
        custom_dir = path.join(self.BASE_DIR, 'search', 'templates',
                               'snippets', 'custom', options['search'])
        output_path = path.join(self.BASE_DIR, 'locale', 'fr', 'LC_MESSAGES',
                                options['search'] + '.po')
        temp_dir = TemporaryDirectory(dir=path.join(
            self.BASE_DIR, 'search', 'templates', 'snippets', 'custom'))
        template_files = []
        for dirpath, dirnames, filenames in walk(custom_dir,
                                                 topdown=True,
                                                 onerror=None,
                                                 followlinks=False):
            for filename in filenames:
                file_path = path.normpath(path.join(dirpath, filename))
                file_ext = path.splitext(filename)[1]
                if file_ext in ['.html', '.py', '.txt']:
                    with open(file_path, encoding='utf-8') as fp:
                        src_data = fp.read()
                        rc = templatize(src_data, origin=dirpath)
                        temp_file = path.join(temp_dir.name, filename)
                        with open(temp_file, 'w', encoding='utf-8') as ofp:
                            ofp.write(rc)
                            template_files.append(temp_file)

        args = [
            'xgettext', '-d', options['search'], '--language=Python',
            '--no-wrap', '--from-code=utf-8', '--sort-by-file',
            '--copyright-holder="Government of Canada, Gouvernement du Canada"',
            '--package-name="Open Canada Search - {0} Module"'.format(
                options['search']), '--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', '-o', output_path
        ]
        #
        if path.exists(output_path):
            args += ['--join-existing']
        args += template_files
        rc = popen_wrapper(args)
        if rc[2] != 0:
            print(rc[1])
        else:
            print("Created {0}.po".format(options['search']))

        temp_dir.cleanup()
Exemple #6
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)
Exemple #7
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)
Exemple #8
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)
Exemple #9
0
    def xgettext(self, template):
        """Extracts to be translated strings from template and turns it into po format."""
        
        cmd = 'xgettext -d django -L Python --keyword=gettext_noop \
            --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code=UTF-8 \
            --output=- -'

        p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        (msg, err) = p.communicate(input=templatize(template))
        
        if err:
            # dont raise exception, some stuff in stderr are just warmings
            logging.warning(err)

        if XGETTEXT_REENCODES_UTF8:
            return msg.decode('utf-8').encode('iso-8859-1')
                    
        return msg
Exemple #10
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)
Exemple #11
0
    def _process_file(file_name, dir_path, domain, extensions):
        """
        Extract translatable literals from :param file_name: for :param domain:.

        Uses the xgettext GNU gettext utility.
        """

        from django.utils.translation import templatize

        _, file_ext = os.path.splitext(file_name)
        if domain == 'djangojs' and file_ext in extensions:
            has_work_file = True

            # read input file
            with open(os.path.join(dir_path, file_name)) as fp:
                src_data = fp.read()

            # transform
            src_data = prepare_js_for_gettext(src_data)

            # write work file
            work_file = os.path.join(dir_path, '%s.c' % file_name)
            with open(work_file, "w") as fp:
                fp.write(src_data)

            gettext_language = "C"
            gettext_keywords = ['gettext_noop', 'gettext_lazy', 'gettext_lazy:1,2', 'pgettext:1c,2', 'npgettext:1c,2,3']

        elif domain == "django" and (file_ext == '.py' or file_ext in extensions):
            if file_ext in extensions:
                has_work_file =  True

                # read input
                with open(os.path.join(dir_path, file_name), "rU") as fp:
                    src_data = fp.read()

                # transform
                content = templatize(src_data, os.path.join(dir_path, file_name))

                # write work file
                work_file = os.path.join(dir_path, '%s.py' % file_name)
                with open(work_file, "w") as fp:
                    fp.write(content)
            else:
                # directly use input as work file (no transformation necessary)
                has_work_file = False

                work_file = os.path.join(dir_path, file_name)

            gettext_language = "Python"
            gettext_keywords = ['gettext_noop', 'gettext_lazy', 'ngettext_lazy:1,2', 'ugettext_noop', 'ugettext_lazy', 'ungettext_lazy:1,2', 'pgettext:1c,2', 'npgettext:1c,2,3', 'pgettext_lazy:1c,2', 'npgettext_lazy:1c,2,3']
        else:
            return []

        # run gettext
        msgs, errors, status = Command._popen('xgettext -d %s -L %s --no-wrap --no-location %s --from-code UTF-8 --add-comments=Translators -o - "%s"' % (domain, gettext_language, " ".join(["--keyword=%s" % k for k in gettext_keywords]) , work_file))

        # clean up
        if has_work_file:
            os.remove(work_file)

        # fail on error
        if errors and status != 0:
                raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))

        # parse result and return the keys
        return [entry.msgid for entry in pofile(msgs)]
Exemple #12
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)
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()
        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 JavaScript %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)
Exemple #14
0
def make_messages(locale=None, domain='django', verbosity='1', all=False, extensions=None):
    """
    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

    if os.path.isdir(os.path.join('conf', 'locale')):
        localedir = os.path.abspath(os.path.join('conf', 'locale'))
    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:
        # backwards compatible error message
        if not sys.argv[0].endswith("make-messages.py"):
            message = "Type '%s help %s' for usage.\n" % (os.path.basename(sys.argv[0]), sys.argv[1])
        else:
            message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
        raise CommandError(message)

    # We require gettext version 0.15 or newer.
    p = Popen('xgettext --version', shell=True, stdout=PIPE, stderr=PIPE)
    match = re.search(r'(?P<major>\d+)\.(?P<minor>\d+)', p.stdout.read())
    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]

    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)

        all_files = []
        for (dirpath, dirnames, filenames) in os.walk("."):
            all_files.extend([(dirpath, f) for f in filenames])
        all_files.sort()
        for dirpath, file in all_files:
            file_base, file_ext = os.path.splitext(file)
            if domain == 'djangojs' and file_ext == '.js':
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                src = open(os.path.join(dirpath, file), "rU").read()
                src = pythonize_re.sub('\n#', src)
                thefile = '%s.py' % file
                open(os.path.join(dirpath, thefile), "w").write(src)
                cmd = 'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (domain, os.path.join(dirpath, thefile))
                p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
                msgs = p.stdout.read()
                errors = p.stderr.read()
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
                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')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                os.unlink(os.path.join(dirpath, thefile))
            elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
                thefile = file
                if file_ext in extensions:
                    src = open(os.path.join(dirpath, file), "rU").read()
                    thefile = '%s.py' % file
                    try:
                        open(os.path.join(dirpath, thefile), "w").write(templatize(src))
                    except SyntaxError, msg:
                        msg = "%s (file: %s)" % (msg, os.path.join(dirpath, file))
                        raise SyntaxError(msg)
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (
                    domain, os.path.join(dirpath, thefile))
                p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
                msgs = p.stdout.read()
                errors = p.stderr.read()
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))

                if thefile != file:
                    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')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                if thefile != file:
                    os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            p = Popen('msguniq --to-code=utf-8 "%s"' % potfile, shell=True, stdout=PIPE, stderr=PIPE)
            msgs = p.stdout.read()
            errors = p.stderr.read()
            if errors:
                raise CommandError("errors happened while running msguniq\n%s" % errors)
            open(potfile, 'w').write(msgs)
            if os.path.exists(pofile):
                p = Popen('msgmerge -q "%s" "%s"' % (pofile, potfile), shell=True, stdout=PIPE, stderr=PIPE)
                msgs = p.stdout.read()
                errors = p.stderr.read()
                if errors:
                    raise CommandError("errors happened while running msgmerge\n%s" % errors)
            open(pofile, 'wb').write(msgs)
            os.unlink(potfile)
Exemple #15
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:
        # backwards compatible error message
        if not sys.argv[0].endswith("make-messages.py"):
            message = "Type '%s help %s' for usage.\n" % (os.path.basename(sys.argv[0]), sys.argv[1])
        else:
            message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
        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 = pythonize_re.sub('\n#', src)
                thefile = '%s.py' % file
                f = open(os.path.join(dirpath, thefile), "w")
                try:
                    f.write(src)
                finally:
                    f.close()
                cmd = (
                    'xgettext -d %s -L Perl %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:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
                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')
                if msgs:
                    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:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))

                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')
                if msgs:
                    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:
                raise CommandError("errors happened while running msguniq\n%s" % errors)
            f = open(potfile, 'w')
            try:
                f.write(msgs)
            finally:
                f.close()
            if os.path.exists(pofile):
                msgs, errors = _popen('msgmerge %s -q "%s" "%s"' %
                                      (wrap, pofile, potfile))
                if errors:
                    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 make_messages(locale=None, domain='django', verbosity='1', all=False,
        extensions=None, symlinks=False, ignore_patterns=[], no_wrap=False, root_dirs=[]):
    """
    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
    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:
        # backwards compatible error message
        if not sys.argv[0].endswith("make-messages.py"):
            message = "Type '%s help %s' for usage.\n" % (os.path.basename(sys.argv[0]), sys.argv[1])
        else:
            message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
        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(root_dirs, 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 = pythonize_re.sub('\n#', src)
                thefile = '%s.py' % file
                f = open(os.path.join(dirpath, thefile), "w")
                try:
                    f.write(src)
                finally:
                    f.close()
                cmd = (
                    'xgettext -d %s -L Perl %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 -o - "%s"' % (
                        domain, wrap, os.path.join(dirpath, thefile)
                    )
                )
                msgs, errors = _popen(cmd)
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
                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')
                if msgs:
                    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
                if file_ext in extensions:
                    src = open(os.path.join(dirpath, file), "rU").read()
                    thefile = '%s.py' % file
                    try:
                        f = open(os.path.join(dirpath, thefile), "w")
                        try:
                            #---------- PATHCED: process gettext in jinja templates properly  ----------------
                            normalized_filename = os.path.abspath(os.path.join(dirpath, file))
                            for template_dir in settings.TEMPLATE_DIRS:
                                if normalized_filename.startswith(template_dir):
                                    normalized_filename = normalized_filename.replace(template_dir + '/', '')
                                    break
                            jinja = True
                            for skip_path in getattr(settings, 'KEEP_DJANGO_TEMPLATES', ()):
                                if normalized_filename.startswith(skip_path):
                                    jinja = False
                                    break
                            if jinja:
                                #patched templatize implementation works with jinja compiler
                                line = 1
                                for trans in list(extract_from_ast(jinja_environment.parse(src.decode('utf-8')))):
                                    f.write((trans[0] - line) * '.\n' ) #seek to line
                                    f.write(' {{ %s(%s) }} ' % (trans[1],repr(trans[2])))
                                    line = trans[0]
                            else:
                                f.write(templatize(src))
                            # -------------- END OF PATCH -----------
                        finally:
                            f.close()
                    except SyntaxError, msg:
                        msg = "%s (file: %s)" % (msg, os.path.join(dirpath, file))
                        raise SyntaxError(msg)
                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 -o - '
                    '"%s"' % (domain, wrap, os.path.join(dirpath, thefile))
                )
                msgs, errors = _popen(cmd)
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))

                if thefile != file:
                    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')
                if msgs:
                    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:
                raise CommandError("errors happened while running msguniq\n%s" % errors)
            f = open(potfile, 'w')
            try:
                f.write(msgs)
            finally:
                f.close()
            if os.path.exists(pofile):
                msgs, errors = _popen('msgmerge %s -q "%s" "%s"' %
                                      (wrap, pofile, potfile))
                if errors:
                    raise CommandError("errors happened while running msgmerge\n%s" % errors)
            elif not invoked_for_django:
                msgs = copy_plural_forms(msgs, locale, domain, verbosity)
            f = open(pofile, 'wb')
            try:
                f.write(msgs)
            finally:
                f.close()
            os.unlink(potfile)
Exemple #17
0
    def _process_file(file_name, dir_path, domain, extensions):
        """
        Extract translatable literals from :param file_name: for :param domain:.

        Uses the xgettext GNU gettext utility.
        """

        from django.utils.translation import templatize

        _, file_ext = os.path.splitext(file_name)
        if domain == 'djangojs' and file_ext in extensions:
            has_work_file = True

            # read input file
            with open(os.path.join(dir_path, file_name)) as fp:
                src_data = fp.read()

            # transform
            src_data = prepare_js_for_gettext(src_data)

            # write work file
            work_file = os.path.join(dir_path, '%s.c' % file_name)
            with open(work_file, "w") as fp:
                fp.write(src_data)

            gettext_language = "C"
            gettext_keywords = ['gettext_noop', 'gettext_lazy', 'gettext_lazy:1,2', 'pgettext:1c,2', 'npgettext:1c,2,3']

        elif domain == "django" and (file_ext == '.py' or file_ext in extensions):
            if file_ext in extensions:
                has_work_file =  True

                # read input
                with open(os.path.join(dir_path, file_name), "rU") as fp:
                    src_data = fp.read()

                # transform
                content = templatize(src_data, os.path.join(dir_path, file_name))

                # write work file
                work_file = os.path.join(dir_path, '%s.py' % file_name)
                with open(work_file, "w") as fp:
                    fp.write(content)
            else:
                # directly use input as work file (no transformation necessary)
                has_work_file = False

                work_file = os.path.join(dir_path, file_name)

            gettext_language = "Python"
            gettext_keywords = ['gettext_noop', 'gettext_lazy', 'ngettext_lazy:1,2', 'ugettext_noop', 'ugettext_lazy', 'ungettext_lazy:1,2', 'pgettext:1c,2', 'npgettext:1c,2,3', 'pgettext_lazy:1c,2', 'npgettext_lazy:1c,2,3']
        else:
            return []

        # run gettext
        msgs, errors, status = Command._popen('xgettext -d %s -L %s --no-wrap --no-location %s --from-code UTF-8 --add-comments=Translators -o - "%s"' % (domain, gettext_language, " ".join(["--keyword=%s" % k for k in gettext_keywords]) , work_file))

        # clean up
        if has_work_file:
            os.remove(work_file)

        # fail on error
        if errors and status != 0:
                raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))

        # parse result and return the keys
        return [entry.msgid for entry in pofile(msgs)]
Exemple #18
0
def make_messages():
    localedir = None

    if os.path.isdir(os.path.join("conf", "locale")):
        localedir = os.path.abspath(os.path.join("conf", "locale"))
    elif os.path.isdir("locale"):
        localedir = os.path.abspath("locale")
    else:
        print "This script should be run from the django svn tree or your project or app tree."
        print "If you did indeed run it from the svn checkout or your project or application,"
        print "maybe you are just missing the conf/locale (in the django tree) or locale (for project"
        print "and application) directory?"
        print "make-messages.py doesn't create it automatically, you have to create it by hand if"
        print "you want to enable i18n for your project or application."
        sys.exit(1)

    (opts, args) = getopt.getopt(sys.argv[1:], "l:d:va")

    lang = None
    domain = "django"
    verbose = False
    all = False

    for o, v in opts:
        if o == "-l":
            lang = v
        elif o == "-d":
            domain = v
        elif o == "-v":
            verbose = True
        elif o == "-a":
            all = True

    if domain not in ("django", "djangojs"):
        print "currently make-messages.py only supports domains 'django' and 'djangojs'"
        sys.exit(1)
    if (lang is None and not all) or domain is None:
        print "usage: make-messages.py -l <language>"
        print "   or: make-messages.py -a"
        sys.exit(1)

    languages = []

    if lang is not None:
        languages.append(lang)
    elif all:
        languages = [el for el in os.listdir(localedir) if not el.startswith(".")]

    for lang in languages:

        print "processing language", lang
        basedir = os.path.join(localedir, lang, "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, dirnames, filenames) in os.walk("."):
            for file in filenames:
                if domain == "djangojs" and file.endswith(".js"):
                    if verbose:
                        sys.stdout.write("processing file %s in %s\n" % (file, dirpath))
                    src = open(os.path.join(dirpath, file), "rb").read()
                    src = pythonize_re.sub("\n#", src)
                    open(os.path.join(dirpath, "%s.py" % file), "wb").write(src)
                    thefile = "%s.py" % file
                    cmd = (
                        'xgettext %s -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"'
                        % (os.path.exists(potfile) and "--omit-header" or "", domain, os.path.join(dirpath, thefile))
                    )
                    (stdin, stdout, stderr) = os.popen3(cmd, "t")
                    msgs = stdout.read()
                    errors = stderr.read()
                    if errors:
                        print "errors happened while running xgettext on %s" % file
                        print errors
                        sys.exit(8)
                    old = "#: " + os.path.join(dirpath, thefile)[2:]
                    new = "#: " + os.path.join(dirpath, file)[2:]
                    msgs = msgs.replace(old, new)
                    if msgs:
                        open(potfile, "ab").write(msgs)
                    os.unlink(os.path.join(dirpath, thefile))
                elif domain == "django" and (file.endswith(".py") or file.endswith(".html")):
                    thefile = file
                    if file.endswith(".html"):
                        src = open(os.path.join(dirpath, file), "rb").read()
                        thefile = "%s.py" % file
                        open(os.path.join(dirpath, thefile), "wb").write(templatize(src))
                    if verbose:
                        sys.stdout.write("processing file %s in %s\n" % (file, dirpath))
                    cmd = (
                        'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"'
                        % (domain, os.path.join(dirpath, thefile))
                    )
                    (stdin, stdout, stderr) = os.popen3(cmd, "t")
                    msgs = stdout.read()
                    errors = stderr.read()
                    if errors:
                        print "errors happened while running xgettext on %s" % file
                        print errors
                        sys.exit(8)
                    if thefile != file:
                        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")
                    if msgs:
                        open(potfile, "ab").write(msgs)
                    if thefile != file:
                        os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            (stdin, stdout, stderr) = os.popen3('msguniq --to-code=utf-8 "%s"' % potfile, "b")
            msgs = stdout.read()
            errors = stderr.read()
            if errors:
                print "errors happened while running msguniq"
                print errors
                sys.exit(8)
            open(potfile, "w").write(msgs)
            if os.path.exists(pofile):
                (stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), "b")
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    print "errors happened while running msgmerge"
                    print errors
                    sys.exit(8)
            open(pofile, "wb").write(msgs)
            os.unlink(potfile)
Exemple #19
0
def make_messages():
    localedir = None

    if os.path.isdir(os.path.join('conf', 'locale')):
        localedir = os.path.abspath(os.path.join('conf', 'locale'))
    elif os.path.isdir('locale'):
        localedir = os.path.abspath('locale')
    else:
        print "This script should be run from the django svn tree or your project or app tree."
        print "If you did indeed run it from the svn checkout or your project or application,"
        print "maybe you are just missing the conf/locale (in the django tree) or locale (for project"
        print "and application) directory?"
        print "make-messages.py doesn't create it automatically, you have to create it by hand if"
        print "you want to enable i18n for your project or application."
        sys.exit(1)

    (opts, args) = getopt.getopt(sys.argv[1:], 'l:d:va')

    lang = None
    domain = 'django'
    verbose = False
    all = False

    for o, v in opts:
        if o == '-l':
            lang = v
        elif o == '-d':
            domain = v
        elif o == '-v':
            verbose = True
        elif o == '-a':
            all = True

    if domain not in ('django', 'djangojs'):
        print "currently make-messages.py only supports domains 'django' and 'djangojs'"
        sys.exit(1)
    if (lang is None and not all) or domain is None:
        print "usage: make-messages.py -l <language>"
        print "   or: make-messages.py -a"
        sys.exit(1)

    languages = []

    if lang is not None:
        languages.append(lang)
    elif all:
        languages = [el for el in os.listdir(localedir) if not el.startswith('.')]

    for lang in languages:

        print "processing language", lang
        basedir = os.path.join(localedir, lang, '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, dirnames, filenames) in os.walk("."):
            for file in filenames:
                if domain == 'djangojs' and file.endswith('.js'):
                    if verbose: sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                    src = open(os.path.join(dirpath, file), "rb").read()
                    src = pythonize_re.sub('\n#', src)
                    open(os.path.join(dirpath, '%s.py' % file), "wb").write(src)
                    thefile = '%s.py' % file
                    cmd = 'xgettext %s -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy --from-code UTF-8 -o - "%s"' % (
                        os.path.exists(potfile) and '--omit-header' or '', domain, os.path.join(dirpath, thefile))
                    (stdin, stdout, stderr) = os.popen3(cmd, 'b')
                    msgs = stdout.read()
                    errors = stderr.read()
                    if errors:
                        print "errors happened while running xgettext on %s" % file
                        print errors
                        sys.exit(8)
                    old = '#: '+os.path.join(dirpath, thefile)[2:]
                    new = '#: '+os.path.join(dirpath, file)[2:]
                    msgs = msgs.replace(old, new)
                    if msgs:
                        open(potfile, 'ab').write(msgs)
                    os.unlink(os.path.join(dirpath, thefile))
                elif domain == 'django' and (file.endswith('.py') or file.endswith('.html')):
                    thefile = file
                    if file.endswith('.html'):
                        src = open(os.path.join(dirpath, file), "rb").read()
                        open(os.path.join(dirpath, '%s.py' % file), "wb").write(templatize(src))
                        thefile = '%s.py' % file
                    if verbose: sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                    cmd = 'xgettext %s -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy --from-code UTF-8 -o - "%s"' % (
                        os.path.exists(potfile) and '--omit-header' or '', domain, os.path.join(dirpath, thefile))
                    (stdin, stdout, stderr) = os.popen3(cmd, 'b')
                    msgs = stdout.read()
                    errors = stderr.read()
                    if errors:
                        print "errors happened while running xgettext on %s" % file
                        print errors
                        sys.exit(8)
                    if thefile != file:
                        old = '#: '+os.path.join(dirpath, thefile)[2:]
                        new = '#: '+os.path.join(dirpath, file)[2:]
                        msgs = msgs.replace(old, new)
                    if msgs:
                        open(potfile, 'ab').write(msgs)
                    if thefile != file:
                        os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            (stdin, stdout, stderr) = os.popen3('msguniq "%s"' % potfile, 'b')
            msgs = stdout.read()
            errors = stderr.read()
            if errors:
                print "errors happened while running msguniq"
                print errors
                sys.exit(8)
            open(potfile, 'w').write(msgs)
            if os.path.exists(pofile):
                (stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), 'b')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    print "errors happened while running msgmerge"
                    print errors
                    sys.exit(8)
            open(pofile, 'wb').write(msgs)
            os.unlink(potfile)
Exemple #20
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)
Exemple #21
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)
def make_messages(locale=None, domain='django', verbosity='1', all=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

    if os.path.isdir(os.path.join('conf', 'locale')):
        localedir = os.path.abspath(os.path.join('conf', 'locale'))
    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:
        # backwards compatible error message
        if not sys.argv[0].endswith("make-messages.py"):
            message = "Type '%s help %s' for usage.\n" % (os.path.basename(sys.argv[0]), sys.argv[1])
        else:
            message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
        raise CommandError(message)

    languages = []
    if locale is not None:
        languages.append(locale)
    elif all:
        languages = [el for el in os.listdir(localedir) if not el.startswith('.')]

    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)

        all_files = []
        for (dirpath, dirnames, filenames) in os.walk("."):
            all_files.extend([(dirpath, f) for f in filenames])
        all_files.sort()
        for dirpath, file in all_files:
            if domain == 'djangojs' and file.endswith('.js'):
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                src = open(os.path.join(dirpath, file), "rb").read()
                src = pythonize_re.sub('\n#', src)
                open(os.path.join(dirpath, '%s.py' % file), "wb").write(src)
                thefile = '%s.py' % file
                cmd = 'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (domain, os.path.join(dirpath, thefile))
                (stdin, stdout, stderr) = os.popen3(cmd, 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
                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')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                os.unlink(os.path.join(dirpath, thefile))
            elif domain == 'django' and (file.endswith('.py') or file.endswith('.html')):
                thefile = file
                if file.endswith('.html'):
                    src = open(os.path.join(dirpath, file), "rb").read()
                    thefile = '%s.py' % file
                    open(os.path.join(dirpath, thefile), "wb").write(templatize(src))
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (
                    domain, os.path.join(dirpath, thefile))
                (stdin, stdout, stderr) = os.popen3(cmd, 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
                if thefile != file:
                    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')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                if thefile != file:
                    os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            (stdin, stdout, stderr) = os.popen3('msguniq --to-code=utf-8 "%s"' % potfile, 'b')
            msgs = stdout.read()
            errors = stderr.read()
            if errors:
                raise CommandError("errors happened while running msguniq\n%s" % errors)
            open(potfile, 'w').write(msgs)
            if os.path.exists(pofile):
                (stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), 'b')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running msgmerge\n%s" % errors)
            open(pofile, 'wb').write(msgs)
            os.unlink(potfile)
Exemple #23
0
def make_messages(locale=None, domain='django', verbosity='1', all=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

    if os.path.isdir(os.path.join('conf', 'locale')):
        localedir = os.path.abspath(os.path.join('conf', 'locale'))
    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:
        # backwards compatible error message
        if not sys.argv[0].endswith("make-messages.py"):
            message = "Type '%s help %s' for usage.\n" % (os.path.basename(sys.argv[0]), sys.argv[1])
        else:
            message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
        raise CommandError(message)

    languages = []
    if locale is not None:
        languages.append(locale)
    elif all:
        languages = [el for el in os.listdir(localedir) if not el.startswith('.')]

    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)

        all_files = []
        for (dirpath, dirnames, filenames) in os.walk("."):
            all_files.extend([(dirpath, f) for f in filenames])
        all_files.sort()
        for dirpath, file in all_files:
            if domain == 'djangojs' and file.endswith('.js'):
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                src = open(os.path.join(dirpath, file), "rb").read()
                src = pythonize_re.sub('\n#', src)
                open(os.path.join(dirpath, '%s.py' % file), "wb").write(src)
                thefile = '%s.py' % file
                cmd = 'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (domain, os.path.join(dirpath, thefile))
                (stdin, stdout, stderr) = os.popen3(cmd, 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
                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')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                os.unlink(os.path.join(dirpath, thefile))
            elif domain == 'django' and (file.endswith('.py') or file.endswith('.html')):
                thefile = file
                if file.endswith('.html'):
                    src = open(os.path.join(dirpath, file), "rb").read()
                    thefile = '%s.py' % file
                    open(os.path.join(dirpath, thefile), "wb").write(templatize(src))
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (
                    domain, os.path.join(dirpath, thefile))
                (stdin, stdout, stderr) = os.popen3(cmd, 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
                if thefile != file:
                    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')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                if thefile != file:
                    os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            (stdin, stdout, stderr) = os.popen3('msguniq --to-code=utf-8 "%s"' % potfile, 'b')
            msgs = stdout.read()
            errors = stderr.read()
            if errors:
                raise CommandError("errors happened while running msguniq\n%s" % errors)
            open(potfile, 'w').write(msgs)
            if os.path.exists(pofile):
                (stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), 'b')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running msgmerge\n%s" % errors)
            open(pofile, 'wb').write(msgs)
            os.unlink(potfile)
Exemple #24
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 = pythonize_re.sub('\n#', src)
                thefile = '%s.py' % file
                f = open(os.path.join(dirpath, thefile), "w")
                try:
                    f.write(src)
                finally:
                    f.close()
                cmd = (
                    'xgettext -d %s -L Perl %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 make_messages(locale=None,
                  domain='django',
                  verbosity='1',
                  all=False,
                  extensions=None):
    """
    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

    if os.path.isdir(os.path.join('conf', 'locale')):
        localedir = os.path.abspath(os.path.join('conf', 'locale'))
    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:
        # backwards compatible error message
        if not sys.argv[0].endswith("make-messages.py"):
            message = "Type '%s help %s' for usage.\n" % (os.path.basename(
                sys.argv[0]), sys.argv[1])
        else:
            message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
        raise CommandError(message)

    # xgettext versions prior to 0.15 assumed Python source files were encoded
    # in iso-8859-1, and produce utf-8 output.  In the case where xgettext is
    # given utf-8 input (required for Django files with non-ASCII characters),
    # this results in a utf-8 re-encoding of the original utf-8 that needs to be
    # undone to restore the original utf-8.  So we check the xgettext version
    # here once and set a flag to remember if a utf-8 decoding needs to be done
    # on xgettext's output for Python files.  We default to assuming this isn't
    # necessary if we run into any trouble determining the version.
    xgettext_reencodes_utf8 = False
    (stdin, stdout, stderr) = os.popen3('xgettext --version', 't')
    match = re.search(r'(?P<major>\d+)\.(?P<minor>\d+)', stdout.read())
    if match:
        xversion = (int(match.group('major')), int(match.group('minor')))
        if xversion < (0, 15):
            xgettext_reencodes_utf8 = True

    languages = []
    if locale is not None:
        languages.append(locale)
    elif all:
        languages = [
            el for el in os.listdir(localedir) if not el.startswith('.')
        ]

    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)

        all_files = []
        for (dirpath, dirnames, filenames) in os.walk("."):
            all_files.extend([(dirpath, f) for f in filenames])
        all_files.sort()
        for dirpath, file in all_files:
            file_base, file_ext = os.path.splitext(file)
            if domain == 'djangojs' and file_ext == '.js':
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' %
                                     (file, dirpath))
                src = open(os.path.join(dirpath, file), "rU").read()
                src = pythonize_re.sub('\n#', src)
                thefile = '%s.py' % file
                open(os.path.join(dirpath, thefile), "w").write(src)
                cmd = 'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (
                    domain, os.path.join(dirpath, thefile))
                (stdin, stdout, stderr) = os.popen3(cmd, 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError(
                        "errors happened while running xgettext on %s\n%s" %
                        (file, errors))
                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')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                os.unlink(os.path.join(dirpath, thefile))
            elif domain == 'django' and (file_ext == '.py'
                                         or file_ext in extensions):
                thefile = file
                if file_ext in extensions:
                    src = open(os.path.join(dirpath, file), "rU").read()
                    thefile = '%s.py' % file
                    open(os.path.join(dirpath, thefile),
                         "w").write(templatize(src))
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' %
                                     (file, dirpath))
                cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (
                    domain, os.path.join(dirpath, thefile))
                (stdin, stdout, stderr) = os.popen3(cmd, 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError(
                        "errors happened while running xgettext on %s\n%s" %
                        (file, errors))

                if xgettext_reencodes_utf8:
                    msgs = msgs.decode('utf-8').encode('iso-8859-1')

                if thefile != file:
                    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')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                if thefile != file:
                    os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            (stdin, stdout,
             stderr) = os.popen3('msguniq --to-code=utf-8 "%s"' % potfile, 't')
            msgs = stdout.read()
            errors = stderr.read()
            if errors:
                raise CommandError(
                    "errors happened while running msguniq\n%s" % errors)
            open(potfile, 'w').write(msgs)
            if os.path.exists(pofile):
                (stdin, stdout, stderr) = os.popen3(
                    'msgmerge -q "%s" "%s"' % (pofile, potfile), 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError(
                        "errors happened while running msgmerge\n%s" % errors)
            open(pofile, 'wb').write(msgs)
            os.unlink(potfile)
Exemple #26
0
def make_messages():
    localedir = None

    if os.path.isdir(os.path.join("conf", "locale")):
        localedir = os.path.abspath(os.path.join("conf", "locale"))
    elif os.path.isdir("locale"):
        localedir = os.path.abspath("locale")
    else:
        print(
            "This script should be run from the django svn tree or your project or app tree."
        )
        print(
            "If you did indeed run it from the svn checkout or your project or application,"
        )
        print(
            "maybe you are just missing the conf/locale (in the django tree) or locale (for project"
        )
        print("and application) directory?")
        print(
            "make-messages.py doesn't create it automatically, you have to create it by hand if"
        )
        print("you want to enable i18n for your project or application.")
        sys.exit(1)

    (opts, args) = getopt.getopt(sys.argv[1:], "l:d:va")

    lang = None
    domain = "django"
    verbose = False
    all = False

    for o, v in opts:
        if o == "-l":
            lang = v
        elif o == "-d":
            domain = v
        elif o == "-v":
            verbose = True
        elif o == "-a":
            all = True

    if domain not in ("django", "djangojs"):
        print(
            "currently make-messages.py only supports domains 'django' and 'djangojs'"
        )
        sys.exit(1)
    if (lang is None and not all) or domain is None:
        print("usage: make-messages.py -l <language>")
        print("   or: make-messages.py -a")
        sys.exit(1)

    languages = []

    if lang is not None:
        languages.append(lang)
    elif all:
        languages = [
            el for el in os.listdir(localedir) if not el.startswith(".")
        ]

    for lang in languages:

        print(("processing language", lang))
        basedir = os.path.join(localedir, lang, "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)

        all_files = []
        for (dirpath, dirnames, filenames) in os.walk("."):
            all_files.extend([(dirpath, f) for f in filenames])
        all_files.sort()
        for dirpath, file in all_files:
            if domain == "djangojs" and file.endswith(".js"):
                if verbose:
                    sys.stdout.write("processing file %s in %s\n" %
                                     (file, dirpath))
                src = open(os.path.join(dirpath, file), "rb").read()
                src = pythonize_re.sub("\n#", src)
                open(os.path.join(dirpath, "%s.py" % file), "wb").write(src)
                thefile = "%s.py" % file
                cmd = (
                    'xgettext %s -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"'
                    % (
                        os.path.exists(potfile) and "--omit-header" or "",
                        domain,
                        os.path.join(dirpath, thefile),
                    ))
                (stdin, stdout, stderr) = os.popen3(cmd, "t")
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    print(("errors happened while running xgettext on %s" %
                           file))
                    print(errors)
                    sys.exit(8)
                old = "#: " + os.path.join(dirpath, thefile)[2:]
                new = "#: " + os.path.join(dirpath, file)[2:]
                msgs = msgs.replace(old, new)
                if msgs:
                    open(potfile, "ab").write(msgs)
                os.unlink(os.path.join(dirpath, thefile))
            elif domain == "django" and (file.endswith(".py")
                                         or file.endswith(".html")
                                         or file.endswith(".txt")
                                         or file.endswith(".rml")):
                thefile = file
                if (file.endswith(".html") or file.endswith(".txt")
                        or file.endswith(".rml")):
                    src = open(os.path.join(dirpath, file), "rb").read()
                    thefile = "%s.py" % file
                    open(os.path.join(dirpath, thefile),
                         "wb").write(templatize(src))
                if verbose:
                    sys.stdout.write("processing file %s in %s\n" %
                                     (file, dirpath))
                cmd = (
                    'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"'
                    % (domain, os.path.join(dirpath, thefile)))
                (stdin, stdout, stderr) = os.popen3(cmd, "t")
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    print(("errors happened while running xgettext on %s" %
                           file))
                    print(errors)
                    sys.exit(8)
                if thefile != file:
                    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")
                if msgs:
                    open(potfile, "ab").write(msgs)
                if thefile != file:
                    os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            (stdin, stdout,
             stderr) = os.popen3('msguniq --to-code=utf-8 "%s"' % potfile, "b")
            msgs = stdout.read()
            errors = stderr.read()
            if errors:
                print("errors happened while running msguniq")
                print(errors)
                sys.exit(8)
            open(potfile, "w").write(msgs)
            if os.path.exists(pofile):
                (stdin, stdout, stderr) = os.popen3(
                    'msgmerge -q "%s" "%s"' % (pofile, potfile), "b")
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    print("errors happened while running msgmerge")
                    print(errors)
                    sys.exit(8)
            open(pofile, "wb").write(msgs)
            os.unlink(potfile)
Exemple #27
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)
Exemple #28
0
def make_messages(locale=None, domain='django', verbosity='1', all=False, extensions=None):
    """
    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

    if os.path.isdir(os.path.join('conf', 'locale')):
        localedir = os.path.abspath(os.path.join('conf', 'locale'))
    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:
        # backwards compatible error message
        if not sys.argv[0].endswith("make-messages.py"):
            message = "Type '%s help %s' for usage.\n" % (os.path.basename(sys.argv[0]), sys.argv[1])
        else:
            message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
        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]

    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)

        all_files = []
        for (dirpath, dirnames, filenames) in os.walk("."):
            all_files.extend([(dirpath, f) for f in filenames])
        all_files.sort()
        for dirpath, file in all_files:
            file_base, file_ext = os.path.splitext(file)
            if domain == 'djangojs' and file_ext == '.js':
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                src = open(os.path.join(dirpath, file), "rU").read()
                src = pythonize_re.sub('\n#', src)
                thefile = '%s.py' % file
                open(os.path.join(dirpath, thefile), "w").write(src)
                cmd = 'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (domain, os.path.join(dirpath, thefile))
                msgs, errors = _popen(cmd)
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
                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')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                os.unlink(os.path.join(dirpath, thefile))
            elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
                thefile = file
                if file_ext in extensions:
                    src = open(os.path.join(dirpath, file), "rU").read()
                    thefile = '%s.py' % file
                    try:
                        open(os.path.join(dirpath, thefile), "w").write(templatize(src))
                    except SyntaxError, msg:
                        msg = "%s (file: %s)" % (msg, os.path.join(dirpath, file))
                        raise SyntaxError(msg)
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (
                    domain, os.path.join(dirpath, thefile))
                msgs, errors = _popen(cmd)
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))

                if thefile != file:
                    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')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                if thefile != file:
                    os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            msgs, errors = _popen('msguniq --to-code=utf-8 "%s"' % potfile)
            if errors:
                raise CommandError("errors happened while running msguniq\n%s" % errors)
            open(potfile, 'w').write(msgs)
            if os.path.exists(pofile):
                msgs, errors = _popen('msgmerge -q "%s" "%s"' % (pofile, potfile))
                if errors:
                    raise CommandError("errors happened while running msgmerge\n%s" % errors)
            open(pofile, 'wb').write(msgs)
            os.unlink(potfile)
Exemple #29
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)
from __future__ import unicode_literals
Exemple #31
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)
Exemple #32
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)
Exemple #33
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)
Exemple #34
0
def make_messages():
    localedir = None

    if os.path.isdir(os.path.join('conf', 'locale')):
        localedir = os.path.abspath(os.path.join('conf', 'locale'))
    elif os.path.isdir('locale'):
        localedir = os.path.abspath('locale')
    else:
        print "This script should be run from the django svn tree or your project or app tree."
        print "If you did indeed run it from the svn checkout or your project or application,"
        print "maybe you are just missing the conf/locale (in the django tree) or locale (for project"
        print "and application) directory?"
        print "make-messages.py doesn't create it automatically, you have to create it by hand if"
        print "you want to enable i18n for your project or application."
        sys.exit(1)

    (opts, args) = getopt.getopt(sys.argv[1:], 'l:d:va')

    lang = None
    domain = 'django'
    verbose = False
    all = False

    for o, v in opts:
        if o == '-l':
            lang = v
        elif o == '-d':
            domain = v
        elif o == '-v':
            verbose = True
        elif o == '-a':
            all = True

    if domain not in ('django', 'djangojs'):
        print "currently make-messages.py only supports domains 'django' and 'djangojs'"
        sys.exit(1)
    if (lang is None and not all) or domain is None:
        print "usage: make-messages.py -l <language>"
        print "   or: make-messages.py -a"
        sys.exit(1)

    languages = []

    if lang is not None:
        languages.append(lang)
    elif all:
        languages = [
            el for el in os.listdir(localedir) if not el.startswith('.')
        ]

    for lang in languages:

        print "processing language", lang
        basedir = os.path.join(localedir, lang, '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, dirnames, filenames) in os.walk("."):
            for file in filenames:
                if domain == 'djangojs' and file.endswith('.js'):
                    if verbose:
                        sys.stdout.write('processing file %s in %s\n' %
                                         (file, dirpath))
                    src = open(os.path.join(dirpath, file), "rb").read()
                    src = pythonize_re.sub('\n#', src)
                    open(os.path.join(dirpath, '%s.py' % file),
                         "wb").write(src)
                    thefile = '%s.py' % file
                    cmd = 'xgettext %s -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (
                        os.path.exists(potfile) and '--omit-header'
                        or '', domain, os.path.join(dirpath, thefile))
                    (stdin, stdout, stderr) = os.popen3(cmd, 'b')
                    msgs = stdout.read()
                    errors = stderr.read()
                    if errors:
                        print "errors happened while running xgettext on %s" % file
                        print errors
                        sys.exit(8)
                    old = '#: ' + os.path.join(dirpath, thefile)[2:]
                    new = '#: ' + os.path.join(dirpath, file)[2:]
                    msgs = msgs.replace(old, new)
                    if msgs:
                        open(potfile, 'ab').write(msgs)
                    os.unlink(os.path.join(dirpath, thefile))
                elif domain == 'django' and (file.endswith('.py')
                                             or file.endswith('.html')):
                    thefile = file
                    if file.endswith('.html'):
                        src = open(os.path.join(dirpath, file), "rb").read()
                        open(os.path.join(dirpath, '%s.py' % file),
                             "wb").write(templatize(src))
                        thefile = '%s.py' % file
                    if verbose:
                        sys.stdout.write('processing file %s in %s\n' %
                                         (file, dirpath))
                    cmd = 'xgettext %s -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (
                        os.path.exists(potfile) and '--omit-header'
                        or '', domain, os.path.join(dirpath, thefile))
                    (stdin, stdout, stderr) = os.popen3(cmd, 'b')
                    msgs = stdout.read()
                    errors = stderr.read()
                    if errors:
                        print "errors happened while running xgettext on %s" % file
                        print errors
                        sys.exit(8)
                    if thefile != file:
                        old = '#: ' + os.path.join(dirpath, thefile)[2:]
                        new = '#: ' + os.path.join(dirpath, file)[2:]
                        msgs = msgs.replace(old, new)
                    if msgs:
                        open(potfile, 'ab').write(msgs)
                    if thefile != file:
                        os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            (stdin, stdout, stderr) = os.popen3('msguniq "%s"' % potfile, 'b')
            msgs = stdout.read()
            errors = stderr.read()
            if errors:
                print "errors happened while running msguniq"
                print errors
                sys.exit(8)
            open(potfile, 'w').write(msgs)
            if os.path.exists(pofile):
                (stdin, stdout, stderr) = os.popen3(
                    'msgmerge -q "%s" "%s"' % (pofile, potfile), 'b')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    print "errors happened while running msgmerge"
                    print errors
                    sys.exit(8)
            open(pofile, 'wb').write(msgs)
            os.unlink(potfile)
Exemple #35
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)
Exemple #36
0
def make_messages(locale=None, domain='django', verbosity='1', all=False, extensions=None):
    """
    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

    if os.path.isdir(os.path.join('conf', 'locale')):
        localedir = os.path.abspath(os.path.join('conf', 'locale'))
    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:
        # backwards compatible error message
        if not sys.argv[0].endswith("make-messages.py"):
            message = "Type '%s help %s' for usage.\n" % (os.path.basename(sys.argv[0]), sys.argv[1])
        else:
            message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
        raise CommandError(message)

    # xgettext versions prior to 0.15 assumed Python source files were encoded
    # in iso-8859-1, and produce utf-8 output.  In the case where xgettext is
    # given utf-8 input (required for Django files with non-ASCII characters),
    # this results in a utf-8 re-encoding of the original utf-8 that needs to be
    # undone to restore the original utf-8.  So we check the xgettext version
    # here once and set a flag to remember if a utf-8 decoding needs to be done
    # on xgettext's output for Python files.  We default to assuming this isn't
    # necessary if we run into any trouble determining the version.
    xgettext_reencodes_utf8 = False
    (stdin, stdout, stderr) = os.popen3('xgettext --version', 't')
    match = re.search(r'(?P<major>\d+)\.(?P<minor>\d+)', stdout.read())
    if match:
        xversion = (int(match.group('major')), int(match.group('minor')))
        if xversion < (0, 15):
            xgettext_reencodes_utf8 = True
 
    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]
    
    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)

        all_files = []
        for (dirpath, dirnames, filenames) in os.walk("."):
            all_files.extend([(dirpath, f) for f in filenames])
        all_files.sort()
        for dirpath, file in all_files:
            file_base, file_ext = os.path.splitext(file)
            if domain == 'djangojs' and file_ext == '.js':
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                src = open(os.path.join(dirpath, file), "r").readlines()
                dest = open(os.path.join(dirpath, '%s.py' % file), "wb")
                for line in src :
                    if not pythonize_re2.search(line):
                        dest.write('#%s' % line)
                    else:
                        dest.write(line)
                dest.close()
                #src = open(os.path.join(dirpath, file), "rU").read()
                #src = pythonize_re.sub('\n#', src)
                thefile = '%s.py' % file
                #open(os.path.join(dirpath, thefile), "w").write(src)
                cmd = 'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (domain, os.path.join(dirpath, thefile))
                (stdin, stdout, stderr) = os.popen3(cmd, 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
                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')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                os.unlink(os.path.join(dirpath, thefile))
            elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
                thefile = file
                if file_ext in extensions:
                    src = open(os.path.join(dirpath, file), "rU").read()
                    thefile = '%s.py' % file
                    try:
                        open(os.path.join(dirpath, thefile), "w").write(templatize(src))
                    except SyntaxError, msg:
                        msg = "%s (file: %s)" % (msg, os.path.join(dirpath, file))
                        raise SyntaxError(msg)
                if verbosity > 1:
                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
                cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (
                    domain, os.path.join(dirpath, thefile))
                (stdin, stdout, stderr) = os.popen3(cmd, 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))

                if xgettext_reencodes_utf8:
                    msgs = msgs.decode('utf-8').encode('iso-8859-1')

                if thefile != file:
                    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')
                if msgs:
                    open(potfile, 'ab').write(msgs)
                if thefile != file:
                    os.unlink(os.path.join(dirpath, thefile))

        if os.path.exists(potfile):
            (stdin, stdout, stderr) = os.popen3('msguniq --to-code=utf-8 "%s"' % potfile, 't')
            msgs = stdout.read()
            errors = stderr.read()
            if errors:
                raise CommandError("errors happened while running msguniq\n%s" % errors)
            open(potfile, 'w').write(msgs)
            if os.path.exists(pofile):
                (stdin, stdout, stderr) = os.popen3('msgmerge -q "%s" "%s"' % (pofile, potfile), 't')
                msgs = stdout.read()
                errors = stderr.read()
                if errors:
                    raise CommandError("errors happened while running msgmerge\n%s" % errors)
            open(pofile, 'wb').write(msgs)
            os.unlink(potfile)
Exemple #37
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)
Exemple #38
0
"""
update_translations -- Update our gettext translation files

This command does basically the same thing as Django's makemessages command.
The reason that we implement our own is the standard one runs really slow.  My
(BDK) best guess is that it's because it tries to process too many files and
there's no good way to change the behavoir on our current django version.

Here's our basic strategy:
  - We create 2 gettext domains -- "django" for python/template strings and
    "djangojs" for javascript strings.

  - For each domain, we create a .po template file (.pot file for short).
    This stores all translatable strings in the app for that domain.

  - For each locale/domain pair we create a .po file which is basically a copy
    of the .pot file with localized strings filled in in that language.  We
    normally get the .po files from transifex, but you can also update them
    in-place with a .po file editor.

  - Outside of this command we use the transifex client to push the .pot file
    and pull in the .po files.

One tricky part is the template files.  gettext can't handle these natively,
we need to run them through Django's templatize() function.  To allow gettext
to see the output, we create a temporary directory and store the output of
templatize() for each template in a path relative to the temp directory.
"""

import glob
    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)
Exemple #40
0
def make_messages(
    locale=None, domain="django", verbosity="1", all=False, extensions=None, symlinks=False, ignore_patterns=[]
):
    """
    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
    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:
        # backwards compatible error message
        if not sys.argv[0].endswith("make-messages.py"):
            message = "Type '%s help %s' for usage.\n" % (os.path.basename(sys.argv[0]), sys.argv[1])
        else:
            message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
        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]

    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 = pythonize_re.sub("\n#", src)
                thefile = "%s.py" % file
                f = open(os.path.join(dirpath, thefile), "w")
                try:
                    f.write(src)
                finally:
                    f.close()
                cmd = (
                    'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --from-code UTF-8 -o - "%s"'
                    % (domain, os.path.join(dirpath, thefile))
                )
                msgs, errors = _popen(cmd)
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
                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")
                if msgs:
                    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
                if file_ext in extensions:
                    src = open(os.path.join(dirpath, file), "rU").read()
                    thefile = "%s.py" % file
                    try:
                        f = open(os.path.join(dirpath, thefile), "w")
                        try:
                            f.write(templatize(src))
                        finally:
                            f.close()
                    except SyntaxError, msg:
                        msg = "%s (file: %s)" % (msg, os.path.join(dirpath, file))
                        raise SyntaxError(msg)
                if verbosity > 1:
                    sys.stdout.write("processing file %s in %s\n" % (file, dirpath))
                cmd = (
                    'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"'
                    % (domain, os.path.join(dirpath, thefile))
                )
                msgs, errors = _popen(cmd)
                if errors:
                    raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))

                if thefile != file:
                    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")
                if msgs:
                    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 --to-code=utf-8 "%s"' % potfile)
            if errors:
                raise CommandError("errors happened while running msguniq\n%s" % errors)
            f = open(potfile, "w")
            try:
                f.write(msgs)
            finally:
                f.close()
            if os.path.exists(pofile):
                msgs, errors = _popen('msgmerge -q "%s" "%s"' % (pofile, potfile))
                if errors:
                    raise CommandError("errors happened while running msgmerge\n%s" % errors)
            elif not invoked_for_django:
                msgs = copy_plural_forms(msgs, locale, domain, verbosity)
            f = open(pofile, "wb")
            try:
                f.write(msgs)
            finally:
                f.close()
            os.unlink(potfile)