localisation du body
This commit is contained in:
parent
b7818f0e4f
commit
b2f52bb9cd
1 changed files with 14 additions and 3 deletions
|
@ -13,6 +13,7 @@ from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from email.Utils import formatdate
|
from email.Utils import formatdate
|
||||||
from markdown import markdown
|
from markdown import markdown
|
||||||
|
from locale_util import setlocale
|
||||||
|
|
||||||
if '/usr/scripts' not in sys.path:
|
if '/usr/scripts' not in sys.path:
|
||||||
sys.path.append('/usr/scripts')
|
sys.path.append('/usr/scripts')
|
||||||
|
@ -27,6 +28,7 @@ text_mutilang_template = template_path + 'text_multilang'
|
||||||
|
|
||||||
templateLoader = jinja2.FileSystemLoader( searchpath=["/", template_path] )
|
templateLoader = jinja2.FileSystemLoader( searchpath=["/", template_path] )
|
||||||
templateEnv = jinja2.Environment( loader=templateLoader )
|
templateEnv = jinja2.Environment( loader=templateLoader )
|
||||||
|
templateEnv.filters['date'] = lambda d: d.strftime('%d %B %Y')
|
||||||
|
|
||||||
|
|
||||||
# file extension to rendering function map
|
# 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
|
return l, None, template_path + mail + '/' + part + '/' + l
|
||||||
raise ValueError("Language %s nor %s found" % (lang, lang_fallback))
|
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):
|
def body(mail, lang1, lang2, mk, params, charset):
|
||||||
file1 = template_path + mail + '/body/' + lang1
|
file1 = template_path + mail + '/body/' + lang1
|
||||||
file2 = template_path + mail + '/body/' + lang2
|
file2 = template_path + mail + '/body/' + lang2
|
||||||
|
@ -62,14 +73,14 @@ def body(mail, lang1, lang2, mk, params, charset):
|
||||||
file1 = file1 + '.' + mk
|
file1 = file1 + '.' + mk
|
||||||
file2 = file2 + '.' + mk
|
file2 = file2 + '.' + mk
|
||||||
if lang1 == lang2 or not os.path.isfile(file2): # No alt language
|
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) ]
|
ret = [ submessage(txt.encode(charset), 'plain', charset) ]
|
||||||
if mk: # compute the html version
|
if mk: # compute the html version
|
||||||
html = templateEnv.get_template(html_template).render({'body': markup[mk](txt)})
|
html = templateEnv.get_template(html_template).render({'body': markup[mk](txt)})
|
||||||
ret.append(submessage(html.encode(charset), 'html', charset))
|
ret.append(submessage(html.encode(charset), 'html', charset))
|
||||||
else:
|
else:
|
||||||
txt1 = templateEnv.get_template(file1).render(params)
|
txt1 = gen_local_body(file1, params, lang1)
|
||||||
txt2 = templateEnv.get_template(file2).render(params)
|
txt2 = gen_local_body(file2, params, lang2)
|
||||||
params_txt=dict(params)
|
params_txt=dict(params)
|
||||||
params_txt.update({'body1': txt1, 'body2':txt2})
|
params_txt.update({'body1': txt1, 'body2':txt2})
|
||||||
txt = templateEnv.get_template(text_mutilang_template).render(params_txt)
|
txt = templateEnv.get_template(text_mutilang_template).render(params_txt)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue