From eed4d7ba9ba64b0eff14f72626256befc768fb3a Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Mon, 2 Feb 2026 17:20:58 +0000 Subject: [PATCH] Add detectors module --- i18n_guardian/detectors/python_gettext.py | 52 +++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 i18n_guardian/detectors/python_gettext.py 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", + ]