```
### Rule FE-003: Language Selector Must Be In Shared JavaScript
**Language selector function MUST be defined in:**
- `static/storefront/js/storefront-layout.js` for storefront
- `static/store/js/init-alpine.js` for store dashboard
```javascript
// ✅ GOOD: Reusable function with consistent implementation
function languageSelector(currentLang, enabledLanguages) {
return {
isLangOpen: false,
currentLang: currentLang || 'fr',
languages: enabledLanguages || ['en', 'fr', 'de', 'lb'],
languageNames: {
'en': 'English',
'fr': 'Français',
'de': 'Deutsch',
'lb': 'Lëtzebuergesch'
},
languageFlags: {
'en': 'gb',
'fr': 'fr',
'de': 'de',
'lb': 'lu'
},
async setLanguage(lang) {
if (lang === this.currentLang) {
this.isLangOpen = false;
return;
}
try {
const response = await fetch('/api/v1/platform/language/set', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ language: lang })
});
if (response.ok) {
this.currentLang = lang;
window.location.reload();
}
} catch (error) {
console.error('Failed to set language:', error);
}
this.isLangOpen = false;
}
};
}
window.languageSelector = languageSelector;
```
### Rule FE-004: Storefront Must Respect Store's Enabled Languages
```html
{% set enabled_langs = store.storefront_languages if store and store.storefront_languages else ['fr', 'de', 'en'] %}
{% if enabled_langs|length > 1 %}
{% endif %}
```
```html
```
### Rule FE-005: Store Dashboard Shows All Languages
```html
```
---
## API Rules
### Rule API-001: Language Endpoint Must Set Cookie
```python
# ✅ GOOD: Set both cookie and return JSON
@router.post("/language/set")
async def set_language(request: LanguageSetRequest, response: Response):
if request.language not in SUPPORTED_LANGUAGES:
raise HTTPException(status_code=400, detail="Unsupported language")
response.set_cookie(
key="lang",
value=request.language,
max_age=365 * 24 * 60 * 60, # 1 year
httponly=False, # Accessible to JavaScript
samesite="lax",
path="/", # Must be site-wide
)
return {"success": True, "language": request.language}
```
### Rule API-002: Validate Language Codes
```python
# ✅ GOOD: Strict validation
SUPPORTED_LANGUAGES = {"en", "fr", "de", "lb"}
class LanguageSetRequest(BaseModel):
language: str = Field(..., pattern="^(en|fr|de|lb)$")
# ❌ BAD: No validation
class LanguageSetRequest(BaseModel):
language: str # ❌ Accepts any string
```
---
## Template Rules
### Rule TPL-001: Always Provide Language Default
```html
{{ request.state.language|default("fr") }}
{{ request.state.language }}
```
### Rule TPL-002: Check Language Array Length Before Rendering Selector
```html
{% if enabled_langs|length > 1 %}
{% endif %}
```
### Rule TPL-003: Use Consistent Flag Icon Classes
```html
```
---
## JavaScript Rules
### Rule JS-001: Language Names Must Use Native Language
```javascript
// ✅ GOOD: Native language names
languageNames: {
'en': 'English',
'fr': 'Français', // ✅ Not "French"
'de': 'Deutsch', // ✅ Not "German"
'lb': 'Lëtzebuergesch' // ✅ Not "Luxembourgish"
}
// ❌ BAD: English names
languageNames: {
'en': 'English',
'fr': 'French', // ❌ Should be "Français"
'de': 'German', // ❌ Should be "Deutsch"
'lb': 'Luxembourgish' // ❌ Should be "Lëtzebuergesch"
}
```
### Rule JS-002: Flag Codes Must Map Correctly
```javascript
// ✅ GOOD: Correct flag mappings
languageFlags: {
'en': 'gb', // ✅ Great Britain flag for English
'fr': 'fr', // ✅ France flag
'de': 'de', // ✅ Germany flag
'lb': 'lu' // ✅ Luxembourg flag
}
// ❌ BAD: Incorrect mappings
languageFlags: {
'en': 'us', // ❌ US flag is incorrect for general English
'en': 'en', // ❌ 'en' is not a valid flag code
'lb': 'lb' // ❌ 'lb' is not a valid flag code
}
```
### Rule JS-003: Language API Must Use Correct Endpoint
```javascript
// ✅ GOOD: Correct endpoint
fetch('/api/v1/platform/language/set', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ language: lang })
});
// ❌ BAD: Wrong endpoint or method
fetch('/api/v1/language/set', ...); // ❌ Missing /platform
fetch('/api/v1/platform/language', ...); // ❌ Missing /set
fetch('/api/v1/platform/language/set', { method: 'GET' }); // ❌ Should be POST
```
---
## Translation File Rules
### Rule TRANS-001: All Translation Keys Must Exist in All Files
```
static/locales/
├── en.json # Must have ALL keys
├── fr.json # Must have ALL keys
├── de.json # Must have ALL keys
└── lb.json # Must have ALL keys
```
### Rule TRANS-002: Translation Files Must Be Valid JSON
```json
// ✅ GOOD: Valid JSON
{
"common": {
"save": "Save",
"cancel": "Cancel"
}
}
// ❌ BAD: Trailing comma
{
"common": {
"save": "Save",
"cancel": "Cancel", // ❌ Trailing comma
}
}
```
### Rule TRANS-003: Use Nested Structure for Organization
```json
// ✅ GOOD: Organized by section
{
"common": {
"save": "Sauvegarder",
"cancel": "Annuler"
},
"store": {
"dashboard": {
"title": "Tableau de bord"
}
},
"storefront": {
"cart": {
"empty": "Votre panier est vide"
}
}
}
// ❌ BAD: Flat structure
{
"save": "Sauvegarder",
"cancel": "Annuler",
"dashboard_title": "Tableau de bord",
"cart_empty": "Votre panier est vide"
}
```
---
## Quick Reference
### Language Selector Implementation Checklist
- [ ] Function defined in appropriate JS file (`storefront-layout.js` or `init-alpine.js`)
- [ ] Function exported to `window.languageSelector`
- [ ] Uses `tojson|safe` filter for language array
- [ ] Provides default values for both parameters
- [ ] Uses native language names (Français, Deutsch, Lëtzebuergesch)
- [ ] Uses correct flag codes (en→gb, fr→fr, de→de, lb→lu)
- [ ] Calls `/api/v1/platform/language/set` with POST method
- [ ] Reloads page after successful language change
- [ ] Hides selector if only one language enabled (storefront)
- [ ] Shows all languages (store dashboard)
### Database Column Defaults
| Table | Column | Type | Default | Nullable |
|-------|--------|------|---------|----------|
| stores | default_language | VARCHAR(5) | 'fr' | NO |
| stores | dashboard_language | VARCHAR(5) | 'fr' | NO |
| stores | storefront_language | VARCHAR(5) | 'fr' | NO |
| stores | storefront_languages | JSON | ["fr","de","en"] | NO |
| users | preferred_language | VARCHAR(5) | NULL | YES |
| customers | preferred_language | VARCHAR(5) | NULL | YES |
### Files Requiring Language Support
| File | Type | Notes |
|------|------|-------|
| `static/storefront/js/storefront-layout.js` | JS | `languageSelector()` for storefront |
| `app/modules/core/static/store/js/init-alpine.js` | JS | `languageSelector()` for store dashboard |
| `app/modules/core/static/admin/js/init-alpine.js` | JS | `languageSelector()` for admin |
| `app/modules/core/static/merchant/js/init-alpine.js` | JS | `languageSelector()` for merchant |
| `app/templates/store/partials/header.html` | Template | Store dashboard language selector |
| `app/templates/admin/partials/header.html` | Template | Admin language selector |
| `app/templates/merchant/partials/header.html` | Template | Merchant language selector |
| `app/templates/storefront/base.html` | Template | Storefront language selector |
| `app/modules/core/routes/api/platform.py` | API | Language endpoints (`/api/v1/platform/language/*`) |
| `middleware/language.py` | Middleware | Language detection per frontend type |
| `static/locales/*.json` | JSON | Translation files |
---
## Common Violations and Fixes
### Violation: Alpine.js Expression Error
**Symptom:**
```
Alpine Expression Error: expected expression, got '}'
```
**Cause:** Inline x-data with Jinja template variable not properly escaped.
**Fix:** Move to function-based approach with `tojson|safe`.
### Violation: languageFlags is not defined
**Symptom:**
```
Uncaught ReferenceError: languageFlags is not defined
```
**Cause:** `languageSelector` function not loaded or not exported.
**Fix:** Ensure function is defined in JS file and exported to `window.languageSelector`.
### Violation: Wrong flag displayed
**Symptom:** US flag shown for English, or no flag for Luxembourgish.
**Cause:** Incorrect flag code mapping.
**Fix:** Use correct mappings: `en→gb`, `fr→fr`, `de→de`, `lb→lu`.
---
## Validation
Add these checks to CI/CD pipeline:
```bash
# Validate translation files are valid JSON
python -c "import json; json.load(open('static/locales/en.json'))"
python -c "import json; json.load(open('static/locales/fr.json'))"
python -c "import json; json.load(open('static/locales/de.json'))"
python -c "import json; json.load(open('static/locales/lb.json'))"
# Check all translation keys exist in all files
python scripts/validate_translations.py
```
---
**Remember:** Language implementation errors cause immediate user-facing issues. Follow these rules strictly.