def make_messages(self, locale, process_all, extensions, verbosity): if not os.path.isdir('locale'): raise CommandError("This script should be run from django project directory") localedir = os.path.abspath('locale') 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] for locale in locales: if verbosity > 0: sys.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(self.domain)) potfile = os.path.join(basedir, '%s.pot' % str(self.domain)) # Collect all posible files files = [] for dirpath, file in find_files(".", self.ignore_patterns, verbosity): _, file_ext = os.path.splitext(file) if file_ext not in extensions: continue files.append(os.path.join(dirpath, file)) # Generate .pot temporal file cmd = ("xgettext --language=PHP --from-code=utf-8 -c --keyword=gettext --keyword=ngettext:1,2 " "--keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 -o {0} {1}") cmd = cmd.format(potfile, " ".join(files)) msgs, errors, status = _popen(cmd) msgs, errors, status = _popen('msguniq --to-code=utf-8 "%s"' % (potfile)) if errors: if status != 0: os.unlink(potfile) raise CommandError("errors happened while running msguniq\n%s" % errors) if os.path.exists(pofile): cmd = "msgmerge -q '%s' '%s' -o '%s'" % (pofile, potfile, pofile) msgs, errors, status = _popen(cmd) if errors: if status != 0: os.unlink(potfile) raise CommandError("errors happened while running msgmerge\n%s" % errors) else: with io.open(potfile, "rt") as fpot, io.open(pofile, "wt") as fpo: for line in fpot: if "charset=CHARSET" in line: line = line.replace('charset=CHARSET', 'charset=UTF-8') fpo.write(line)
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)
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)