diff --git a/i18n_guardian/detectors/gettext.py b/i18n_guardian/detectors/gettext.py new file mode 100644 index 0000000..93c2380 --- /dev/null +++ b/i18n_guardian/detectors/gettext.py @@ -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", + ]