3.9 KiB
3.9 KiB
JSON-Based Translation System for Kobelly
🎉 Benefits of This System
✅ No compilation needed - Just edit JSON files directly
✅ Edit English without breaking translations - Each language is independent
✅ Simple and lightweight - No complex dependencies
✅ Real-time updates - Changes appear immediately after restart
✅ Easy to maintain - Clear JSON structure
📁 File Structure
translations/
├── en.json # English translations
├── de.json # German translations
├── fr.json # French translations
└── nl.json # Dutch translations
🔧 How to Use
1. In Templates
Replace Babel syntax with the new system:
Old (Babel):
<h1>{{ _('Welcome to Kobelly Web Solutions') }}</h1>
<p>{{ _('We create stunning, modern websites') }}</p>
New (JSON-based):
<h1>{{ translate('Welcome to Kobelly Web Solutions') }}</h1>
<p>{{ translate('We create stunning, modern websites') }}</p>
<!-- Or use the short alias -->
<h1>{{ t('Welcome to Kobelly Web Solutions') }}</h1>
<p>{{ t('We create stunning, modern websites') }}</p>
2. Adding New Translations
- Add the string to your template:
<h2>{{ t('New Section Title') }}</h2>
- Add it to the translation files:
// translations/en.json
{
"New Section Title": "New Section Title"
}
// translations/de.json
{
"New Section Title": "Neuer Abschnittstitel"
}
- Restart your Flask app - Changes appear immediately!
3. Language Switching
The system automatically detects language from:
- URL parameter:
?lang=de - Session storage
- Browser preferences
Manual language switching:
<a href="?lang=en">English</a>
<a href="?lang=de">Deutsch</a>
<a href="?lang=fr">Français</a>
<a href="?lang=nl">Nederlands</a>
Or use the built-in selector:
{{ create_language_selector() | safe }}
🛠️ Management Scripts
Extract New Strings
python extract_translations.py
Clean Translation Files
python clean_translations.py
Add New Language
- Add language code to
SUPPORTED_LANGUAGESintranslation_manager.py - Create
translations/[lang].json - Add translations
📝 Example Translation File
{
"Home": "Home",
"Services": "Services",
"About": "About",
"Contact": "Contact",
"Welcome to Kobelly Web Solutions": "Welcome to Kobelly Web Solutions",
"We create stunning, modern websites that drive results": "We create stunning, modern websites that drive results"
}
🔄 Migration from Babel
-
Replace template syntax:
{{ _('text') }}→{{ t('text') }}{{ gettext('text') }}→{{ translate('text') }}
-
Remove Babel dependencies:
- Remove
Flask-Babelfrom requirements.txt - Remove
babel.cfg
- Remove
-
Update app.py:
- Remove Babel imports and configuration
- Add translation manager imports
🚀 Getting Started
- Start the app:
python app.py
-
Edit translations:
- Open
translations/[language].json - Replace English text with translations
- Save and restart app
- Open
-
Test language switching:
- Visit
http://localhost:5000?lang=de - Or use the language selector
- Visit
💡 Tips
- Keep keys descriptive - Use full sentences as keys
- Use consistent naming - Follow the same pattern for similar content
- Test all languages - Make sure translations fit in your layout
- Backup your translations - JSON files are easy to version control
🔧 Troubleshooting
Translations not showing?
- Check that the key exists in the JSON file
- Restart the Flask application
- Check browser console for errors
Language not switching?
- Clear browser cache
- Check session storage
- Verify language code is in
SUPPORTED_LANGUAGES
New strings not appearing?
- Run
python extract_translations.pyto extract new strings - Add translations to all language files
- Restart the application