Commit 63de4112 authored by Michal Čihař's avatar Michal Čihař

Added management command for auto translation

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 5d5737de
......@@ -31,6 +31,33 @@ Example:
./manage.py --author michal@cihar.com add_suggestions weblate master cs /tmp/suggestions-cs.po
auto_translate <project> <component> <language>
-----------------------------------------------
.. django-admin:: auto_translate
.. versionadded:: 2.5
Performs automatic translation based on other component translations.
By default it uses all other components from current project, but you can
specify other source component by ``--source`` parameter.
All changes are by default authored by anonymous user, you can specify another
username by ``--user``.
The ``--overwrite`` and ``--inconsistent`` parameters control which strings
will be updated.
Example:
.. code-block:: sh
./manage.py --user nijel --inconsistent --source phpmyadmin/master phpmyadmin 4-5 cs
.. seealso:: :ref:`auto-translation`
changesite
----------
......
......@@ -27,6 +27,7 @@ Released on ? 2015.
* Improved targeting for whiteboard messages.
* Support for automatic translation across projects.
* Optimized fulltext search index.
* Added management command for auto translation.
weblate 2.4
-----------
......
......@@ -166,6 +166,8 @@ All machine translations are available on single tab on translation page.
.. seealso:: :ref:`machine-translation-setup`
.. _auto-translation:
Automatic translation
---------------------
......
# -*- coding: utf-8 -*-
#
# Copyright © 2012 - 2016 Michal Čihař <michal@cihar.com>
#
# This file is part of Weblate <https://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 optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User
from django.http.request import HttpRequest
from weblate.trans.models import Translation, SubProject
from weblate.accounts.models import get_author_name
from weblate.trans.autotranslate import auto_translate
class Command(BaseCommand):
"""
Command for mass automatic translation.
"""
help = 'performs automatic translation based on other components'
args = '<project> <component> <language>'
option_list = BaseCommand.option_list + (
make_option(
'--user',
default='anonymous',
help=(
'User performing the change',
)
),
make_option(
'--source',
default='',
help=(
'Source component <project/component>'
)
),
make_option(
'--overwrite',
default=False,
action='store_true',
help=(
'Overwrite existing translations in target component'
)
),
make_option(
'--inconsistent',
default=False,
action='store_true',
help=(
'Process only inconsistent translations'
)
),
)
def handle(self, *args, **options):
# Check params
if len(args) != 3:
raise CommandError('Invalid number of parameters!')
# Get translation object
try:
translation = Translation.objects.get(
subproject__project__slug=args[0],
subproject__slug=args[1],
language__code=args[2],
)
except Translation.DoesNotExist:
raise CommandError('No matching translation project found!')
# Get user
try:
user = User.objects.get(username=options['user'])
except User.DoesNotExist:
raise CommandError('User does not exist!')
if options['source']:
parts = options['source'].split('/')
if len(parts) != 2:
raise CommandError('Invalid source component specified!')
try:
subproject = SubProject.objects.get(
project__slug=parts[0],
slug=parts[1],
)
except SubProject.DoesNotExist:
raise CommandError('No matching source component found!')
source = subproject.id
else:
source = ''
result = auto_translate(
user, translation, source,
options['inconsistent'], options['overwrite']
)
self.stdout.write('Updated {0} units'.format(result))
......@@ -23,6 +23,8 @@ Tests for automatic translation
"""
from django.core.urlresolvers import reverse
from django.core.management import call_command
from django.core.management.base import CommandError
from weblate.trans.models import SubProject
from weblate.trans.tests.test_views import ViewTestCase
......@@ -58,14 +60,17 @@ class AutoTranslationTest(ViewTestCase):
)
self.assertRedirects(response, self.translation_url)
def test_different(self):
'''
Tests for automatic translation with different content.
'''
def make_different(self):
self.edit_unit(
'Hello, world!\n',
'Nazdar svete!\n'
)
def test_different(self):
'''
Tests for automatic translation with different content.
'''
self.make_different()
params = {'project': 'test', 'lang': 'cs', 'subproject': 'test-2'}
url = reverse('auto_translation', kwargs=params)
response = self.client.post(url, follow=True)
......@@ -78,3 +83,58 @@ class AutoTranslationTest(ViewTestCase):
# Check we've translated something
translation = self.subproject2.translation_set.get(language_code='cs')
self.assertEqual(translation.translated, 1)
def test_command(self):
call_command(
'auto_translate',
'test',
'test',
'cs',
)
def test_command_different(self):
self.make_different()
call_command(
'auto_translate',
'test',
'test-2',
'cs',
source='test/test',
)
def test_command_errors(self):
self.assertRaises(
CommandError,
call_command,
'auto_translate',
'test',
'test',
'cs',
user='invalid',
)
self.assertRaises(
CommandError,
call_command,
'auto_translate',
'test',
'test',
'cs',
source='invalid',
)
self.assertRaises(
CommandError,
call_command,
'auto_translate',
'test',
'test',
'cs',
source='test/invalid',
)
self.assertRaises(
CommandError,
call_command,
'auto_translate',
'test',
'test',
'xxx',
)
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