translate stuff

This commit is contained in:
Sanj 2011-12-24 19:02:32 +05:30
parent 4c56e363b7
commit f46fd27aa0
3 changed files with 43 additions and 3 deletions

View File

@ -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<br />" % (",".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"))

View File

@ -12,7 +12,14 @@ function googleTranslateElementInit() {
}, 'google_translate_element');
}
</script><script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
{{ txt|safe }}
{{ txt|linebreaksbr|safe }}
<br /><br /><br />
<form action="/dotranslate/{{ model }}/{{ field }}/" method="POST">
{% csrf_token %}
<textarea name="txt" id="txt"></textarea> <br />
<input type="submit" />
</form>
</body>
</html>

View File

@ -13,5 +13,6 @@ urlpatterns = patterns('',
# Uncomment the next line to enable the admin:
(r'translate/(?P<model>[A-Za-z].*?)/(?P<field>[A-Za-z].*?)/', 'ships.views.translate'),
(r'dotranslate/(?P<model>[A-Za-z].*?)/(?P<field>[A-Za-z].*?)/', 'ships.views.dotranslate'),
(r'^admin/', include(admin.site.urls)),
)