コード例 #1
0
def make_messages(flavor_dir, msgfile, locale=None, verbosity=1, all=False, 
        no_wrap=False, no_location=True, no_obsolete=False, 
        stdout=sys.stdout, domain='django'):
    """
    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.
    """
    invoked_for_django = False
    localedir = os.path.abspath(os.path.join(flavor_dir, 'locale'))

    if (locale is None and not all):
        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 = subprocess.check_output('xgettext --version', shell=True)
    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)

        dirpath, file = os.path.split(msgfile)
        extensions = []
        process_file(file, dirpath, potfile, domain, verbosity, extensions,
                wrap, location, stdout)

        if os.path.exists(potfile):
            write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
                    not invoked_for_django, wrap, location, no_obsolete)
コード例 #2
0
ファイル: make_messages.py プロジェクト: Betriebsrat/ecm
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)
コード例 #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)
コード例 #4
0
ファイル: makemessages.py プロジェクト: repodevs/bluebottle
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)