localisation du body

This commit is contained in:
Daniel STAN 2014-10-21 20:55:53 +02:00
parent b7818f0e4f
commit b2f52bb9cd

View file

@ -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)