[FIX] email_template: allow to format date in the right language

Format_tz allow to format a date, but if the format was '%B', it was always January regardless lang in context
Now, it possible to use babel, to get the date in the customer language.

So:
    format_tz(object.date_order, format='short', context={'use_babel':1, 'lang': 'fr_BE'})
        will return '5/01/16 22:20'

    format_tz(object.date_order, format='short', context={'use_babel':1, 'lang': 'en_US'})
        will return '1/5/16, 10:20 PM'

    format_tz(object.date_order, context={'use_babel':1, 'lang': 'en_US})
        will return 'Jan 5, 2016, 10:20:31 PM'

    format_tz(object.date_order, context={'use_babel':1, 'lang': 'fr_BE'})
        will return '5 janv. 2016 22:20:31'
This commit is contained in:
Jeremy Kersten 2016-01-07 11:45:52 +01:00
parent 7473f4a3c7
commit 9e6528f77d
1 changed files with 10 additions and 1 deletions

View File

@ -42,9 +42,18 @@ def format_tz(pool, cr, uid, dt, tz=False, format=False, context=None):
if tz:
context['tz'] = tz or pool.get('res.users').read(cr, SUPERUSER_ID, uid, ['tz'])['tz'] or "UTC"
timestamp = datetime.datetime.strptime(dt, tools.DEFAULT_SERVER_DATETIME_FORMAT)
ts = fields.datetime.context_timestamp(cr, uid, timestamp, context)
# Babel allows to format datetime in a specific language without change locale
# So month 1 = January in English, and janvier in French
# Be aware that the default value for format is 'medium', instead of 'short'
# medium: Jan 5, 2016, 10:20:31 PM | 5 janv. 2016 22:20:31
# short: 1/5/16, 10:20 PM | 5/01/16 22:20
if context.get('use_babel'):
# Formatting available here : http://babel.pocoo.org/en/latest/dates.html#date-fields
from babel.dates import format_datetime
return format_datetime(ts, format or 'medium', locale=context.get("lang") or 'en_US')
if format:
return ts.strftime(format)
else: