49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
"""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",
|
|
]
|