public
Last active

Django ticket #19482 localflavor apps locales

  • Download Gist
generate.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
#!/usr/bin/env python
from __future__ import unicode_literals
import os
import re
import shutil
from subprocess import Popen
 
from django.contrib import localflavor
import polib
from polib import POEntry
 
 
LOCALFLAVOR_DIRECTORY = localflavor.__path__[0]
 
FLAVOR_RE = re.compile(r'^(?P<name>[a-z]{2})_?$')
flavors = tuple(
directory[0:2] for directory in os.listdir(LOCALFLAVOR_DIRECTORY)
if FLAVOR_RE.match(directory) and directory != 'uk' # uk was deprecated for gb
)
 
print('Cloning localflavor repositories...')
 
if not os.path.isdir('repositories'):
os.mkdir('repositories')
 
def clone_or_pull(flavor):
localflavor_name = "django-localflavor-%s" % flavor
destination = os.path.join('repositories', localflavor_name)
if os.path.exists(destination):
process = Popen(['git', 'clean', '-f', '-d'],
cwd=destination)
process.wait()
process = Popen(['git', 'checkout', 'master', '-f'],
cwd=destination)
process.wait()
process = Popen(['git', 'pull', 'origin', 'master'],
cwd=destination)
process.wait()
else:
repository = "git://github.com/django/%s.git" % localflavor_name
process = Popen(['git', 'clone', repository, destination])
process.wait()
 
 
for flavor in flavors:
clone_or_pull(flavor)
 
 
LOCALFLAVOR_LOCALE_DIRECTORY = os.path.join(LOCALFLAVOR_DIRECTORY, 'locale')
locales = tuple(os.listdir(LOCALFLAVOR_LOCALE_DIRECTORY))
 
print('Extracting messages from deprecated contrib localflavor...')
 
PO_FILE_PATH = os.path.join('LC_MESSAGES', 'django.po')
headers = {}
last_translators = {}
messages = {}
for locale in locales:
catalog = polib.pofile(os.path.join(LOCALFLAVOR_LOCALE_DIRECTORY, locale, PO_FILE_PATH))
headers[locale] = catalog.header
last_translators[locale] = catalog.metadata['Last-Translator']
for entry in catalog:
if entry.translated() and entry.msgstr != entry.msgid:
for path, line in entry.occurrences:
flavor = os.path.split(path)[0][0:2]
messages.setdefault(flavor, {})
messages[flavor].setdefault(locale, {})
messages[flavor][locale][entry.msgid] = entry.msgstr
 
 
print('Making messages for django-localflavor-* apps')
 
DEFAULT_PO_HEADER = """SOME DESCRIPTIVE TITLE.
Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
This file is distributed under the same license as the PACKAGE package.
FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
"""
 
for flavor in flavors:
# No locale for this flavor
if flavor not in messages:
continue
localflavor_name = "django-localflavor-%s" % flavor
app_directory = os.path.join('repositories', localflavor_name,
localflavor_name.replace('-', '_'))
locale_directory = os.path.join(app_directory, 'locale')
# Ensure the locale folder exist in order to be able to run makemessages
if not os.path.exists(locale_directory):
os.mkdir(locale_directory)
print("Making message for django-localflavor-%s" % flavor)
compile = False
for locale in messages[flavor].keys():
catalog_path = os.path.join(locale_directory, locale, PO_FILE_PATH)
catalog_exists = os.path.exists(catalog_path)
# Create the catalog if it doesn't already exist
if not catalog_exists:
process = Popen(['django-admin.py', 'makemessages', '-l', locale],
cwd=app_directory)
process.wait()
catalog = polib.pofile(catalog_path)
locale_messages = messages[flavor][locale]
replaced = False
for entry in catalog:
msgstr = locale_messages.get(entry.msgid)
if msgstr:
if entry.flags:
raise Exception(
"flavor: %s, locale: %s, msgid: %s, flags: %r" % (
flavor, locale, entry.msgid, entry.flags
))
if entry.msgstr != msgstr:
entry.msgstr = msgstr
replaced = True
# Save the catalog if we replaced atleast one string
if replaced:
if catalog.header == DEFAULT_PO_HEADER:
catalog.header = headers[locale]
if catalog.metadata['Project-Id-Version'] == 'PACKAGE VERSION':
catalog.metadata['Project-Id-Version'] = localflavor_name
if catalog.metadata['Last-Translator'] == 'FULL NAME <EMAIL@ADDRESS>':
catalog.metadata['Last-Translator'] = last_translators[locale]
catalog.save()
compile = True
elif not catalog_exists:
# Remove the useless generated catalog file
os.remove(catalog_path)
if compile:
process = Popen(['django-admin.py', 'compilemessages'],
cwd=app_directory)
process.wait()
 
 
print('Generating patches')
if os.path.exists('patches'):
shutil.rmtree('patches')
os.mkdir('patches')
 
PATCHES_DIRECTORY = os.path.abspath('patches')
for flavor in flavors:
localflavor_name = "django-localflavor-%s" % flavor
cwd = os.path.join('repositories', localflavor_name)
destination = os.path.join(PATCHES_DIRECTORY, "%s.diff" % localflavor_name)
process = Popen(['git', 'add', '.'], cwd=cwd)
process.wait()
with open(destination, 'w+') as stdout:
process = Popen(['git', 'diff', '--cached', '--binary'],
cwd=cwd, stdout=stdout)
process.wait()
if not os.path.getsize(destination):
os.remove(destination)
process = Popen(['git', 'reset', '.'], cwd=cwd)
process.wait()
requirements.txt
1 2
https://github.com/django/django/archive/stable/1.5.x.tar.gz
polib

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.