Skip to content

Commit

Permalink
babel integration
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Feb 1, 2013
1 parent 0bba066 commit cac572c
Show file tree
Hide file tree
Showing 23 changed files with 350 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
app.db
search.db
flask
*.mo

3 changes: 3 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from flask.ext.login import LoginManager
from flask.ext.openid import OpenID
from flask.ext.mail import Mail
from flask.ext.babel import Babel, lazy_gettext
from config import basedir, ADMINS, MAIL_SERVER, MAIL_PORT, MAIL_USERNAME, MAIL_PASSWORD
from momentjs import momentjs

Expand All @@ -13,8 +14,10 @@
lm = LoginManager()
lm.init_app(app)
lm.login_view = 'login'
lm.login_message = lazy_gettext('Please log in to access this page.')
oid = OpenID(app, os.path.join(basedir, 'tmp'))
mail = Mail(app)
babel = Babel(app)

if not app.debug:
import logging
Expand Down
6 changes: 5 additions & 1 deletion app/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask.ext.wtf import Form, TextField, BooleanField, TextAreaField
from flask.ext.wtf import Required, Length
from flask.ext.babel import gettext
from app.models import User

class LoginForm(Form):
Expand All @@ -19,9 +20,12 @@ def validate(self):
return False
if self.nickname.data == self.original_nickname:
return True
if self.nickname.data != User.make_valid_nickname(self.nickname.data):
self.nickname.errors.append(gettext('This nickname has invalid characters. Please use letters, numbers, dots and underscores only.'))
return False
user = User.query.filter_by(nickname = self.nickname.data).first()
if user != None:
self.nickname.errors.append('This nickname is already in use. Please choose another one.')
self.nickname.errors.append(gettext('This nickname is already in use. Please choose another one.'))
return False
return True

Expand Down
5 changes: 5 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from app import db
from app import app
import flask.ext.whooshalchemy as whooshalchemy
import re

ROLE_USER = 0
ROLE_ADMIN = 1
Expand All @@ -26,6 +27,10 @@ class User(db.Model):
backref = db.backref('followers', lazy = 'dynamic'),
lazy = 'dynamic')

@staticmethod
def make_valid_nickname(nickname):
return re.sub('[^a-zA-Z0-9_\.]', '', nickname)

@staticmethod
def make_unique_nickname(nickname):
if User.query.filter_by(nickname = nickname).first() == None:
Expand Down
4 changes: 4 additions & 0 deletions app/static/js/moment-es.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/templates/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{% extends "base.html" %}

{% block content %}
<h1>File Not Found</h1>
<p><a href="{{url_for('index')}}">Back</a></p>
<h1>{{ _('File Not Found') }}</h1>
<p><a href="{{url_for('index')}}">{{ _('Back') }}</a></p>
{% endblock %}
6 changes: 3 additions & 3 deletions app/templates/500.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% extends "base.html" %}

{% block content %}
<h1>An unexpected error has occurred</h1>
<p>The administrator has been notified. Sorry for the inconvenience!</p>
<p><a href="{{url_for('index')}}">Back</a></p>
<h1>{{ _('An unexpected error has occurred') }}</h1>
<p>{{ _('The administrator has been notified. Sorry for the inconvenience!') }}</p>
<p><a href="{{url_for('index')}}">{{ _('Back') }}</a></p>
{% endblock %}
12 changes: 8 additions & 4 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/js/moment.min.js"></script>
{% if g.locale != 'en' %}
<script src="/static/js/moment-{{g.locale}}.min.js"></script>
{% endif %}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
Expand All @@ -24,15 +27,15 @@
</a>
<a class="brand" href="/">microblog</a>
<ul class="nav">
<li><a href="{{ url_for('index') }}">Home</a></li>
<li><a href="{{ url_for('index') }}">{{ _('Home') }}</a></li>
{% if g.user.is_authenticated() %}
<li><a href="{{ url_for('user', nickname = g.user.nickname) }}">Your Profile</a></li>
<li><a href="{{ url_for('logout') }}">Logout</a></li>
<li><a href="{{ url_for('user', nickname = g.user.nickname) }}">{{ _('Your Profile') }}</a></li>
<li><a href="{{ url_for('logout') }}">{{ _('Logout') }}</a></li>
{% endif %}
</ul>
<div class="nav-collapse collapse">
{% if g.user.is_authenticated() %}
<form class="navbar-search pull-right" action="{{url_for('search')}}" method="post" name="search">{{g.search_form.hidden_tag()}}{{g.search_form.search(size=20,placeholder="Search",class="search-query")}}</form>
<form class="navbar-search pull-right" action="{{url_for('search')}}" method="post" name="search">{{g.search_form.hidden_tag()}}{{g.search_form.search(size=20,placeholder=_('Search'),class="search-query")}}</form>
{% endif %}
</div>
</div>
Expand All @@ -45,3 +48,4 @@
</div>
</body>
</html>

8 changes: 4 additions & 4 deletions app/templates/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{% extends "base.html" %}

