1
1
from flask import render_template , flash , redirect , session , url_for , request , g
2
2
from flask .ext .login import login_user , logout_user , current_user , login_required
3
- from app import app , db , lm , oid
3
+ from flask .ext .babel import gettext as _
4
+
5
+ from app import app , db , lm , oid , babel
4
6
from forms import LoginForm , EditForm , PostForm , SearchForm
5
7
from models import User , ROLE_USER , ROLE_ADMIN , Post
6
8
from datetime import datetime
7
9
8
- from config import POSTS_PER_PAGE , MAX_SEARCH_RESULTS
10
+ from config import POSTS_PER_PAGE , MAX_SEARCH_RESULTS , LANGUAGES
9
11
from emails import follower_notification
10
12
11
13
@app .before_request
@@ -27,7 +29,7 @@ def index(page = 1):
27
29
post = Post (body = form .post .data , timestamp = datetime .utcnow (), author = g .user )
28
30
db .session .add (post )
29
31
db .session .commit ()
30
- flash ('Your post is now live!' )
32
+ flash (_ ( 'Your post is now live!' ) )
31
33
return redirect (url_for ('index' ))
32
34
posts = g .user .followed_posts ().paginate (page , POSTS_PER_PAGE , False )
33
35
return render_template ("index.html" ,
@@ -55,7 +57,7 @@ def login():
55
57
@oid .after_login
56
58
def after_login (resp ):
57
59
if resp .identity_url is None or resp .identity_url == "" :
58
- flash ('Invalid login. Please try again' )
60
+ flash (_ ( 'Invalid login. Please try again' ) )
59
61
return redirect (url_for ('login' ))
60
62
user = User .query .filter_by (identity_url = resp .identity_url ).first ()
61
63
if user is None :
@@ -97,7 +99,7 @@ def load_user(id):
97
99
def user (nickname , page = 1 ):
98
100
user = User .query .filter_by (nickname = nickname ).first ()
99
101
if user == None :
100
- flash ('User ' + nickname + ' not found.' )
102
+ flash (_ ( 'User %( nickname)s not found.' , nickname = nickname ) )
101
103
return redirect (url_for ('index' ))
102
104
posts = user .posts .paginate (page , POSTS_PER_PAGE , False )
103
105
return render_template ('user.html' ,
@@ -113,7 +115,7 @@ def edit():
113
115
g .user .about_me = form .about_me .data
114
116
db .session .add (g .user )
115
117
db .session .commit ()
116
- flash ('Your changes have been saved' )
118
+ flash (_ ( 'Your changes have been saved' ) )
117
119
return redirect (url_for ('edit' ))
118
120
else :
119
121
form .nickname .data = g .user .nickname
@@ -134,37 +136,37 @@ def internal_error(error):
134
136
def follow (nickname ):
135
137
user = User .query .filter_by (nickname = nickname ).first ()
136
138
if user == None :
137
- flash ('User' + nickname + ' not found.' )
139
+ flash (_ ( 'User %( nickname)s not found.' , nickname = nickname ) )
138
140
return redirect (url_for ('index' ))
139
141
if user == g .user :
140
- flash ('You can\' t follow yourself!' )
142
+ flash (_ ( 'You can\' t follow yourself!' ) )
141
143
return redirect (url_for ('user' , nickname = nickname ))
142
144
u = g .user .follow (user )
143
145
if u is None :
144
- flash ('Cannot follow ' + nickname + '.' )
146
+ flash (_ ( 'Cannot follow %(nickname)s.' , nickname = nickname ) )
145
147
return redirect (url_for ('user' , nickname = nickname ))
146
148
db .session .add (u )
147
149
db .session .commit ()
148
- flash ('You are now following ' + nickname + '.' )
150
+ flash (_ ( 'You are now following %(nickname)s.' , nickname = nickname ) )
149
151
follower_notification (user , g .user )
150
152
return redirect (url_for ('user' , nickname = nickname ))
151
153
152
154
@app .route ('/unfollow/<nickname>' )
153
155
def unfollow (nickname ):
154
156
user = User .query .filter_by (nickname = nickname ).first ()
155
157
if user == None :
156
- flash ('User ' + nickname + ' not found.' )
158
+ flash (_ ( 'User %( nickname) not found.' , nickname = nickname ) )
157
159
return redirect (url_for ('index' ))
158
160
if user == g .user :
159
- flash ('You can\' t unfollow yourself!' )
161
+ flash (_ ( 'You can\' t unfollow yourself!' ) )
160
162
return redirect (url_for ('user' , nickname = nickname ))
161
163
u = g .user .unfollow (user )
162
164
if u is None :
163
- flash ('Cannot unfollow ' + nickname + '.' )
165
+ flash (_ ( 'Cannot unfollow %(nickname).' , nickname = nickname ) )
164
166
return redirect (url_for ('user' , nickname = nickname ))
165
167
db .session .add (u )
166
168
db .session .commit ()
167
- flash ('You have stopped following ' + nickname + '.' )
169
+ flash (_ ( 'You have stopped following %(nickname)s.' , nickname = nickname ) )
168
170
return redirect (url_for ('user' , nickname = nickname ))
169
171
170
172
@app .route ('/search' , methods = ['GET' ])
@@ -175,3 +177,8 @@ def search():
175
177
return render_template ('search_results.html' ,
176
178
query = query ,
177
179
results = results )
180
+
181
+ @babel .localeselector
182
+ def get_locale ():
183
+ return 'es' #request.accept_languages.best_match(LANGUAGES.keys())
184
+
0 commit comments