From: Michael Tremer Date: Mon, 29 Dec 2025 16:46:50 +0000 (+0000) Subject: web: Add a function to nicely format numbers X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1406e313ecbe96216ded5f11bc043ca974a3038b;p=ipfire.org.git web: Add a function to nicely format numbers Signed-off-by: Michael Tremer --- diff --git a/configure.ac b/configure.ac index d5505a37..aa75daac 100644 --- a/configure.ac +++ b/configure.ac @@ -30,6 +30,7 @@ AC_PROG_SED AM_PATH_PYTHON([3.11]) AX_PYTHON_MODULE([PIL], [fatal]) +AX_PYTHON_MODULE([babel], [fatal]) AX_PYTHON_MODULE([feedparser], [fatal]) AX_PYTHON_MODULE([html2text], [fatal]) AX_PYTHON_MODULE([iso3166], [fatal]) diff --git a/src/web/__init__.py b/src/web/__init__.py index 3e7ee30b..4a653c6a 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -1,5 +1,6 @@ #/usr/bin/python +import babel.numbers import logging import os.path import phonenumbers @@ -56,6 +57,7 @@ class Application(tornado.web.Application): "format_country_name" : self.format_country_name, "format_language_name" : self.format_language_name, "format_month_name" : self.format_month_name, + "format_number" : self.format_number, "format_phone_number" : self.format_phone_number, "format_phone_number_to_e164" : self.format_phone_number_to_e164, "format_phone_number_location" : self.format_phone_number_location, @@ -445,6 +447,9 @@ class Application(tornado.web.Application): return month + def format_number(self, handler, *args, **kwargs): + return babel.numbers.format_number(*args, locale=handler.locale.code, **kwargs) + def format_phone_number(self, handler, number): if not isinstance(number, phonenumbers.PhoneNumber): try: