-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
28 lines (22 loc) · 987 Bytes
/
Copy pathutils.py
File metadata and controls
28 lines (22 loc) · 987 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- coding: utf-8 -*-
import gettext
import logging
import os
from typing import Callable
locale_path = os.path.join(os.path.dirname(__file__), "locale")
logger = logging.getLogger(__name__)
SUPPORTED_LANGS = frozenset(["en", "it", "fur", "vec", "es", "cat"])
_cache: dict[str, gettext.GNUTranslations] = {}
def _load(lang: str) -> gettext.GNUTranslations:
if lang not in _cache:
try:
t = gettext.translation("langAtlasBot", localedir=locale_path, languages=[lang])
except FileNotFoundError:
logger.warning("No translation file for '%s', falling back to 'en'", lang)
t = gettext.translation("langAtlasBot", localedir=locale_path, languages=["en"])
_cache[lang] = t
return _cache[lang]
def get_translator(lang: str) -> tuple[Callable, Callable]:
"""Return (gettext, ngettext) callables for the given language code."""
t = _load(lang if lang in SUPPORTED_LANGS else "en")
return t.gettext, t.ngettext