Skip to content

Instantly share code, notes, and snippets.

@BigglesZX
Last active August 29, 2015 14:18
Show Gist options
  • Save BigglesZX/c3aaaf56e36399e074ac to your computer and use it in GitHub Desktop.
Save BigglesZX/c3aaaf56e36399e074ac to your computer and use it in GitHub Desktop.
Django management command to copy translations from Loco endpoints to local PO files
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from os.path import join
from urllib import urlretrieve
class Command(BaseCommand):
help = 'Imports translations from Loco'
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError("You must specify a local locale name")
locale = args[0]
if locale not in settings.LOCALE_MAP:
raise CommandError("Invalid locale specified")
locale = settings.LOCALE_MAP[locale]
url = "https://localise.biz:443/api/export/locale/{0}.po?key={1}&format=gettext" \
.format(locale['loco_name'], settings.LOCO_PROJECT_API_KEY)
filename, headers = urlretrieve(url, join(locale['path'], 'django.po'))
self.stdout.write('Imported Loco translations for locale: {0}\n'.format(args[0]))
self.stdout.write('You can now run `django-admin compilemessages` to compile the new PO data.\n')
''' Map local locales to Loco locales. Ha. Keys are locale names this side. '''
LOCO_PROJECT_API_KEY = 'YOUR_LOCO_API_KEY_HERE'
LOCALE_MAP = {
'de': {
'loco_name': 'de_DE',
'path': join(PROJECT_ROOT, 'locale', 'de', 'LC_MESSAGES'),
},
# etc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment