Commit 2762e7bb authored by Michal Čihař's avatar Michal Čihař

Add command to compare language definitions to a file

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent b858867c
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2014 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <http://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from django.core.management.base import BaseCommand, CommandError
from optparse import make_option
from weblate.lang.models import Language
class Command(BaseCommand):
help = 'Checks language definitions against rst/csv file'
def handle(self, *args, **options):
'''
Creates default set of languages, optionally updating them
to match current shipped definitions.
'''
if len(args) != 1:
raise CommandError('Use: checklang file')
errors = Language.objects.check(args[0])
for error in errors:
self.stderr.write(error)
......@@ -251,6 +251,38 @@ class LanguageManager(models.Manager):
'''
return self.filter(translation__total__gt=0).distinct()
def check(self, filename):
"""
Checks database language definitions with supplied ones.
"""
errors = []
with open(filename) as handle:
for line in handle:
line = line.strip().decode('utf-8')
parts = [part.strip() for part in line.split(',')]
if len(parts) != 3:
continue
lang, name, plurals = parts
nplurals, pluralform = plurals.strip(';').split(';')
nplurals = int(nplurals.split('=', 1)[1])
pluralform = pluralform.split('=', 1)[1]
try:
language = Language.objects.get(code=lang)
except Language.DoesNotExist:
errors.append(
u'missing language {0}'.format(line)
)
continue
if nplurals != language.nplurals:
errors.append(
u'different number of plurals {0}'.format(line)
)
errors.append(
u'have {0}'.format(language.get_plural_form())
)
return errors
@receiver(post_syncdb)
@receiver(post_migrate)
......
This diff is collapsed.
......@@ -25,6 +25,7 @@ Tests for language manipulations.
from django.test import TestCase
from weblate.lang.models import Language
from django.core.management import call_command
import os.path
class LanguagesTest(TestCase):
......@@ -196,3 +197,10 @@ class CommandTest(TestCase):
def test_setuplang_noupdate(self):
call_command('setuplang', update=False)
self.assertTrue(Language.objects.exists())
def test_setuplang(self):
testfile = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'plurals.txt'
)
call_command('checklang', testfile)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment