Last active
August 29, 2015 14:18
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' 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