feat: initial commit for Paralax creative engineering system

This commit is contained in:
Itsmemarkmalvar
2026-10-12 21:21:33 +08:00
commit 516886dbe7
599 changed files with 227969 additions and 0 deletions

View File

@@ -0,0 +1,156 @@
---
name: i18n-localization
description: Internationalization and localization patterns. Detecting hardcoded strings, managing translations, locale files, RTL support.
when_to_use: "When internationalizing an app, managing translations, detecting hardcoded strings, or adding RTL support."
allowed-tools: Read, Glob, Grep
version: 1.0.0
---
# i18n & Localization
> Internationalization (i18n) and Localization (L10n) best practices.
---
## 1. Core Concepts
| Term | Meaning |
|------|---------|
| **i18n** | Internationalization - making app translatable |
| **L10n** | Localization - actual translations |
| **Locale** | Language + Region (en-US, tr-TR) |
| **RTL** | Right-to-left languages (Arabic, Hebrew) |
---
## 2. When to Use i18n
| Project Type | i18n Needed? |
|--------------|--------------|
| Public web app | ✅ Yes |
| SaaS product | ✅ Yes |
| Internal tool | ⚠️ Maybe |
| Single-region app | ⚠️ Consider future |
| Personal project | ❌ Optional |
---
## 3. Implementation Patterns
### React (react-i18next)
```tsx
import { useTranslation } from 'react-i18next';
function Welcome() {
const { t } = useTranslation();
return <h1>{t('welcome.title')}</h1>;
}
```
### Next.js (next-intl)
```tsx
import { useTranslations } from 'next-intl';
export default function Page() {
const t = useTranslations('Home');
return <h1>{t('title')}</h1>;
}
```
### Python (gettext)
```python
from gettext import gettext as _
print(_("Welcome to our app"))
```
---
## 4. File Structure
```
locales/
├── en/
│ ├── common.json
│ ├── auth.json
│ └── errors.json
├── tr/
│ ├── common.json
│ ├── auth.json
│ └── errors.json
└── ar/ # RTL
└── ...
```
---
## 5. Best Practices
### DO ✅
- Use translation keys, not raw text
- Namespace translations by feature
- Support pluralization
- Handle date/number formats per locale
- Plan for RTL from the start
- Use ICU message format for complex strings
### DON'T ❌
- Hardcode strings in components
- Concatenate translated strings
- Assume text length (German is 30% longer)
- Forget about RTL layout
- Mix languages in same file
---
## 6. Common Issues
| Issue | Solution |
|-------|----------|
| Missing translation | Fallback to default language |
| Hardcoded strings | Use linter/checker script |
| Date format | Use Intl.DateTimeFormat |
| Number format | Use Intl.NumberFormat |
| Pluralization | Use ICU message format |
---
## 7. RTL Support
```css
/* CSS Logical Properties */
.container {
margin-inline-start: 1rem; /* Not margin-left */
padding-inline-end: 1rem; /* Not padding-right */
}
[dir="rtl"] .icon {
transform: scaleX(-1);
}
```
---
## 8. Checklist
Before shipping:
- [ ] All user-facing strings use translation keys
- [ ] Locale files exist for all supported languages
- [ ] Date/number formatting uses Intl API
- [ ] RTL layout tested (if applicable)
- [ ] Fallback language configured
- [ ] No hardcoded strings in components
---
## Script
| Script | Purpose | Command |
|--------|---------|---------|
| `scripts/i18n_checker.py` | Detect hardcoded strings & missing translations | `python scripts/i18n_checker.py <project_path>` |

View File

