From f46fd27aa01a99977e9cf8bd8e1f1b9cb65843e0 Mon Sep 17 00:00:00 2001 From: Sanj Date: Sat, 24 Dec 2011 19:02:32 +0530 Subject: [PATCH] translate stuff --- manifests/ships/views.py | 36 ++++++++++++++++++++++++++++-- manifests/templates/translate.html | 9 +++++++- manifests/urls.py | 1 + 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/manifests/ships/views.py b/manifests/ships/views.py index a9a87af..d0608ea 100644 --- a/manifests/ships/views.py +++ b/manifests/ships/views.py @@ -4,6 +4,8 @@ from django.http import HttpResponse from ships import models import json from django.shortcuts import render_to_response +from django.db.models import Q +from django.template import RequestContext def translate(request, model, field): start = request.GET.get("start", False) @@ -29,7 +31,37 @@ def translate(request, model, field): for obj in qset: val = obj[field] ordArr = [] + isEnglish = True for char in val: + # unichr is the opposite of ord + charOrd = ord(char) + if (charOrd > 500): + isEnglish = False ordArr.append(str(ord(char))) - response += "%s|%s
" % (",".join(ordArr), val,) - return render_to_response("translate.html", {'txt': response}) + if not isEnglish: + response += "%s\n\n" % (val,) + context = RequestContext(request, { + 'txt': response, + 'model': model, + 'field': field + }) + return render_to_response("translate.html", context) + +def dotranslate(request, model, field): + txt = request.POST.get('txt', '') + lines = txt.split("\n\n") + trans_field = field + "_trans" + m = models.__getattribute__(model) + savedArr = [] + for line in lines: + split = line.split("|") + ordArr = split[0].join(",") + val = split[1] + oldVal = [unichr(v) for v in ordArr].join("") + q = Q(**{"%s__isequal" % field: val}) + matches = m.objects.filter(q) + savedArr.append("%s: %s" % (oldVal, val,)) + for m in matches: + m[trans_field] = val + m.save() + return HttpResponse(savedArr.join("\n")) diff --git a/manifests/templates/translate.html b/manifests/templates/translate.html index f5b152f..c92f20f 100644 --- a/manifests/templates/translate.html +++ b/manifests/templates/translate.html @@ -12,7 +12,14 @@ function googleTranslateElementInit() { }, 'google_translate_element'); } - {{ txt|safe }} + {{ txt|linebreaksbr|safe }} + +


+
+ {% csrf_token %} +
+ +
diff --git a/manifests/urls.py b/manifests/urls.py index 03f3839..6fcd15f 100644 --- a/manifests/urls.py +++ b/manifests/urls.py @@ -13,5 +13,6 @@ urlpatterns = patterns('', # Uncomment the next line to enable the admin: (r'translate/(?P[A-Za-z].*?)/(?P[A-Za-z].*?)/', 'ships.views.translate'), + (r'dotranslate/(?P[A-Za-z].*?)/(?P[A-Za-z].*?)/', 'ships.views.dotranslate'), (r'^admin/', include(admin.site.urls)), )