Skip to content

Commit

Permalink
Merge pull request #83 from poojithamiryala/master
Browse files Browse the repository at this point in the history
Translating messages in one go to the specified language
  • Loading branch information
Onwuagba authored Jun 26, 2020
2 parents ad9d9fe + b0fe23e commit 4c6d0e3
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 10 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ uritemplate==3.0.1
urllib3==1.25.9
whitenoise==5.1.0
wrapt==1.12.1
googletrans==3.0.0
3 changes: 2 additions & 1 deletion smsApp/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .views import userdetails,sendmessage
from .views import userdetails, sendmessage, translateMessages
from django.urls import path
from .views import create_receipents_details, get_recipient_details, save_recipients_details
from rest_framework.schemas.coreapi import AutoSchema
Expand All @@ -12,6 +12,7 @@
path('v1/sms/recipient/create', create_receipents_details),
path('v1/sms/recipient/save', save_recipients_details),
path('v1/sms/recipient/all', get_recipient_details),
path('v1/sms/message/translate', translateMessages),
# path('doc/', doc_view),
path('', doc_view),
]
166 changes: 157 additions & 9 deletions smsApp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,47 @@
from rest_framework.response import Response
from rest_framework import status
from twilio.rest import Client
from django.conf import settings
from django.conf import settings
from django.http import HttpResponse
from django.http import JsonResponse

from .models import Receipent
from .serializers import RecepientSerializer
from googletrans import Translator


# Create your views here.
@api_view(['GET','POST'])
#post and get methods on users
@api_view(['GET', 'POST'])
# post and get methods on users
def userdetails(request):
if request.method == 'GET':
users = user.objects.all()
serialized_users = userserializer(users, many = True)
serialized_users = userserializer(users, many=True)
return Response(serialized_users.data)
elif request.method == 'POST':
serialized_users = userserializer(data=request.data)
if serialized_users.is_valid():
serialized_users.save()
return Response(serialized_users.data, status=status.HTTP_201_CREATED)
return Response(userserializer.errors, status=status.HTTP_400_BAD_REQUEST)

#send message to users using twillio


# send message to users using twillio
def sendmessage(request):
users = user.objects.all()
serialized_users = userserializer(users, many = True)
serialized_users = userserializer(users, many=True)
for number in serialized_users:
phone_number = number.phone_number
message = ('sample message')
clients = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
for recipient in serialized_users:
number = recipient.phone_number
clients.messages.create(to=number,
from_=settings.TWILIO_NUMBER,
body=message)
from_=settings.TWILIO_NUMBER,
body=message)
return HttpResponse("messages sent!", 200)


# Create your views here.

@api_view(['GET'])
Expand Down Expand Up @@ -93,3 +97,147 @@ def save_recipients_details(request):
return JsonResponse(data, status=status.HTTP_200_OK)
except:
return JsonResponse({"error": "There is no recipient with this name"}, status=status.HTTP_200_OK)


@api_view(['GET'])
def translateMessages(request):
if request.method == 'GET':
"""
Translates multiple messages in a single go to the specified language
Need to send message and language(the language you want to translate)
Set of languages supported are
The destination here must be a key of the below dictionary(eg:af)
{
'af': 'afrikaans',
'sq': 'albanian',
'am': 'amharic',
'ar': 'arabic',
'hy': 'armenian',
'az': 'azerbaijani',
'eu': 'basque',
'be': 'belarusian',
'bn': 'bengali',
'bs': 'bosnian',
'bg': 'bulgarian',
'ca': 'catalan',
'ceb': 'cebuano',
'ny': 'chichewa',
'zh-cn': 'chinese (simplified)',
'zh-tw': 'chinese (traditional)',
'co': 'corsican',
'hr': 'croatian',
'cs': 'czech',
'da': 'danish',
'nl': 'dutch',
'en': 'english',
'eo': 'esperanto',
'et': 'estonian',
'tl': 'filipino',
'fi': 'finnish',
'fr': 'french',
'fy': 'frisian',
'gl': 'galician',
'ka': 'georgian',
'de': 'german',
'el': 'greek',
'gu': 'gujarati',
'ht': 'haitian creole',
'ha': 'hausa',
'haw': 'hawaiian',
'iw': 'hebrew',
'hi': 'hindi',
'hmn': 'hmong',
'hu': 'hungarian',
'is': 'icelandic',
'ig': 'igbo',
'id': 'indonesian',
'ga': 'irish',
'it': 'italian',
'ja': 'japanese',
'jw': 'javanese',
'kn': 'kannada',
'kk': 'kazakh',
'km': 'khmer',
'ko': 'korean',
'ku': 'kurdish (kurmanji)',
'ky': 'kyrgyz',
'lo': 'lao',
'la': 'latin',
'lv': 'latvian',
'lt': 'lithuanian',
'lb': 'luxembourgish',
'mk': 'macedonian',
'mg': 'malagasy',
'ms': 'malay',
'ml': 'malayalam',
'mt': 'maltese',
'mi': 'maori',
'mr': 'marathi',
'mn': 'mongolian',
'my': 'myanmar (burmese)',
'ne': 'nepali',
'no': 'norwegian',
'ps': 'pashto',
'fa': 'persian',
'pl': 'polish',
'pt': 'portuguese',
'pa': 'punjabi',
'ro': 'romanian',
'ru': 'russian',
'sm': 'samoan',
'gd': 'scots gaelic',
'sr': 'serbian',
'st': 'sesotho',
'sn': 'shona',
'sd': 'sindhi',
'si': 'sinhala',
'sk': 'slovak',
'sl': 'slovenian',
'so': 'somali',
'es': 'spanish',
'su': 'sundanese',
'sw': 'swahili',
'sv': 'swedish',
'tg': 'tajik',
'ta': 'tamil',
'te': 'telugu',
'th': 'thai',
'tr': 'turkish',
'uk': 'ukrainian',
'ur': 'urdu',
'uz': 'uzbek',
'vi': 'vietnamese',
'cy': 'welsh',
'xh': 'xhosa',
'yi': 'yiddish',
'yo': 'yoruba',
'zu': 'zulu',
'fil': 'Filipino',
'he': 'Hebrew'
}
"""
if request.query_params.get('language') is None:
return JsonResponse({"error": "Enter the language you want to translate"},
status=status.HTTP_400_BAD_REQUEST)
if len(request.GET.getlist('message')) == 0:
return JsonResponse({"error": "Enter message"}, status=status.HTTP_400_BAD_REQUEST)
try:
messages = request.GET.getlist('message')
language = request.query_params.get('language')
# Customizing service URL We can use another google translate domain for translation. If multiple URLs
# are provided it then randomly chooses a domain.
translator = Translator(service_urls=[
'translate.google.com',
'translate.google.co.kr',
])
translations = translator.translate(messages, dest=language)
result = []
for translation in translations:
result.append({
'original text': translation.origin,
'translated text': translation.text
})
return JsonResponse({"message": "Translated all the texts successfully", "data": result},
status=status.HTTP_200_OK)
except Exception as error:
return JsonResponse({"error": error}, status=status.HTTP_400_BAD_REQUEST)

0 comments on commit 4c6d0e3

Please sign in to comment.