mariuzzo/laravel-js-localization
Export Laravel translation files to JavaScript. Generate a JS bundle (with Lang.js) via artisan to use familiar Laravel-style trans() and pluralization on the frontend. Supports Laravel 4.2 through 8.x, with options to choose files and output path.
.json or .php). This aligns well with SPA (Single-Page App) architectures or decoupled frontend-backend systems where dynamic language switching is required without full page reloads.filecache or Redis) to minimize backend load during runtime.trans(), Lang facade), allowing reuse of existing translation files (.json/.lang.php).public_path() or asset() helpers for dynamic JS file generation.SetLocaleMiddleware) to preload translations based on user preferences or headers.| Risk Area | Mitigation Strategy |
|---|---|
| Translation Sync | Ensure backend and frontend translation files are in sync (CI/CD hooks or scripts). |
| Performance Overhead | Test with large translation sets; consider lazy-loading or splitting JS bundles. |
| SEO Impact | Client-side rendering may delay content visibility; use SSR hybrids (e.g., Inertia.js). |
| Legacy Systems | May require refactoring if relying heavily on server-side trans() helpers. |
| Security | Validate locale inputs to prevent injection (e.g., app()->setLocale() checks). |
Phase 1: Backend Readiness
.json/.lang.php) for compatibility.config/app.php locale fallback and default values.'fallback_locale' => 'en',
'locales' => ['en', 'es', 'fr'],
Phase 2: Frontend Integration
composer require mariuzzo/laravel-js-localization
php artisan vendor:publish --provider="Mariuzzo\LaravelJsLocalization\LaravelJsLocalizationServiceProvider"
config/js-localization.php:
'locales' => ['en', 'es', 'fr'],
'default' => 'en',
'fallback' => 'en',
'json' => true, // Use JSON files
php artisan js-localization:generate
<script src="{{ asset('js/localization.js') }}"></script>
Phase 3: Frontend Usage
// Set locale (e.g., from URL or user preference)
window.jsLocalization.setLocale('es');
// Translate
console.log(window.jsLocalization.trans('messages.welcome')); // "¡Bienvenido!"
import { createI18n } from 'vue-i18n';
const i18n = createI18n({
legacy: false,
locale: window.jsLocalization.getLocale(),
messages: { es: window.jsLocalization.getTranslations('es') },
});
trans() helpers; ensure no conflicts in middleware.| Step | Priority | Dependencies |
|---|---|---|
| Backend config | High | Laravel setup, translation files |
| JS file generation | Medium | Backend config, asset pipeline |
| Frontend integration | High | JS files, framework (if applicable) |
| Testing | Critical | All locales, edge cases (e.g., missing translations) |
| Monitoring | Low | Post-launch performance/errors |
php artisan js-localization:generate.\Log::debug('Missing translation: ' . $key . ' in locale ' . $locale);
jsLocalization.trans() failures) should surface in browser console.window.jsLocalization.setLocale('fr'); // Updates URL hash or query param
localization.en.js, localization.es.js) or lazy-loading.filecache or CDN.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Missing translation key | Broken UI | Fallback to default locale/key. |
| JS bundle not loaded | No translations | Use <noscript> fallback or SSR. |
| Locale mismatch (backend/front) | Inconsistent UI | Validate locale in middleware. |
| Large translation files | Slow JS load | Code-split or lazy-load locales. |
| CDN failure | Broken assets | Local fallback or edge caching. |
hreflang tags if using SSR hybrids).resources/lang/es/*.json and regenerate JS").jsLocalization API usage.How can I help you explore Laravel packages today?