From 00248665d70fad5bf3c1679db0871ae7418d399f Mon Sep 17 00:00:00 2001 From: Abdo Date: Fri, 19 Apr 2024 12:52:02 +0300 Subject: [PATCH] Avoid locale.getdefaultlocale() if possible (#3143) * Avoid locale.getdefaultlocale() if possible * Suppress warning * Add comment * Apply suggestions from code review --- pylib/anki/lang.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pylib/anki/lang.py b/pylib/anki/lang.py index e68ffb6b2..2cd788f9b 100644 --- a/pylib/anki/lang.py +++ b/pylib/anki/lang.py @@ -5,6 +5,7 @@ from __future__ import annotations import locale import re +import warnings import weakref from typing import TYPE_CHECKING, Any @@ -182,7 +183,14 @@ def get_def_lang(lang: str | None = None) -> tuple[int, str]: """Return lang converted to name used on disk and its index, defaulting to system language or English if not available.""" try: - (sys_lang, enc) = locale.getdefaultlocale() + # getdefaultlocale() is deprecated since Python 3.11, but we need to keep using it as getlocale() behaves differently: https://bugs.python.org/issue38805 + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + (sys_lang, enc) = locale.getdefaultlocale() + except AttributeError: + # this will return a different format on Windows (e.g. Italian_Italy), resulting in us falling back to en_US + # further below + (sys_lang, enc) = locale.getlocale() except: # fails on osx sys_lang = "en_US"