mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 15:32:23 -04:00
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:
parent
c62e2d8df0
commit
00248665d7
1 changed files with 9 additions and 1 deletions
|
@ -5,6 +5,7 @@ from __future__ import annotations
|
||||||
|
|
||||||
import locale
|
import locale
|
||||||
import re
|
import re
|
||||||
|
import warnings
|
||||||
import weakref
|
import weakref
|
||||||
from typing import TYPE_CHECKING, Any
|
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
|
"""Return lang converted to name used on disk and its index, defaulting to system language
|
||||||
or English if not available."""
|
or English if not available."""
|
||||||
try:
|
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:
|
except:
|
||||||
# fails on osx
|
# fails on osx
|
||||||
sys_lang = "en_US"
|
sys_lang = "en_US"
|
||||||
|
|
Loading…
Reference in a new issue