Installation
composer require devwizardhq/laravel-localizer
php artisan localizer:install
Follow prompts to configure default locales (e.g., en, fr).
First Translation Use
Add a translation key in resources/lang/en/messages.php:
return [
'welcome' => 'Welcome, :name!',
];
Use it in Laravel:
__('messages.welcome', ['name' => 'John']);
Frontend Integration (React/Vue) Generate TypeScript types and JSON assets:
php artisan localizer:generate
Import in your frontend:
import { translations } from '@/localizer/translations';
console.log(translations.en.messages.welcome); // "Welcome, :name!"
First Auto-Translation Run Google Translate auto-fill for missing keys:
php artisan localizer:translate --locale=fr
config/localizer.php (adjust locales, Google Translate API key, and output paths).resources/js/localizer/ (TypeScript types and JSON files).app/Http/Middleware/LocalizerMiddleware.php (handles locale detection and injection).localizer:generate, localizer:translate, localizer:scan.Backend:
resources/lang/.__() in Blade/Laravel logic.php artisan localizer:generate.Frontend (React Example):
// Use the generated types
const { t } = useTranslation(); // Hook from your i18n library (e.g., i18next)
const welcomeMessage = t('messages.welcome', { name: 'Jane' });
Translation Management:
php artisan localizer:scan to auto-discover keys from __() calls in Blade/Laravel code.php artisan localizer:translate --locale=es.php artisan localizer:validate.Frontend Integration:
php artisan localizer:generate to output:
translations.ts (typed translations object).translations.json (runtime data for your i18n library).LocalizerMiddleware to inject the current locale’s translations into your SPA’s context (e.g., via Inertia or API responses).RTL Support:
config/localizer.php:
'rtl_locales' => ['ar', 'he'],
isRtl() helper in your frontend CSS/JS:
if (localizer.isRtl()) {
document.body.classList.add('rtl');
}
With Inertia.js:
share method in app/Http/Middleware/HandleInertiaRequests.php:
public function share(Request $request): array
{
return array_merge(parent::share($request), [
'localizer' => [
'locale' => app()->getLocale(),
'translations' => json_encode(__('json')),
],
]);
}
const { localizer } = page.props;
With Vue/i18next:
import translations from '@/localizer/translations';
i18n.init({
resources: {
en: { translation: translations.en },
fr: { translation: translations.fr },
},
});
Vendor Translations:
laravel-framework:
// config/localizer.php
'vendor_locales' => [
'laravel-framework' => ['en', 'es'],
],
Custom Scanning:
__() patterns:
// app/Providers/LocalizerServiceProvider.php
Localizer::extendScanner(function (Scanner $scanner) {
$scanner->addPattern('/__\("([^"]+)"\)/', 1);
});
Build-Time Generation:
php artisan localizer:generate during your build process (e.g., in vite.config.ts or webpack.mix.js):
mix.after(() => {
require('laravel-mix').run('localizer:generate');
});
Caching:
// config/localizer.php
'cache' => [
'enabled' => true,
'driver' => 'file', // or 'redis'
],
Lazy Loading:
// Only load the current locale's translations
const { locale } = localizer;
const currentTranslations = translations[locale];
Key Mismatches:
__('messages.welcome')) may not match frontend usage (t('messages.welcome')).php artisan localizer:validate to catch inconsistencies. Use the --fix flag to auto-correct:
php artisan localizer:validate --fix
Google Translate API Limits:
config/localizer.php:
'google_translate' => [
'enabled' => true,
'api_key' => env('GOOGLE_TRANSLATE_API_KEY'),
'max_results' => 1000, // Increase as needed
],
TypeScript Conflicts:
config/localizer.php:
'typescript' => [
'exclude_keys' => ['password.*', 'api.*'],
],
Middleware Overhead:
LocalizerMiddleware may slow down API routes.// app/Http/Kernel.php
protected $middleware = [
// ...
\DevWizardHQ\Localizer\Http\Middleware\LocalizerMiddleware::class,
];
protected $middlewareGroups = [
'web' => [
// ...
],
'api' => [
// Exclude LocalizerMiddleware for API
],
];
Log Translation Issues:
config/localizer.php:
'debug' => [
'enabled' => true,
'log_path' => storage_path('logs/localizer.log'),
],
Verify Generated Assets:
php artisan localizer:generate outputs files to the correct path (default: resources/js/localizer/).Locale Detection:
LocalizerMiddleware:
// app/Http/Middleware/LocalizerMiddleware.php
public function handle($request, Closure $next) {
\Log::info('Detected locale:', [$request->locale(), $request->header('x-localizer-locale')]);
return $next($request);
}
Custom Translator:
Localizer::extendTranslator(function () {
return new CustomTranslator();
});
Post-Processing:
Localizer::afterGenerate(function (array $translations) {
$translations['en']['custom'] = 'dynamic value';
return $translations;
});
Custom Scanner:
__() patterns (e.g., __m('module.key')):
Localizer::extendScanner(function (Scanner $scanner) {
$scanner->addPattern('/__m\(\'([^\']+)\'\)/', 1);
});
Webpack/Vite Plugin:
// vite.config.ts
import { defineConfig } from 'vite';
How can I help you explore Laravel packages today?