diff --git a/app/models.py b/app/models.py
index 8ef40cc..4d61a74 100644
--- a/app/models.py
+++ b/app/models.py
@@ -91,6 +91,7 @@ class Post(db.Model):
body = db.Column(db.String(140))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), index=True)
+ language = db.Column(db.String(5))
author = db.relationship('User', back_populates='posts')
def __repr__(self):
diff --git a/app/routes.py b/app/routes.py
index d0b8f47..b08ce04 100644
--- a/app/routes.py
+++ b/app/routes.py
@@ -3,11 +3,13 @@ from flask import render_template, flash, redirect, url_for, request, g
from flask_login import login_user, logout_user, current_user, login_required
from werkzeug.urls import url_parse
from flask_babel import _, get_locale
+from langdetect import detect, LangDetectException
from app import app, db
from app.forms import LoginForm, RegistrationForm, EditProfileForm, \
EmptyForm, PostForm, ResetPasswordRequestForm, ResetPasswordForm
from app.models import User, Post
from app.email import send_password_reset_email
+from app.translate import translate
@app.before_request
@@ -24,7 +26,12 @@ def before_request():
def index():
form = PostForm()
if form.validate_on_submit():
- post = Post(body=form.post.data, author=current_user)
+ try:
+ language = detect(form.post.data)
+ except LangDetectException:
+ language = ''
+ post = Post(body=form.post.data, author=current_user,
+ language=language)
db.session.add(post)
db.session.commit()
flash(_('Your post is now live!'))
@@ -198,3 +205,12 @@ def unfollow(username):
return redirect(url_for('user', username=username))
else:
return redirect(url_for('index'))
+
+
+@app.route('/translate', methods=['POST'])
+@login_required
+def translate_text():
+ data = request.get_json()
+ return {'text': translate(data['text'],
+ data['source_language'],
+ data['dest_language'])}
diff --git a/app/static/loading.gif b/app/static/loading.gif
new file mode 100644
index 0000000..d0bce15
Binary files /dev/null and b/app/static/loading.gif differ
diff --git a/app/templates/_post.html b/app/templates/_post.html
index 7e05990..c1e33c0 100644
--- a/app/templates/_post.html
+++ b/app/templates/_post.html
@@ -14,7 +14,17 @@
{{ _('%(username)s said %(when)s',
username=user_link, when=moment(post.timestamp).fromNow()) }}
- {{ post.body }}
+ {{ post.body }}
+ {% if post.language and post.language != g.locale %}
+
+
+ {{ _('Translate') }}
+
+ {% endif %}
diff --git a/app/templates/base.html b/app/templates/base.html
index 2f72507..79d05b3 100644
--- a/app/templates/base.html
+++ b/app/templates/base.html
@@ -56,5 +56,22 @@
{{ moment.include_moment() }}
{{ moment.lang(g.locale) }}
+