From b2f52bb9cdfc600e300c6855d3a2a51359f69b17 Mon Sep 17 00:00:00 2001 From: Daniel STAN Date: Tue, 21 Oct 2014 20:55:53 +0200 Subject: [PATCH] localisation du body --- gestion/mail/mail.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/gestion/mail/mail.py b/gestion/mail/mail.py index 1637b150..e7abb26f 100644 --- a/gestion/mail/mail.py +++ b/gestion/mail/mail.py @@ -13,6 +13,7 @@ from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.Utils import formatdate from markdown import markdown +from locale_util import setlocale if '/usr/scripts' not in sys.path: sys.path.append('/usr/scripts') @@ -27,6 +28,7 @@ text_mutilang_template = template_path + 'text_multilang' templateLoader = jinja2.FileSystemLoader( searchpath=["/", template_path] ) templateEnv = jinja2.Environment( loader=templateLoader ) +templateEnv.filters['date'] = lambda d: d.strftime('%d %B %Y') # file extension to rendering function map @@ -55,6 +57,15 @@ def get_lang(mail, part, lang, lang_fallback): return l, None, template_path + mail + '/' + part + '/' + l raise ValueError("Language %s nor %s found" % (lang, lang_fallback)) +def gen_local_body(fname, params, lang): + """Génère le texte localisé d'un body""" + locales = { + 'fr': 'fr_FR.UTF-8', + 'en': 'en_US.UTF-8', + } + with setlocale(locales.get(lang, 'C')): + return templateEnv.get_template(fname).render(params) + def body(mail, lang1, lang2, mk, params, charset): file1 = template_path + mail + '/body/' + lang1 file2 = template_path + mail + '/body/' + lang2 @@ -62,14 +73,14 @@ def body(mail, lang1, lang2, mk, params, charset): file1 = file1 + '.' + mk file2 = file2 + '.' + mk if lang1 == lang2 or not os.path.isfile(file2): # No alt language - txt = templateEnv.get_template(file1).render(params) + txt = gen_local_body(file1, params, lang1) ret = [ submessage(txt.encode(charset), 'plain', charset) ] if mk: # compute the html version html = templateEnv.get_template(html_template).render({'body': markup[mk](txt)}) ret.append(submessage(html.encode(charset), 'html', charset)) else: - txt1 = templateEnv.get_template(file1).render(params) - txt2 = templateEnv.get_template(file2).render(params) + txt1 = gen_local_body(file1, params, lang1) + txt2 = gen_local_body(file2, params, lang2) params_txt=dict(params) params_txt.update({'body1': txt1, 'body2':txt2}) txt = templateEnv.get_template(text_mutilang_template).render(params_txt)