Files
7000pctAUTO 05dc2b08e1
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
Add detectors module
2026-02-02 17:20:52 +00:00

76 lines
2.3 KiB
Python

"""i18next detector."""
import json
from pathlib import Path
from typing import Optional
from i18n_guardian.detectors.base import Detector
class I18NextDetector(Detector):
"""Detect i18next library usage."""
@property
def name(self) -> str:
return "i18next"
@property
def patterns(self) -> list:
return ["package.json", "i18next.config.js", "i18n.config.js"]
def detect(self, path: Path) -> Optional[str]:
"""Check for i18next usage."""
package_json = path / "package.json"
if package_json.exists():
try:
with open(package_json, "r", encoding="utf-8") as f:
data = json.load(f)
dependencies = data.get("dependencies", {})
dev_dependencies = data.get("devDependencies", {})
all_deps = {**dependencies, **dev_dependencies}
if "i18next" in all_deps:
return self.name
except (json.JSONDecodeError, OSError):
pass
i18next_config = path / "i18next.config.js"
if i18next_config.exists():
return self.name
i18n_config = path / "i18n.config.js"
if i18n_config.exists():
content = i18n_config.read_text(encoding="utf-8")
if "i18next" in content:
return self.name
locales_dir = path / "locales"
if locales_dir.exists():
for lang_file in locales_dir.glob("*.json"):
content = lang_file.read_text(encoding="utf-8")
try:
data = json.loads(content)
if isinstance(data, dict) and len(data) > 0:
return self.name
except json.JSONDecodeError:
continue
for js_file in path.rglob("*.js"):
try:
content = js_file.read_text(encoding="utf-8")
if "i18n.t(" in content or "i18next.t(" in content:
return self.name
except OSError:
continue
return None
def get_i18n_functions(self) -> list:
"""Get i18next function names."""
return [
"t",
"i18n.t",
"i18next.t",
"useTranslation",
"withTranslation",
]