{% block content %}
<h1>Edit Your Profile</h1>
<h1>{{ _('Edit Your Profile') }}</h1>
{% include 'flash.html' %}
<div class="well">
<form class="form-horizontal" action="" method="post" name="edit">
{{form.hidden_tag()}}
<div class="control-group{% if form.errors.post %} error{% endif %}">
<label class="control-label" for="nickname">Your nickname:</label>
<label class="control-label" for="nickname">{{ _('Your nickname:') }}</label>
<div class="controls">
{{ form.nickname(maxlength = 64, class = "span4") }}
{% for error in form.errors.nickname %}
Expand All @@ -17,7 +17,7 @@ <h1>Edit Your Profile</h1>
</div>
</div>
<div class="control-group{% if form.errors.post %} error{% endif %}">
<label class="control-label" for="about_me">About yourself:</label>
<label class="control-label" for="about_me">{{ _('About yourself:') }}</label>
<div class="controls">
{{ form.about_me(cols = 64, rows = 4, class = "span4") }}
{% for error in form.errors.about_me %}
Expand All @@ -27,7 +27,7 @@ <h1>Edit Your Profile</h1>
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-primary" type="submit" value="Save Changes">
<input class="btn btn-primary" type="submit" value="{{ _('Save Changes') }}">
</div>
</div>
</form>
Expand Down
6 changes: 3 additions & 3 deletions app/templates/follower_email.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p>Dear {{user.nickname}},</p>
<p><a href="{{url_for("user", nickname = follower.nickname, _external = True)}}">{{follower.nickname}}</a> is now a follower.</p>
<p>{{ _('%(nickname)s is now a follower.', nickname = '<a href="' + url_for("user", nickname = follower.nickname, _external = True) + '">' + follower.nickname + '</a>') }}</p>
<table>
<tr valign="top">
<td><img src="{{follower.avatar(50)}}"></td>
Expand All @@ -9,5 +9,5 @@
</td>
</tr>
</table>
<p>Regards,</p>
<p>The <code>microblog</code> admin</p>
<p>{{ _('Regards,') }}</p>
<p>{{ _('The <code>microblog</code> admin') }}</p>
8 changes: 4 additions & 4 deletions app/templates/follower_email.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Dear {{user.nickname}},
{{ _('Dear %(nickname)s,', nickname = user.nickname) }}

{{follower.nickname}} is now a follower. Click on the following link to visit {{follower.nickname}}'s profile page:
{{ _('%(nickname)s is now a follower. Click on the following link to visit %(nickname)s\'s profile page:', nickname = follower.nickname) }}

{{url_for("user", nickname = follower.nickname, _external = True)}}

Regards,
{{ _('Regards,') }}

The microblog admin
{{ _('The microblog admin') }}
16 changes: 8 additions & 8 deletions app/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{% extends "base.html" %}

{% block content %}
<h1>Hi, {{g.user.nickname}}!</h1>
<h1>{{ _('Hi, %(nickname)s!', nickname = g.user.nickname) }}</h1>
{% include 'flash.html' %}
<div class="well">
<form class="form-horizontal" action="" method="post" name="post">
{{form.hidden_tag()}}
<div class="control-group{% if form.errors.post %} error{% endif %}">
<label class="control-label" for="post">Say something:</label>
<label class="control-label" for="post">{{ _('Say something:') }}</label>
<div class="controls">
{{ form.post(size = 30, maxlength = 140) }}
{% for error in form.errors.post %}
Expand All @@ -18,7 +18,7 @@ <h1>Hi, {{g.user.nickname}}!</h1>
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-primary" type="submit" value="Post!">
<input class="btn btn-primary" type="submit" value="{{ _('Post!') }}">
</div>
</div>
</form>
Expand All @@ -28,14 +28,14 @@ <h1>Hi, {{g.user.nickname}}!</h1>
{% endfor %}
<ul class="pager">
{% if posts.has_prev %}
<li class="previous"><a href="{{ url_for('index', page = posts.prev_num) }}">Newer posts</a></li>
<li class="previous"><a href="{{ url_for('index', page = posts.prev_num) }}">{{ _('Newer posts') }}</a></li>
{% else %}
<li class="previous disabled"><a href="#">Newer posts</a></li>
<li class="previous disabled"><a href="#">{{ _('Newer posts') }}</a></li>
{% endif %}
{% if posts.has_next %}
<li class="next"><a href="{{ url_for('index', page = posts.next_num) }}">Older posts</a></li>
<li class="next"><a href="{{ url_for('index', page = posts.next_num) }}">{{ _('Older posts') }}</a></li>
{% else %}
<li class="next disabled"><a href="#">Older posts</a></li>
<li class="next disabled"><a href="#">{{ _('Older posts') }}</a></li>
{% endif %}
</ul>
{% endblock %}
{% endblock %}
10 changes: 5 additions & 5 deletions app/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
</script>
{% include 'flash.html' %}
<div class="well">
<h3>Please Sign In</h3>
<h3>{{ _('Please Sign In') }}</h3>
<form class="form" action="" method="post" name="login">
{{form.hidden_tag()}}
<div class="help-block">Click on your OpenID provider below:</div>
<div class="help-block">{{ _('Click on your OpenID provider below:') }}</div>
<div class="control-group">
{% for pr in providers %}
<a href="javascript:set_openid('{{pr.url}}', '{{pr.name}}');"><img src="/static/img/{{pr.name.lower()}}.png" class="img-polaroid" style="margin:2px;" /></a>
{% endfor %}
</div>
<div class="control-group{% if form.errors.openid %} error{% endif %}">
<label class="control-label" for="openid">Or enter your OpenID here:</label>
<label class="control-label" for="openid">{{ _('Or enter your OpenID here:') }}</label>
<div class="controls">
{{ form.openid(size = 80, class = "span4") }}
{% for error in form.errors.openid %}
Expand All @@ -38,13 +38,13 @@ <h3>Please Sign In</h3>
<div class="control-group">
<div class="controls">
<label class="checkbox" for="remember_me">
{{ form.remember_me }} Remember Me
{{ form.remember_me }} {{ _('Remember Me') }}
</label>
</div>
</div>
<div class="control-group">
<div class="controls">
<input class="btn btn-primary" type="submit" value="Sign In">
<input class="btn btn-primary" type="submit" value="{{ _('Sign In') }}">
</div>
</div>
</form>
Expand Down
4 changes: 3 additions & 1 deletion app/templates/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<tr>
<td width="70px"><a href="{{url_for('user', nickname = post.author.nickname)}}"><img src="{{post.author.avatar(70)}}" /></a></td>
<td>
<p><a href="{{url_for('user', nickname = post.author.nickname)}}">{{post.author.nickname}}</a> said {{momentjs(post.timestamp).fromNow()}}:</p>
{% autoescape false %}
<p>{{ _('%(nickname)s said %(when)s:', nickname = '<a href="%s">%s</a>' % (url_for('user', nickname = post.author.nickname), post.author.nickname), when = momentjs(post.timestamp).fromNow()) }}</p>
{% endautoescape %}
<p><strong>{{post.body}}</strong></p>
</td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion app/templates/search_results.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% extends "base.html" %}

{% block content %}
<h1>Search results for "{{query}}":</h1>
<h1>{{ _('Search results for "%(query)s":', query = query) }}</h1>
{% include 'flash.html' %}
{% for post in results %}
{% include 'post.html' %}
Expand Down
18 changes: 9 additions & 9 deletions app/templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<h1>{{user.nickname}}</h1>
{% if user.about_me %}<p>{{user.about_me}}</p>{% endif %}
{% if user.last_seen %}
<p><em>Last seen: {{momentjs(user.last_seen).calendar()}}</em></p>
<p><em>{{ _('Last seen:') }} {{ momentjs(user.last_seen).calendar() }}</em></p>
{% endif %}
<p>Followers: {{user.followers.count() - 1}} | Following: {{user.followed.count() - 1}} |
<p>{{ _('Followers:') }} {{user.followers.count() - 1}} | {{ _('Following:') }} {{user.followed.count() - 1}} |
{% if user.id == g.user.id %}
<a href="{{url_for('edit')}}">Edit your profile</a>
<a href="{{url_for('edit')}}">{{ _('Edit your profile') }}</a>
{% elif not g.user.is_following(user) %}
<a href="{{url_for('follow', nickname = user.nickname)}}">Follow</a>
<a href="{{url_for('follow', nickname = user.nickname)}}">{{ _('Follow') }}</a>
{% else %}
<a href="{{url_for('unfollow', nickname = user.nickname)}}">Unfollow</a>
<a href="{{url_for('unfollow', nickname = user.nickname)}}">{{ _('Unfollow') }}</a>
{% endif %}
</p>
</div>
Expand All @@ -27,14 +27,14 @@ <h1>{{user.nickname}}</h1>
{% endfor %}
<ul class="pager">
{% if posts.has_prev %}
<li class="previous"><a href="{{ url_for('user', nickname = user.nickname, page = posts.prev_num) }}">Newer posts</a></li>
<li class="previous"><a href="{{ url_for('user', nickname = user.nickname, page = posts.prev_num) }}">{{ _('Newer posts') }}</a></li>
{% else %}
<li class="previous disabled"><a href="#">Newer posts</a></li>
<li class="previous disabled"><a href="#">{{ _('Newer posts') }}</a></li>
{% endif %}
{% if posts.has_next %}
<li class="next"><a href="{{ url_for('user', nickname = user.nickname, page = posts.next_num) }}">Older posts</a></li>
<li class="next"><a href="{{ url_for('user', nickname = user.nickname, page = posts.next_num) }}">{{ _('Older posts') }}</a></li>
{% else %}
<li class="next disabled"><a href="#">Older posts</a></li>
<li class="next disabled"><a href="#">{{ _('Older posts') }}</a></li>
{% endif %}
</ul>
{% endblock %}
Loading

0 comments on commit cac572c

Please sign in to comment.