From 08b41b369dd7653730c40d492b62afa19b6eca84 Mon Sep 17 00:00:00 2001 From: Arturo Espinosa Date: Sat, 5 Jul 2025 22:08:34 -0600 Subject: [PATCH] maint: Use babel.Locale and babel.format_decimal for deprecations * Instead of using locale.getdefaultlocale which is deprecated, let the class method babel.Locale.default take care of the selection of a locale. * Replaced the deprecated babel.numbers.format_number with format_decimal. * LANG is now LOCALE, a babel.Locale object which makes everything tidier and preserves more discovered locale details for the formatting function calls. --- tkfilebrowser/constants.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/tkfilebrowser/constants.py b/tkfilebrowser/constants.py index 05346be..a40de15 100644 --- a/tkfilebrowser/constants.py +++ b/tkfilebrowser/constants.py @@ -24,8 +24,8 @@ Constants and functions """ -import locale -from babel.numbers import format_number +from babel import Locale +from babel.numbers import format_decimal from babel.dates import format_date, format_datetime from datetime import datetime import os @@ -79,10 +79,7 @@ IM_RECENT_24 = os.path.join(PATH, "images", "recent_24.png") # --- translation -try: - LANG = locale.getdefaultlocale()[0] -except ValueError: - LANG = 'en' +LOCALE = Locale.default('LC_MESSAGES') EN = {} FR = {"B": "octets", "MB": "Mo", "kB": "ko", "GB": "Go", "TB": "To", @@ -95,8 +92,8 @@ "Shortcuts": "Raccourcis", "Save As": "Enregistrer sous", "Recent": "Récents", "Recently used": "Récemment utilisés"} LANGUAGES = {"fr": FR, "en": EN} -if LANG[:2] == "fr": - TR = LANGUAGES["fr"] +if LOCALE.language in LANGUAGES: + TR = LANGUAGES[LOCALE.language] else: TR = LANGUAGES["en"] @@ -110,15 +107,15 @@ def _(text): def locale_date(date=None): - return format_date(date, 'short', locale=LANG) + return format_date(date, 'short', locale=LOCALE) def locale_datetime(date=None): - return format_datetime(date, 'EEEE HH:mm', locale=LANG) + return format_datetime(date, 'EEEE HH:mm', locale=LOCALE) def locale_number(nb): - return format_number(nb, locale=LANG) + return format_decimal(nb, locale=LOCALE) SIZES = [_("B"), _("kB"), _("MB"), _("GB"), _("TB")] @@ -126,7 +123,7 @@ def locale_number(nb): # --- locale settings for dates TODAY = locale_date() YEAR = datetime.now().year -DAY = int(format_date(None, 'D', locale=LANG)) +DAY = int(format_date(None, 'D', locale=LOCALE)) # --- functions