Commit ffb0b89c authored by Michal Čihař's avatar Michal Čihař

Add URL for translations

parent b813f0af
......@@ -22,7 +22,7 @@ from weblate import appsettings
from trans.util import load_class
# Initialize checks list
SERVICES = {}
MACHINE_TRANSLATION_SERVICES = {}
for path in appsettings.MACHINE_TRANSLATION_SERVICES:
obj = load_class(path)()
SERVICES[obj.mtid] = obj
MACHINE_TRANSLATION_SERVICES[obj.mtid] = obj
......@@ -24,6 +24,7 @@ Tests for AJAX/JS views.
from trans.tests.views import ViewTestCase
from django.core.urlresolvers import reverse
from django.utils import simplejson
class JSViewsTest(ViewTestCase):
......@@ -46,6 +47,19 @@ class JSViewsTest(ViewTestCase):
)
self.assertContains(response, 'No similar strings found')
def test_translate(self):
unit = self.get_unit()
response = self.client.get(
reverse('js-translate', kwargs={'unit_id': unit.id}),
{'service': 'dummy'}
)
self.assertContains(response, 'Ahoj')
data = simplejson.loads(response.content)
self.assertEqual(
data,
['Nazdar světe!', 'Ahoj světe!']
)
def test_get_other(self):
unit = self.get_unit()
response = self.client.get(
......
......@@ -22,11 +22,12 @@ from django.shortcuts import render_to_response, get_object_or_404
from django.views.decorators.cache import cache_page
from weblate import appsettings
from django.template import RequestContext
from django.http import HttpResponse
from django.contrib.auth.decorators import permission_required
from django.http import HttpResponse, HttpResponseBadRequest
from django.contrib.auth.decorators import permission_required, login_required
from django.db.models import Q
from trans.models import Unit, Check, Dictionary
from trans.machine import MACHINE_TRANSLATION_SERVICES
from trans.views.helper import SearchOptions
from trans.decorators import any_permission_required
from trans.views.helper import get_project, get_subproject, get_translation
......@@ -74,6 +75,29 @@ def get_similar(request, unit_id):
'similar': similar,
}))
@login_required
def translate(request, unit_id):
'''
AJAX handler for translating.
'''
unit = get_object_or_404(Unit, pk=int(unit_id))
unit.check_acl(request)
service_name = request.GET.get('service', 'INVALID')
if not service_name in MACHINE_TRANSLATION_SERVICES:
return HttpResponseBadRequest('Invalid service specified')
response = MACHINE_TRANSLATION_SERVICES[service_name].translate(
unit.translation.language.code,
unit.get_source_plurals()[0]
)
return HttpResponse(
json.dumps(response),
mimetype='application/json'
)
def get_other(request, unit_id):
'''
......
......@@ -528,6 +528,11 @@ urlpatterns = patterns(
'trans.views.js.get_similar',
name='js-similar',
),
url(
r'^js/translate/(?P<unit_id>[0-9]+)/$',
'trans.views.js.translate',
name='js-translate',
),
url(
r'^js/other/(?P<unit_id>[0-9]+)/$',
'trans.views.js.get_other',
......
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