Installation
Add the bundle to your composer.json:
composer require dinecat/i18n-bundle
Register it in config/bundles.php:
return [
// ...
Dinecat\I18nBundle\DinecatI18nBundle::class => ['all' => true],
];
Basic Configuration
Configure locales in config/packages/dinecat_i18n.yaml:
dinecat_i18n:
locales: ['en', 'fr', 'de']
default_locale: 'en'
First Use Case Translate a template variable in Twig:
{{ 'Hello, %name%'|trans({'name': user.name}) }}
Ensure translation files exist in translations/messages.{locale}.yml:
# translations/messages.fr.yml
"Hello, %name%": Bonjour, %name%
Middleware Integration
Use Symfony’s LocaleListener or create a custom middleware to set the locale via URL, session, or header:
// src/EventListener/LocaleListener.php
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$locale = $request->getPreferredLanguage(['en', 'fr', 'de']);
$request->setLocale($locale);
}
Route-Based Locale Configure routes to include locale prefixes:
# config/routes.yaml
_locale:
resource: "@DinecatI18nBundle/Resources/config/routing/locale.yaml"
Entity Translation
Use the bundle’s Translatable trait for Doctrine entities:
use Dinecat\I18nBundle\Doctrine\ORM\Mapping\Annotation as I18n;
/**
* @I18n\TranslationDomain("messages")
*/
class Product
{
/**
* @I18n\Translatable
*/
private $name;
}
Populate translations via CLI or admin panel.
Array/Data Translation Translate arrays/dictionaries in controllers:
use Dinecat\I18nBundle\Translation\Translator;
public function show(Translator $translator)
{
$data = ['greeting' => 'Hello'];
$translated = $translator->trans($data, null, 'fr');
// Returns ['greeting' => 'Bonjour']
}
Custom Filters Extend Twig with custom filters for nested translations:
{% set user = {'name': 'John'} %}
{{ 'Welcome, %name%!'|trans(user)|raw }}
Register custom filters in twig.config.php:
$twig->addFilter(new \Twig\TwigFilter('custom_trans', [$translator, 'trans']));
Translation Domains
Use domains to separate translation scopes (e.g., validation, admin):
{{ 'error.invalid'|trans({}, 'validation') }}
config/packages/dinecat_i18n.yaml:
dinecat_i18n:
fallback_locales: ['en', 'fr']
Ensures fr_CA falls back to fr then en.Missing Translation Files
No translation found for key errors.translations/{domain}.{locale}.yml (e.g., messages.fr.yml).php bin/console debug:translation to validate keys.Locale Not Persisting
$request->getSession()->set('_locale', $locale);
Doctrine Entity Translation Not Working
@Translatable fields ignore locale changes.Translation entity is mapped correctly.Translatable trait is applied to the field, not the class.translation_domain is set (e.g., @TranslationDomain("products")).Caching Issues
php bin/console cache:clear
dev environment for testing:
# config/packages/dinecat_i18n.yaml
dinecat_i18n:
cache: false
Log Missing Translations Enable debug mode to log missing keys:
# config/packages/monolog.yaml
monolog:
handlers:
main:
level: debug
channels: ["!event"]
Inspect Translator Service Dump the translator’s loaded catalog:
dump($translator->getCatalogue()->getLocale());
Custom Translator Extend the translator to add logic (e.g., pluralization):
use Dinecat\I18nBundle\Translation\Translator as BaseTranslator;
class CustomTranslator extends BaseTranslator
{
public function transPlural($id, array $parameters = [], $locale = null, $domain = null)
{
// Custom pluralization logic
}
}
Register as a service:
# config/services.yaml
services:
App\Translation\CustomTranslator:
decorates: 'dinecat_i18n.translator'
arguments: ['@App\Translation\CustomTranslator.inner']
Dynamic Translation Loading Load translations from a database or API:
$translator->addResource(
'yml',
file_get_contents($dbTranslation->getContent()),
$locale,
$domain
);
Twig Extensions Add custom Twig functions for complex translations:
$twig->addFunction(new \Twig\TwigFunction('trans_with_fallback', [$translator, 'trans'], ['is_safe' => ['html']]));
How can I help you explore Laravel packages today?