mirror of
https://github.com/Hopiu/django-rosetta.git
synced 2026-05-09 13:54:44 +00:00
updated Azure Translator support
This commit is contained in:
parent
49b4c3e964
commit
8785c39483
5 changed files with 63 additions and 48 deletions
|
|
@ -7,7 +7,8 @@ MESSAGES_PER_PAGE = getattr(settings, 'ROSETTA_MESSAGES_PER_PAGE', 10)
|
|||
# Enable Google translation suggestions
|
||||
ENABLE_TRANSLATION_SUGGESTIONS = getattr(settings, 'ROSETTA_ENABLE_TRANSLATION_SUGGESTIONS', False)
|
||||
# Can be obtained for free here: https://ssl.bing.com/webmaster/Developers/AppIds/
|
||||
BING_APP_ID = getattr(settings, 'BING_APP_ID', None)
|
||||
AZURE_CLIENT_ID = getattr(settings, 'AZURE_CLIENT_ID', None)
|
||||
AZURE_CLIENT_SECRET = getattr(settings, 'AZURE_CLIENT_SECRET', None)
|
||||
|
||||
# Displays this language beside the original MSGID in the admin
|
||||
MAIN_LANGUAGE = getattr(settings, 'ROSETTA_MAIN_LANGUAGE', None)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
<link rel="stylesheet" href="{{ADMIN_MEDIA_PREFIX}}css/base.css" type="text/css"/>
|
||||
<link rel="stylesheet" href="{{ADMIN_MEDIA_PREFIX}}css/forms.css" type="text/css"/>
|
||||
<link rel="stylesheet" href="{{ADMIN_MEDIA_PREFIX}}css/changelists.css" type="text/css"/>
|
||||
<link rel="stylesheet" href="{{ADMIN_MEDIA_PREFIX}}css/changelists.css" type="text/css"/>
|
||||
<style type="text/css" media="screen">
|
||||
{% include 'rosetta/css/rosetta.css' %}
|
||||
</style>
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ google.setOnLoadCallback(function() {
|
|||
$('.hide', $(this).parent()).hide();
|
||||
});
|
||||
|
||||
{% if ENABLE_TRANSLATION_SUGGESTIONS and BING_APP_ID %}
|
||||
{% if ENABLE_TRANSLATION_SUGGESTIONS and AZURE_CLIENT_ID and AZURE_CLIENT_SECRET %}
|
||||
$('a.suggest').click(function(e){
|
||||
e.preventDefault();
|
||||
var a = $(this);
|
||||
|
|
@ -16,30 +16,24 @@ google.setOnLoadCallback(function() {
|
|||
var trans=$('textarea',a.parent());
|
||||
var sourceLang = '{{ MESSAGES_SOURCE_LANGUAGE_CODE }}';
|
||||
var destLang = '{{ rosetta_i18n_lang_code }}';
|
||||
var app_id = '{{ BING_APP_ID }}';
|
||||
var apiUrl = "http://api.microsofttranslator.com/V2/Ajax.svc/Translate";
|
||||
|
||||
orig = unescape(orig).replace(/<br\s?\/?>/g,'\n').replace(/<code>/,'').replace(/<\/code>/g,'').replace(/>/g,'>').replace(/</g,'<');
|
||||
a.attr('class','suggesting').html('...');
|
||||
window.onTranslationComplete = function(resp) {
|
||||
trans.val(unescape(resp).replace(/'/g,'\'').replace(/"/g,'"').replace(/%\s+(\([^\)]+\))\s*s/g,' %$1s '));
|
||||
a.hide();
|
||||
};
|
||||
window.onTranslationError = function(response){
|
||||
a.text(response);
|
||||
};
|
||||
var apiData = {
|
||||
onerror: 'onTranslationError',
|
||||
appid: app_id,
|
||||
from: sourceLang,
|
||||
to: destLang,
|
||||
oncomplete: "onTranslationComplete",
|
||||
text: orig
|
||||
};
|
||||
$.ajax({
|
||||
url: apiUrl,
|
||||
data: apiData,
|
||||
dataType: 'jsonp'});
|
||||
|
||||
$.getJSON("/rosetta/translate/", {
|
||||
from: sourceLang,
|
||||
to: destLang,
|
||||
text: orig
|
||||
},
|
||||
function(data) {
|
||||
if (data.success){
|
||||
trans.val(unescape(data.translation).replace(/'/g,'\'').replace(/"/g,'"').replace(/%\s+(\([^\)]+\))\s*s/g,' %$1s '));
|
||||
a.hide();
|
||||
} else {
|
||||
a.text(data.error);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
{% endif %}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,4 +5,5 @@ urlpatterns = patterns('rosetta.views',
|
|||
url(r'^pick/$', 'list_languages', name='rosetta-pick-file'),
|
||||
url(r'^download/$', 'download_file', name='rosetta-download-file'),
|
||||
url(r'^select/(?P<langid>[\w\-]+)/(?P<idx>\d+)/$', 'lang_sel', name='rosetta-language-selection'),
|
||||
url(r'^translate/$', 'translate_text', name='translate_text'),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ from django.shortcuts import render_to_response
|
|||
from django.template import RequestContext
|
||||
from django.utils.encoding import smart_unicode, iri_to_uri
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils import simplejson
|
||||
from django.views.decorators.cache import never_cache
|
||||
from rosetta.conf import settings as rosetta_settings
|
||||
from rosetta.polib import pofile
|
||||
|
|
@ -47,6 +48,7 @@ def home(request):
|
|||
return out_
|
||||
|
||||
storage = get_storage(request)
|
||||
|
||||
version = rosetta.get_version(True)
|
||||
if storage.has('rosetta_i18n_fn'):
|
||||
rosetta_i18n_fn = storage.get('rosetta_i18n_fn')
|
||||
|
|
@ -57,11 +59,7 @@ def home(request):
|
|||
if rosetta_i18n_write:
|
||||
rosetta_i18n_pofile = pofile(rosetta_i18n_fn, wrapwidth=rosetta_settings.POFILE_WRAP_WIDTH)
|
||||
for entry in rosetta_i18n_pofile:
|
||||
entry.md5hash = hashlib.md5(
|
||||
entry.msgid.encode("utf8") +
|
||||
entry.msgstr.encode("utf8") +
|
||||
(entry.msgctxt and entry.msgctxt.encode("utf8") or "")
|
||||
).hexdigest()
|
||||
entry.md5hash = hashlib.md5(entry.msgid.encode("utf8") + entry.msgstr.encode("utf8")).hexdigest()
|
||||
|
||||
else:
|
||||
rosetta_i18n_pofile = storage.get('rosetta_i18n_pofile')
|
||||
|
|
@ -172,11 +170,15 @@ def home(request):
|
|||
storage.set('rosetta_i18n_pofile', rosetta_i18n_pofile)
|
||||
|
||||
# Retain query arguments
|
||||
query_arg = '?_next=1'
|
||||
if 'query' in request.GET or 'query' in request.POST:
|
||||
query_arg += '&query=%s' % request.REQUEST.get('query')
|
||||
query_arg = ''
|
||||
if 'query' in request.REQUEST:
|
||||
query_arg = '?query=%s' % request.REQUEST.get('query')
|
||||
if 'page' in request.GET:
|
||||
query_arg += '&page=%d&_next=1' % int(request.GET.get('page'))
|
||||
if query_arg:
|
||||
query_arg = query_arg + '&'
|
||||
else:
|
||||
query_arg = '?'
|
||||
query_arg = query_arg + 'page=%d' % int(request.GET.get('page'))
|
||||
return HttpResponseRedirect(reverse('rosetta-home') + iri_to_uri(query_arg))
|
||||
rosetta_i18n_lang_name = _(storage.get('rosetta_i18n_lang_name'))
|
||||
rosetta_i18n_lang_code = storage.get('rosetta_i18n_lang_code')
|
||||
|
|
@ -199,14 +201,6 @@ def home(request):
|
|||
page = int(request.GET.get('page'))
|
||||
else:
|
||||
page = 1
|
||||
|
||||
if '_next' in request.GET or '_next' in request.POST:
|
||||
page += 1
|
||||
if page > paginator.num_pages:
|
||||
page = 1
|
||||
query_arg = '?page=%d' % page
|
||||
return HttpResponseRedirect(reverse('rosetta-home') + iri_to_uri(query_arg))
|
||||
|
||||
rosetta_messages = paginator.page(page).object_list
|
||||
if rosetta_settings.MAIN_LANGUAGE and rosetta_settings.MAIN_LANGUAGE != rosetta_i18n_lang_code:
|
||||
|
||||
|
|
@ -235,8 +229,9 @@ def home(request):
|
|||
except AttributeError:
|
||||
ADMIN_MEDIA_PREFIX = settings.STATIC_URL + 'admin/'
|
||||
ADMIN_IMAGE_DIR = ADMIN_MEDIA_PREFIX + 'img/'
|
||||
ENABLE_TRANSLATION_SUGGESTIONS = rosetta_settings.BING_APP_ID and rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS
|
||||
BING_APP_ID = rosetta_settings.BING_APP_ID
|
||||
ENABLE_TRANSLATION_SUGGESTIONS = rosetta_settings.AZURE_CLIENT_ID and rosetta_settings.AZURE_CLIENT_SECRET and rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS
|
||||
AZURE_CLIENT_ID = rosetta_settings.AZURE_CLIENT_ID
|
||||
AZURE_CLIENT_SECRET = rosetta_settings.AZURE_CLIENT_SECRET
|
||||
MESSAGES_SOURCE_LANGUAGE_NAME = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_NAME
|
||||
MESSAGES_SOURCE_LANGUAGE_CODE = rosetta_settings.MESSAGES_SOURCE_LANGUAGE_CODE
|
||||
if storage.has('rosetta_last_save_error'):
|
||||
|
|
@ -339,6 +334,7 @@ def lang_sel(request, langid, idx):
|
|||
"""
|
||||
Selects a file to be translated
|
||||
"""
|
||||
|
||||
storage = get_storage(request)
|
||||
if langid not in [l[0] for l in settings.LANGUAGES]:
|
||||
raise Http404
|
||||
|
|
@ -356,11 +352,7 @@ def lang_sel(request, langid, idx):
|
|||
storage.set('rosetta_i18n_fn', file_)
|
||||
po = pofile(file_)
|
||||
for entry in po:
|
||||
entry.md5hash = hashlib.md5(
|
||||
entry.msgid.encode("utf8") +
|
||||
entry.msgstr.encode("utf8") +
|
||||
(entry.msgctxt and entry.msgctxt.encode("utf8") or "")
|
||||
).hexdigest()
|
||||
entry.md5hash = hashlib.md5(entry.msgid.encode("utf8") + entry.msgstr.encode("utf8")).hexdigest()
|
||||
|
||||
storage.set('rosetta_i18n_pofile', po)
|
||||
try:
|
||||
|
|
@ -389,3 +381,30 @@ def can_translate(user):
|
|||
except Group.DoesNotExist:
|
||||
return False
|
||||
|
||||
from microsofttranslator import Translator, TranslateApiException
|
||||
|
||||
def translate_text(request):
|
||||
language_from = request.GET.get('from', None)
|
||||
language_to = request.GET.get('to', None)
|
||||
text = request.GET.get('text', None)
|
||||
|
||||
if language_from == language_to:
|
||||
data = { 'success' : True, 'translation' : text }
|
||||
else:
|
||||
# run the translation:
|
||||
AZURE_CLIENT_ID = getattr(settings, 'AZURE_CLIENT_ID', None)
|
||||
AZURE_CLIENT_SECRET = getattr(settings, 'AZURE_CLIENT_SECRET', None)
|
||||
|
||||
print AZURE_CLIENT_ID
|
||||
print AZURE_CLIENT_SECRET
|
||||
|
||||
translator = Translator(AZURE_CLIENT_ID, AZURE_CLIENT_SECRET)
|
||||
|
||||
try:
|
||||
translated_text = translator.translate(text, language_to)
|
||||
data = { 'success' : True, 'translation' : translated_text }
|
||||
except TranslateApiException as e:
|
||||
data = { 'success' : False, 'error' : "Translation API Exception: {0}".format(e.message) }
|
||||
|
||||
return HttpResponse(simplejson.dumps(data), mimetype='application/json')
|
||||
|
||||
Loading…
Reference in a new issue