diff --git a/i18n_guardian/detectors/python_gettext.py b/i18n_guardian/detectors/python_gettext.py new file mode 100644 index 0000000..9e41927 --- /dev/null +++ b/i18n_guardian/detectors/python_gettext.py @@ -0,0 +1,52 @@ +"""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", + ]