translation framework

This commit is contained in:
Sanj 2011-12-22 21:09:18 +05:30
parent 533ffc79df
commit 4c56e363b7
5 changed files with 59 additions and 2 deletions

View File

@ -116,6 +116,7 @@ INSTALLED_APPS = (
'django.contrib.admin', 'django.contrib.admin',
# Uncomment the next line to enable admin documentation: # Uncomment the next line to enable admin documentation:
'django.contrib.admindocs', 'django.contrib.admindocs',
'django_extensions'
) )
try: try:

View File

@ -21,17 +21,20 @@ class Ship(models.Model):
number = models.IntegerField(max_length=20) number = models.IntegerField(max_length=20)
date = models.DateField() date = models.DateField()
ship_name = models.CharField(max_length=255) ship_name = models.CharField(max_length=255)
ship_name_trans = models.CharField(max_length=255, blank=True)
captain = models.CharField(max_length=255) captain = models.CharField(max_length=255)
captain_trans = models.CharField(max_length=255, blank=True)
flag = models.CharField(max_length=255) flag = models.CharField(max_length=255)
owner = models.CharField(max_length=255) owner = models.CharField(max_length=255)
owner_trans = models.CharField(max_length=255, blank=True)
port = models.CharField(max_length=255) port = models.CharField(max_length=255)
country = models.CharField(max_length=255) country = models.CharField(max_length=255)
def __unicode__(self): def __unicode__(self):
return "%d: %s" % (self.number, self.ship_name,) return "%d: %s" % (self.number, self.ship_name,)
class Meta: # class Meta:
ordering = ['number'] # ordering = ['number']
def no_of_goods(self): def no_of_goods(self):
return Good.objects.filter(ship=self).count() return Good.objects.filter(ship=self).count()

View File

@ -1 +1,35 @@
# Create your views here. # Create your views here.
from models import *
from django.http import HttpResponse
from ships import models
import json
from django.shortcuts import render_to_response
def translate(request, model, field):
start = request.GET.get("start", False)
end = request.GET.get("end", False)
if not start and not end:
limit = False
else:
limit = True
try:
m = models.__getattribute__(model)
except AttributeError:
return HttpResponse("no such model")
response = ''
if field not in m._meta.get_all_field_names():
return HttpResponse("no such field")
qset = m.objects.values(field).distinct()
if limit:
if not start:
start = 0
if not end:
end = qset.count()
qset = qset[start:end]
for obj in qset:
val = obj[field]
ordArr = []
for char in val:
ordArr.append(str(ord(char)))
response += "%s|%s<br />" % (",".join(ordArr), val,)
return render_to_response("translate.html", {'txt': response})

View File

@ -0,0 +1,18 @@
<!doctype html>
<html>
<head>
</head>
<body>
<div id="google_translate_element"></div><script>
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'ar',
includedLanguages: 'en'
}, 'google_translate_element');
}
</script><script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
{{ txt|safe }}
</body>
</html>

View File

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