Installation:
composer require cyberspectrum/i18n-bundle
Add to config/bundles.php (Symfony) or config/app.php (Laravel via bridge):
Cyberspectrum\I18nBundle\CyberspectrumI18nBundle::class => ['all' => true],
Configuration: Publish the default config:
php artisan vendor:publish --provider="Cyberspectrum\I18nBundle\CyberspectrumI18nBundle" --tag="config"
Update config/i18n.php with your locales (e.g., ['en', 'fr']) and translation paths.
First Use Case: Load translations in a controller/service:
use Cyberspectrum\I18nBundle\I18n;
class MyController extends Controller {
public function __construct(private I18n $i18n) {}
public function index() {
$translation = $this->i18n->get('messages.welcome', [], 'en');
return response()->json(['message' => $translation]);
}
}
Translation Loading:
$this->i18n->get('key.nested.subkey', ['param' => 'value']);
$this->i18n->setLocale('fr');
$translation = $this->i18n->get('messages.error');
File-Based Translations:
resources/lang/{locale}/messages.php (Laravel-style) or translations/{locale}/messages.yml (Symfony-style).messages.yml:
welcome: "Welcome, {name}!"
error: "An error occurred: {code}"
Integration with Blade (Laravel):
Blade::directive('trans', function ($expression) {
return "<?php echo app('i18n')->get($expression); ?>";
});
@trans('messages.welcome', ['name' => $user->name])
Fallback Locales:
config/i18n.php:
'fallback_locales' => ['en', 'fr'],
Validation Messages:
$validator = Validator::make($data, $rules);
$validator->setTranslator($this->i18n);
Locale Auto-Detection:
$this->i18n->setLocale(request()->header('Accept-Language') ?? 'en');
Caching:
config/i18n.php:
'cache' => [
'enabled' => true,
'driver' => 'file', // or 'redis', 'database'
],
php artisan cache:clear
File Format Conflicts:
$this->i18n->setLoader(new \Cyberspectrum\I18nBundle\Loader\JsonLoader());
Namespace Collisions:
auth.login, auth.register).Service Container Binding:
AppServiceProvider:
$this->app->bind(I18n::class, function ($app) {
return new I18n($app['config']['i18n']);
});
Missing Translations:
storage/logs/laravel.log for TranslationNotFoundException.config/i18n.php:
'paths' => [
resource_path('lang/{locale}'),
],
Locale Overrides:
$this->i18n->setFallbackLocales([]);
Performance:
$start = microtime(true);
$this->i18n->get('key');
$time = microtime(true) - $start;
Custom Loaders:
Cyberspectrum\I18nBundle\Loader\LoaderInterface for databases/APIs:
class ApiLoader implements LoaderInterface {
public function load($locale, $domain, $theme = null) {
return json_decode(file_get_contents("https://api.example.com/translations/{$locale}"));
}
}
Event Listeners:
TranslationLoaded):
$this->i18n->addListener(function ($event) {
if ($event->getKey() === 'messages.welcome') {
$event->setTranslation(strtoupper($event->getTranslation()));
}
});
Middleware for Locale:
public function handle($request, Closure $next) {
$locale = $request->segment(1) ?? config('app.locale');
app('i18n')->setLocale($locale);
return $next($request);
}
How can I help you explore Laravel packages today?