Avoid locale.getdefaultlocale() if possible (#3143)

* Avoid locale.getdefaultlocale() if possible

* Suppress warning

* Add comment

* Apply suggestions from code review
This commit is contained in:
Abdo 2024-04-19 12:52:02 +03:00 committed by GitHub
parent c62e2d8df0
commit 00248665d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 1 deletions

View File

@ -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"