Files
i18n-guardian/i18n_guardian/detectors/python_gettext.py
7000pctAUTO eed4d7ba9b
Some checks failed
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / build (push) Has been cancelled
CI / test (3.10) (push) Has been cancelled
Add detectors module
2026-02-02 17:20:58 +00:00

53 lines
1.3 KiB
Python

"""Python Gettext detector."""
from pathlib import Path
from typing import Optional
from i18n_guardectors.base import Detector
class PythonGettextDetector(Detector):
"""Detect Python gettext module usage."""
@property
def name(self) -> str:
return "python-gettext"
@property
def patterns(self) -> list:
return ["*.py"]
def detect(self, path: Path) -> Optional[str]:
"""Check for Python gettext usage."""
for py_file in path.rglob("*.py"):
try:
content = py_file.read_text(encoding="utf-8")
if any(
pattern in content
for pattern in [
"import gettext",
"from gettext import",
"gettext.gettext",
"gettext.ngettext",
]
):
return self.name
except OSError:
continue
locale_dir = path / "locale"
if locale_dir.exists():
for _ in locale_dir.rglob("*/LC_MESSAGES/*.mo"):
return self.name
return None
def get_i18n_functions(self) -> list:
"""Get Python gettext function names."""
return [
"_",
"gettext",
"ngettext",
"pgettext",
]