Add detectors module
This commit is contained in:
70
i18n_guardian/detectors/vue_i18n.py
Normal file
70
i18n_guardian/detectors/vue_i18n.py
Normal file
@@ -0,0 +1,70 @@
|
||||
"""Vue I18n detector."""
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from i18n_guardian.detectors.base import Detector
|
||||
|
||||
|
||||
class VueI18nDetector(Detector):
|
||||
"""Detect Vue I18n library usage."""
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return "vue-i18n"
|
||||
|
||||
@property
|
||||
def patterns(self) -> list:
|
||||
return ["package.json", "vue.config.js", "vite.config.js"]
|
||||
|
||||
def detect(self, path: Path) -> Optional[str]:
|
||||
"""Check for Vue I18n 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 "vue-i18n" in all_deps:
|
||||
return self.name
|
||||
except (json.JSONDecodeError, OSError):
|
||||
pass
|
||||
|
||||
vue_config = path / "vue.config.js"
|
||||
if vue_config.exists():
|
||||
content = vue_config.read_text(encoding="utf-8")
|
||||
if "vue-i18n" in content:
|
||||
return self.name
|
||||
|
||||
vite_config = path / "vite.config.js"
|
||||
if vite_config.exists():
|
||||
content = vite_config.read_text(encoding="utf-8")
|
||||
if "vue-i18n" in content:
|
||||
return self.name
|
||||
|
||||
for vue_file in path.rglob("*.vue"):
|
||||
try:
|
||||
content = vue_file.read_text(encoding="utf-8")
|
||||
if "$t(" in content or "$tc(" in content:
|
||||
return self.name
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
return None
|
||||
|
||||
def get_i18n_functions(self) -> list:
|
||||
"""Get Vue I18n function names."""
|
||||
return [
|
||||
"$t",
|
||||
"$tc",
|
||||
"$d",
|
||||
"$n",
|
||||
"$te",
|
||||
"t",
|
||||
"tc",
|
||||
"d",
|
||||
"n",
|
||||
]
|
||||
Reference in New Issue
Block a user