Exemplo n.º 1
0
def process_file(file, dirpath, potfile, domain, verbosity, #@ReservedAssignment
                 extensions, wrap, location, stdout=sys.stdout, 
                 extra_keywords=None):
    """
    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()
        
        keywords = ['gettext_noop', 
                    'gettext_lazy', 
                    'ngettext_lazy:1,2',
                    'pgettext:1c,2', 
                    'npgettext:1c,2,3',
                    ]
        if extra_keywords:
            keywords.extend(extra_keywords)
        
        cmd = 'xgettext -d %s -L C %s %s'
        for kw in keywords: 
            cmd += ' --keyword=%s' % kw
        cmd += ' --from-code UTF-8 --add-comments=Translators -o - "%s"'
        cmd %= (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)

        keywords = ['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',
                    ]
        if extra_keywords:
            keywords.extend(extra_keywords)
        
        cmd = 'xgettext -d %s -L Python %s %s'
        for kw in keywords: 
            cmd += ' --keyword=%s' % kw
        cmd += ' --from-code UTF-8 --add-comments=Translators -o - "%s"'
        cmd %= (domain, wrap, location, work_file)
    else:
        return
    msgs, errors = makemessages._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:
        makemessages.write_pot_file(potfile, msgs, orig_file, work_file, is_templatized)
    if is_templatized:
        os.unlink(work_file)
Exemplo n.º 2
0
def _make_messages(locale=None, domain='django', verbosity=1, all=False, #@ReservedAssignment
        extensions=None, symlinks=False, ignore_patterns=None, no_wrap=False,
        no_location=False, no_obsolete=False, stdout=sys.stdout, 
        extra_keywords=None):
    """
    Uses the ``locale/`` directory from the Django SVN tree or an
    application/project to process all files with translatable literals for
    the :param domain: domain and :param locale: locale.
    """
    # 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)

    if ignore_patterns is None:
        ignore_patterns = []

    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 = makemessages._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())

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

    wrap = '--no-wrap' if no_wrap else ''
    location = '--no-location' if no_location else ''

    for locale in locales:
        if verbosity > 0:
            stdout.write("processing language %s\n" % 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 makemessages.find_files(".", ignore_patterns, verbosity, #@ReservedAssignment
                stdout, symlinks=symlinks):
            process_file(file, dirpath, potfile, domain, verbosity, extensions,
                    wrap, location, stdout, extra_keywords)

        if os.path.exists(potfile):
            makemessages.write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
                    not invoked_for_django, wrap, location, no_obsolete)
Exemplo n.º 3
0
def _make_messages(
        locale=None,
        domain='django',
        verbosity=1,
        all=False,  #@ReservedAssignment
        extensions=None,
        symlinks=False,
        ignore_patterns=None,
        no_wrap=False,
        no_location=False,
        no_obsolete=False,
        stdout=sys.stdout,
        extra_keywords=None):
    """
    Uses the ``locale/`` directory from the Django SVN tree or an
    application/project to process all files with translatable literals for
    the :param domain: domain and :param locale: locale.
    """
    # 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)

    if ignore_patterns is None:
        ignore_patterns = []

    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 = makemessages._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())

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

    wrap = '--no-wrap' if no_wrap else ''
    location = '--no-location' if no_location else ''

    for locale in locales:
        if verbosity > 0:
            stdout.write("processing language %s\n" % 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 makemessages.find_files(
                ".",
                ignore_patterns,
                verbosity,  #@ReservedAssignment
                stdout,
                symlinks=symlinks):
            process_file(file, dirpath, potfile, domain, verbosity, extensions,
                         wrap, location, stdout, extra_keywords)

        if os.path.exists(potfile):
            makemessages.write_po_file(pofile, potfile, domain, locale,
                                       verbosity, stdout,
                                       not invoked_for_django, wrap, location,
                                       no_obsolete)
Exemplo n.º 4
0
def process_file(
        file,
        dirpath,
        potfile,
        domain,
        verbosity,  #@ReservedAssignment
        extensions,
        wrap,
        location,
        stdout=sys.stdout,
        extra_keywords=None):
    """
    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()

        keywords = [
            'gettext_noop',
            'gettext_lazy',
            'ngettext_lazy:1,2',
            'pgettext:1c,2',
            'npgettext:1c,2,3',
        ]
        if extra_keywords:
            keywords.extend(extra_keywords)

        cmd = 'xgettext -d %s -L C %s %s'
        for kw in keywords:
            cmd += ' --keyword=%s' % kw
        cmd += ' --from-code UTF-8 --add-comments=Translators -o - "%s"'
        cmd %= (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)

        keywords = [
            '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',
        ]
        if extra_keywords:
            keywords.extend(extra_keywords)

        cmd = 'xgettext -d %s -L Python %s %s'
        for kw in keywords:
            cmd += ' --keyword=%s' % kw
        cmd += ' --from-code UTF-8 --add-comments=Translators -o - "%s"'
        cmd %= (domain, wrap, location, work_file)
    else:
        return
    msgs, errors = makemessages._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:
        makemessages.write_pot_file(potfile, msgs, orig_file, work_file,
                                    is_templatized)
    if is_templatized:
        os.unlink(work_file)
Exemplo n.º 5
0
def make_messages(locale=None, domain='django', verbosity=1, all=False,
                  extensions=None, symlinks=False, ignore_patterns=None,
                  no_wrap=False,
                  no_location=False, no_obsolete=False, stdout=sys.stdout,
                  include_paths=None):
    """
    Uses the ``locale/`` directory from the Django Git tree or an
    application/project to process all files with translatable literals for
    the :param domain: domain and :param locale: locale.

    NOTE: Only a small part is changed to work with included files.
    """
    if include_paths is None:
        include_paths = []

    # 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)

    if ignore_patterns is None:
        ignore_patterns = []

    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 Git "
                           "tree or your project or app tree. If you did indeed run it "
                           "from the Git 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, errors, status = _popen('xgettext --version')
    if status != STATUS_OK:
        raise CommandError("Error running xgettext. Note that Django "
                           "internationalization requires GNU gettext 0.15 or newer.")
    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())

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

    wrap = '--no-wrap' if no_wrap else ''
    location = '--no-location' if no_location else ''

    for locale in locales:
        if verbosity > 0:
            stdout.write("processing language %s\n" % 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' % str(domain))
        potfile = os.path.join(basedir, '%s.pot' % str(domain))

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

        # NOTE: Additional for-loop added compared to original command to iterate over included projects.
        for root in ['.'] + include_paths:
            if not os.path.exists(root):
                stdout.write(
                    '  skipping directory: {0} (not found)'.format(root))
            else:
                stdout.write('  processing directory: {0}'.format(root))
                for dirpath, file in find_files(root, ignore_patterns,
                                                verbosity,
                                                stdout, symlinks=symlinks):
                    try:
                        process_file(file, dirpath, potfile, domain, verbosity,
                                     extensions,
                                     wrap, location, stdout)
                    except UnicodeDecodeError:
                        stdout.write(
                            "UnicodeDecodeError: skipped file %s in %s" % (
                            file, dirpath))

        if os.path.exists(potfile):
            write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
                          not invoked_for_django, wrap, location, no_obsolete)