Add detectors module
Some checks failed
CI / test (3.10) (push) Has been cancelled
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

This commit is contained in:
2026-02-02 17:20:55 +00:00
parent 3280f33645
commit 688aa3156d

View File

@@ -0,0 +1,48 @@
"""Gettext detector."""
from pathlib import Path
from typing import Optional
from i18n_guardian.detectors.base import Detector
class GettextDetector(Detector):
"""Detect Gettext library usage."""
@property
def name(self) -> str:
return "gettext"
@property
def patterns(self) -> list:
return ["*.po", "*.pot", "messages.po"]
def detect(self, path: Path) -> Optional[str]:
"""Check for Gettext usage."""
for po_file in path.rglob("*.po"):
if po_file.exists():
return self.name
for pot_file in path.rglob("*.pot"):
if pot_file.exists():
return self.name
for py_file in path.rglob("*.py"):
try:
content = py_file.read_text(encoding="utf-8")
if "_(" in content and ("import gettext" in content or "import _" in content):
return self.name
except OSError:
continue
return None
def get_i18n_functions(self) -> list:
"""Get Gettext function names."""
return [
"_",
"gettext",
"ngettext",
"pgettext",
"npgettext",
]