@@ -0,0 +1,223 @@
#!/usr/bin/env python3
"""Audit locale completeness and likely user-facing hard-coded strings."""
from __future__ import annotations
import argparse
import json
import re
import sys
from pathlib import Path
from typing import Any
SKIP_DIRS = {"node_modules", ".git", ".agents", ".agent", "dist", "build", "__pycache__", ".venv", "venv", ".next", "tests", "test", "spec", "specs"}
CODE_TYPES = {".tsx": "jsx", ".jsx": "jsx", ".ts": "jsx", ".js": "jsx", ".vue": "vue", ".py": "python"}
I18N_PATTERNS = (
re.compile(r"\buseTranslation\s*\("),
re.compile(r"\buseTranslations\s*\("),
re.compile(r"(?<![\w$])t\s*\(\s*[\"']"),
re.compile(r"\$t\s*\("),
re.compile(r"\bgettext\s*\("),
re.compile(r"(?<!\w)_\s*\(\s*[\"']"),
re.compile(r"\bFormattedMessage\b"),
re.compile(r"\bi18n\."),
)
HARDCODED_PATTERNS = {
"jsx": (
re.compile(r">\s*([A-Z][A-Za-z][A-Za-z\s!?.,'-]{2,80})\s*</"),
re.compile(r"(?:title|placeholder|label|alt|aria-label)=[\"']([A-Z][A-Za-z\s!?.,'-]{2,80})[\"']"),
),
"vue": (
re.compile(r">\s*([A-Z][A-Za-z][A-Za-z\s!?.,'-]{2,80})\s*</"),
re.compile(r"(?:title|placeholder|label|alt|aria-label)=[\"']([A-Z][A-Za-z\s!?.,'-]{2,80})[\"']"),
),
"python": (
re.compile(r"\bflash\s*\(\s*[\"']([A-Z][^\"']{4,100})[\"']"),
),
}
def is_skipped(path: Path, root: Path) -> bool:
try:
rel = path.relative_to(root)
except ValueError:
rel = path
return any(part in SKIP_DIRS for part in rel.parts)
def find_locale_files(root: Path) -> list[Path]:
patterns = (
"**/locales/**/*.json", "**/translations/**/*.json", "**/lang/**/*.json",
"**/i18n/**/*.json", "**/messages/*.json", "**/*.po",
)
found: set[Path] = set()
for pattern in patterns:
for path in root.glob(pattern):
if path.is_file() and not is_skipped(path, root):
found.add(path)
return sorted(found)
def flatten_keys(value: Any, prefix: str = "") -> set[str]:
keys: set[str] = set()
if not isinstance(value, dict):
return keys
for key, child in value.items():
name = f"{prefix}.{key}" if prefix else str(key)
if isinstance(child, dict):
keys.update(flatten_keys(child, name))
else:
keys.add(name)
return keys
def check_locale_completeness(locale_files: list[Path], root: Path) -> dict[str, Any]:
if not locale_files:
return {"status": "not_applicable", "files": 0, "languages": [], "issues": [], "notes": ["No locale files found."]}
locales: dict[str, dict[str, set[str]]] = {}
issues: list[dict[str, str]] = []
for path in locale_files:
if path.suffix == ".po":
continue
try:
content = json.loads(path.read_text("utf-8"))
except (OSError, json.JSONDecodeError) as exc:
issues.append({"severity": "high", "file": path.relative_to(root).as_posix(), "issue": f"Invalid locale JSON: {exc}"})
continue
language = path.parent.name
locales.setdefault(language, {})[path.stem] = flatten_keys(content)
languages = sorted(locales)
if len(languages) >= 2:
base = languages[0]
namespaces = set().union(*(set(locales[lang]) for lang in languages))
for namespace in sorted(namespaces):
base_keys = locales[base].get(namespace, set())
for language in languages[1:]:
keys = locales[language].get(namespace, set())
missing = sorted(base_keys - keys)
extra = sorted(keys - base_keys)
if missing:
issues.append({"severity": "high", "file": f"{language}/{namespace}", "issue": f"Missing {len(missing)} key(s): {', '.join(missing[:8])}"})
if extra:
issues.append({"severity": "medium", "file": f"{language}/{namespace}", "issue": f"Has {len(extra)} extra key(s)"})
return {"status": "checked", "files": len(locale_files), "languages": languages, "issues": issues, "notes": []}
def iter_code_files(root: Path):
for path in root.rglob("*"):
if path.is_file() and path.suffix.lower() in CODE_TYPES and not is_skipped(path, root):
yield path
def project_uses_i18n(root: Path, code_files: list[Path], locale_files: list[Path]) -> bool:
if locale_files:
return True
package = root / "package.json"
if package.is_file():
try:
text = package.read_text("utf-8").lower()
if any(name in text for name in ("i18next", "next-intl", "react-intl", "vue-i18n", "@lingui")):
return True
except OSError:
pass
for path in code_files[:200]:
try:
content = path.read_text("utf-8", errors="ignore")
except OSError:
continue
if any(pattern.search(content) for pattern in I18N_PATTERNS):
return True
return False
def check_hardcoded_strings(root: Path, code_files: list[Path], enforce: bool) -> dict[str, Any]:
if not enforce:
return {"status": "not_applicable", "files_checked": 0, "files_using_i18n": 0, "issues": [], "notes": ["No i18n framework or locale files detected."]}
issues: list[dict[str, Any]] = []
files_using_i18n = 0
checked = 0
for path in code_files[:500]:
try:
content = path.read_text("utf-8", errors="ignore")
except OSError:
continue
checked += 1
has_i18n = any(pattern.search(content) for pattern in I18N_PATTERNS)
if has_i18n:
files_using_i18n += 1
if has_i18n:
continue
file_type = CODE_TYPES[path.suffix.lower()]
for pattern in HARDCODED_PATTERNS[file_type]:
for match in pattern.finditer(content):
value = match.group(1).strip()
if value.lower().startswith(("http", "error", "warning")):
continue
line = content.count("\n", 0, match.start()) + 1
issues.append({
"severity": "medium",
"file": path.relative_to(root).as_posix(),
"line": line,
"issue": "Likely user-facing hard-coded string",
"text": value[:100],
})
if len(issues) >= 100:
break
if len(issues) >= 100:
break
if len(issues) >= 100:
break
return {"status": "checked", "files_checked": checked, "files_using_i18n": files_using_i18n, "issues": issues, "notes": []}
def audit(root: Path, strict: bool = False) -> dict[str, Any]:
locale_files = find_locale_files(root)
code_files = list(iter_code_files(root))
enforce = strict or project_uses_i18n(root, code_files, locale_files)
locale = check_locale_completeness(locale_files, root)
code = check_hardcoded_strings(root, code_files, enforce)
findings = locale["issues"] + code["issues"]
high = sum(item["severity"] == "high" for item in findings)
medium = sum(item["severity"] == "medium" for item in findings)
return {
"project": str(root),
"applicable": enforce,
"locale_check": locale,
"code_check": code,
"summary": {"total": len(findings), "high": high, "medium": medium, "passed": high == 0},
}
def main() -> int:
parser = argparse.ArgumentParser(description="Check locale completeness and likely hard-coded UI strings")
parser.add_argument("project", nargs="?", default=".")
parser.add_argument("--strict", action="store_true", help="Scan hard-coded strings even when no i18n setup is detected")
parser.add_argument("--json", action="store_true", dest="as_json")
args = parser.parse_args()
root = Path(args.project).resolve()
if not root.is_dir():
parser.error(f"Project directory does not exist: {root}")
report = audit(root, args.strict)
if args.as_json:
print(json.dumps(report, indent=2, ensure_ascii=False))
else:
print(f"i18n Audit: {root}")
if not report["applicable"]:
print("[SKIP] No i18n framework or locale files detected.")
else:
locale = report["locale_check"]
code = report["code_check"]
print(f"Locale files: {locale['files']} | Languages: {', '.join(locale['languages']) or 'unknown'}")
print(f"Code files checked: {code['files_checked']} | Files using i18n: {code['files_using_i18n']}")
for item in (locale["issues"] + code["issues"])[:30]:
line = f":{item['line']}" if "line" in item else ""
print(f"[{item['severity'].upper()}] {item['file']}{line} - {item['issue']}")
print("[PASS] No blocking i18n issues." if report["summary"]["passed"] else "[FAIL] Blocking i18n issues found.")
return 0 if report["summary"]["passed"] else 1
if __name__ == "__main__":
raise SystemExit(main())