[FIX] tools : use babel to format timedelta
Babel is already taking the language into account while formating a time delta into a readable string like '2 minutes ago'. _format_time_ago was put in translate.py file to be able to use the _ translate class. But it can be placed in misc like other format function already using babel. This fix the PR #34624 and is linked to Task ID : 2028059 Task ID: 2055847 Closes PR #35806 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
@@ -6,8 +6,7 @@ from hashlib import sha256
|
||||
import hmac
|
||||
|
||||
from odoo import fields, models, api, _
|
||||
from odoo.tools.translate import _format_time_ago
|
||||
from odoo.tools.misc import _consteq
|
||||
from odoo.tools.misc import _consteq, _format_time_ago
|
||||
from odoo.http import request
|
||||
|
||||
|
||||
|
||||
@@ -1285,6 +1285,15 @@ def format_time(env, value, tz=False, time_format='medium', lang_code=False):
|
||||
|
||||
return babel.dates.format_time(value, format=time_format, locale=locale)
|
||||
|
||||
|
||||
def _format_time_ago(env, time_delta, lang_code=False):
|
||||
if not lang_code:
|
||||
langs = [code for code, _ in env['res.lang'].get_installed()]
|
||||
lang_code = env.context['lang'] if env.context.get('lang') in langs else (env.user.company_id.partner_id.lang or langs[0])
|
||||
locale = babel.Locale.parse(lang_code)
|
||||
return babel.dates.format_timedelta(-time_delta, add_direction=True, locale=locale)
|
||||
|
||||
|
||||
def format_amount(env, amount, currency, lang_code=False):
|
||||
fmt = "%.{0}f".format(currency.decimal_places)
|
||||
lang = env['res.lang']._lang_get(lang_code or env.context.get('lang') or 'en_US')
|
||||
|
||||
@@ -1151,36 +1151,3 @@ def load_language(cr, lang):
|
||||
env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
|
||||
installer = env['base.language.install'].create({'lang': lang})
|
||||
installer.lang_install()
|
||||
|
||||
def _format_time_ago(env, time):
|
||||
# Set Context to be used by translate _get_Lang (by introspection)
|
||||
# DO NOT REMOVE THIS context VARIABLE
|
||||
context = env.context
|
||||
seconds = time.total_seconds()
|
||||
days = round(seconds / (60 * 60 * 24))
|
||||
|
||||
if not days:
|
||||
if seconds < 10:
|
||||
return _("just now")
|
||||
if seconds / 60 < 1:
|
||||
return _("%s seconds ago") % round(seconds)
|
||||
if seconds < 120:
|
||||
return _("a minute ago")
|
||||
if seconds / 3600 < 1:
|
||||
return _("%s minutes ago") % round(seconds / 60)
|
||||
if seconds < 7200:
|
||||
return _("an hour ago")
|
||||
return _("%s hours ago") % round(seconds / (60*60))
|
||||
else:
|
||||
if days == 1:
|
||||
return _("yesterday")
|
||||
if days < 7:
|
||||
return _("%s days ago") % days
|
||||
if days < 31:
|
||||
nbr_weeks = round(days / 7)
|
||||
return _("%s week%s ago") % (nbr_weeks, '' if nbr_weeks == 1 else 's')
|
||||
if days < 365:
|
||||
nbr_months = round(days / 30)
|
||||
return _("%s month%s ago") % (nbr_months, '' if nbr_months == 1 else 's')
|
||||
nbr_years = round(days / 365)
|
||||
return _("%s year%s ago") % (nbr_years, '' if nbr_years == 1 else 's')
|
||||
|
||||
Reference in New Issue
Block a user