From: Julien Rabier Date: Sat, 22 Dec 2012 14:52:26 +0000 (+0100) Subject: add support for translation via babel X-Git-Url: http://git.cyclocoop.org/?p=cavote.git;a=commitdiff_plain;h=71482ea95ddeb20aab4ad186b22ecf11ef2616a3 add support for translation via babel --- diff --git a/babel.cfg b/babel.cfg new file mode 100644 index 0000000..f0234b3 --- /dev/null +++ b/babel.cfg @@ -0,0 +1,3 @@ +[python: **.py] +[jinja2: **/templates/**.html] +extensions=jinja2.ext.autoescape,jinja2.ext.with_ diff --git a/main.py b/main.py index 4832fd3..d772464 100755 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ from flask import Flask, request, session, g, redirect, url_for, abort, \ render_template, flash from flask_openid import OpenID +from flaskext.babel import Babel, gettext, ngettext import sqlite3 from datetime import date, time, timedelta, datetime import time @@ -21,6 +22,7 @@ app = Flask(__name__) app.config.from_object(__name__) oid = OpenID(app) +babel = Babel(app) def connect_db(): return sqlite3.connect(app.config['DATABASE']) @@ -115,7 +117,7 @@ def create_or_login(resp): openid_url = resp.identity_url user = query_db('select * from users where openid = ?', [openid_url], one=True) if user is not None: - flash(u'Successfully signed in') + flash(gettext(u'Successfully signed in')) connect_user(user) return redirect(oid.get_next_url()) return redirect(url_for('home')) @@ -146,18 +148,18 @@ def password_lost(): BODY = string.join(( "From: %s" % EMAIL, "To: %s" % user['email'], - "Subject: [Cavote] Password lost", + "Subject: [Cavote] %s" % gettext(u"Lost password"), "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()).decode('utf-8'), "X-Mailer: %s" % VERSION, "", - "You have lost your password.", - "This link will log you without password.", - "Don't forget to define a new one as soon a possible!", - "This link will only work one time.", + gettext(u"It seems that you have lost your password."), + gettext(u"This link will log you without password."), + gettext(u"Don't forget to define a new one as soon a possible!"), + gettext(u"This link will only work one time."), "", link, "", - "If you think this mail is not for you, please ignore and delete it." + gettext(u"If you think this mail is not for you, please ignore and delete it.") ), "\r\n") server = smtplib.SMTP(SMTP_SERVER) server.sendmail(EMAIL, [user['email']], BODY) @@ -217,9 +219,7 @@ def user_password(userid): # new (invalid) key key = 'i%s' % keygen() # start with i: invalid key print "\n\nchange key for %s\n" % key # FIXME TMP - g.db.execute('update users set password = ?, key = ? where id = ?', - [crypt(request.form['password'], key), - key, session['user']['id']]) + g.db.execute('update users set password = ?, key = ? where id = ?', [crypt(request.form['password'], key), key, session['user']['id']]) g.db.commit() flash(u'Votre mot de passe a été mis à jour.', 'success') else: @@ -282,20 +282,20 @@ def admin_user_add(): BODY = string.join(( "From: %s" % EMAIL, "To: %s" % user['email'], - "Subject: [Cavote] Welcome", + "Subject: [Cavote] %s" % gettext(u"Welcome"), "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()).decode('utf-8'), "X-Mailer: %s" % VERSION, "", - "Hi %s!" % user['name'], - "Welcome on %s." % TITLE, - "Your account's adresse is : %s." % user['email'], + "%(text)s %(user)s!" % {"text": gettext(u"Hi"), "user": user['name']}, + "%(text2)s %(title)s." % {"text2": gettext(u"Welcome on"), "title": TITLE}, + "%(text3)s %(email)s." % {"text3": gettext(u"Your account address is"), "email": user['email']}, "", - "To log in for the first time and set your password, please follow this link :", + gettext(u"To log in for the first time and set your password, please follow this link :"), link, "" ), "\r\n") server = smtplib.SMTP(SMTP_SERVER) - server.sendmail(EMAIL, [user['email']], BODY) + server.sendmail(EMAIL, [user['email']], BODY.encode('utf-8')) server.quit() flash(u'Le nouvel utilisateur a été créé avec succès', 'success') return redirect(url_for('admin_users')) @@ -606,19 +606,19 @@ def admin_vote_edit(voteid): BODY = string.join(( "From: %s" % EMAIL, "To: %s" % user['email'], - "Subject: [Cavote] A vote has been opened for your group", + "Subject: [Cavote] %s" % gettext(u"A vote has been opened for your group"), "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()).decode('utf-8'), "X-Mailer: %s" % VERSION, "", - "A vote has been opened and you are in a group concerned by it : %s" % request.form['title'], + "%(text)s %(title)s" % {"text": gettext(u"A vote has been opened and you are in a group concerned by it :"), "title": request.form['title']}, "", - "This link will bring you to the form where you will be able to vote :", + gettext(u"This link will bring you to the form where you will be able to vote :"), link, "", - "If you think this mail is not for you, please ignore and delete it." + gettext(u"If you think this mail is not for you, please ignore and delete it.") ), "\r\n") server = smtplib.SMTP(SMTP_SERVER) - server.sendmail(EMAIL, [user['email']], BODY) + server.sendmail(EMAIL, [user['email']], BODY.encode('utf-8')) server.quit() else: flash(u'Vous devez proposer au moins deux choix pour ouvrir le vote.', 'error') @@ -626,15 +626,13 @@ def admin_vote_edit(voteid): isterminated = 1 if vote['is_open']: isopen = 1 - g.db.execute('update votes set title = ?, description = ?, category = ?, is_transparent = ?, is_public = ?, is_open = ?, is_terminated = ?, date_end = ?, reminder_last_days = ? where id = ?', - [request.form['title'], request.form['description'], request.form['category'], transparent, public, isopen, isterminated, date_end, request.form['reminder'], voteid]) + g.db.execute('update votes set title = ?, description = ?, category = ?, is_transparent = ?, is_public = ?, is_open = ?, is_terminated = ?, date_end = ?, reminder_last_days = ? where id = ?', [request.form['title'], request.form['description'], request.form['category'], transparent, public, isopen, isterminated, date_end, request.form['reminder'], voteid]) g.db.commit() vote = query_db('select * from votes where id = ?', [voteid], one=True) flash(u"Le vote a bien été mis à jour.", "success") else: flash(u'Vous devez spécifier un titre.', 'error') - vote['duration'] = (datetime.strptime(vote['date_end'], "%Y-%m-%d") - - datetime.strptime(vote['date_begin'], "%Y-%m-%d")).days + vote['duration'] = (datetime.strptime(vote['date_end'], "%Y-%m-%d") - datetime.strptime(vote['date_begin'], "%Y-%m-%d")).days group = query_db('select name from groups where id = ?', [vote['id_group']], one=True) choices = query_db('select * from choices where id_vote = ?', [voteid]) attachments = query_db('select * from attachments where id_vote = ?', [voteid]) @@ -719,4 +717,3 @@ def admin_vote_deleteattachment(voteid, attachmentid): if __name__ == '__main__': app.run() -