sonata-project/translation-bundle
Installation:
composer require sonata-project/translation-bundle
Enable the bundle in config/bundles.php:
SonataProject\TranslationBundle\SonataTranslationBundle::class => ['all' => true],
Configure Locales (config/packages/sonata_translation.yaml):
sonata_translation:
locales: [en, fr, es]
default_locale: en
use_gedmo: true # or false for knplabs
use_knplabs: false
First Use Case:
Gedmo\Translatable\Translatable to your entity and configure TranslatableListener in config/packages/doctrine.yaml:
gedmo_listener:
translatable: true
Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface to your entity and configure TranslatableBehavior in config/packages/doctrine.yaml:
knp_gedmo:
doctrine_behaviors:
translatable: true
Locale Switching:
Use the locale_switcher block in Twig:
{{ render(controller('SonataTranslationBundle:Block:localeSwitcher')) }}
use Gedmo\Mapping\Annotation as Gedmo;
#[Gedmo\Translatable]
class Product
{
#[Gedmo\Translatable(fields={"name", "description"})]
private string $name;
#[Gedmo\Translatable(fields={"description"})]
private string $description;
}
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
class Product implements TranslatableInterface
{
private string $name;
private string $description;
}
AbstractTranslatableAdmin for SonataAdmin:
use Sonata\TranslationBundle\Admin\AbstractTranslatableAdmin;
class ProductAdmin extends AbstractTranslatableAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name')
->add('description');
}
}
RequestLocaleProvider to dynamically set locale from URL:
# config/routes.yaml
sonata_translation_locale:
path: /{_locale}
defaults: { _locale: '%locale%' }
requirements:
_locale: '%sonata_translation.locales%'
TranslationChecker to validate translations:
use Sonata\TranslationBundle\Checker\TranslationChecker;
$checker = $container->get(TranslationChecker::class);
$errors = $checker->check($entity);
{{ app.request.locale }}
{{ 'product.name'|trans }}
sonata-project/translation-bundle recipe for quick setup:
composer require sonata-project/translation-bundle --with-all-dependencies
LocaleProviderInterface for custom logic:
use Sonata\TranslationBundle\Provider\LocaleProviderInterface;
class CustomLocaleProvider implements LocaleProviderInterface
{
public function getLocale(): string
{
return 'custom_locale';
}
}
Register as a service:
services:
App\Provider\CustomLocaleProvider:
tags: ['sonata.translation.locale_provider']
{% block sonata_block %}
{{ render(controller('SonataTranslationBundle:Block:localeSwitcher')) }}
{% endblock %}
Locale context in API Platform:
# config/packages/api_platform.yaml
api_platform:
formats:
jsonld:
mime_types: ['application/ld+json']
context: '@api/contexts/translation'
LocaleProvider in tests:
$this->container->set('sonata.translation.locale_provider', $mockProvider);
You have requested a non-existent parameter "locale".default_locale is set in sonata_translation.yaml and matches a valid locale in the locales array.use_gedmo or use_knplabs (not both) in sonata_translation.yaml.sonata_translation:
gedmo:
translatable_listener_service: gedmo.listener.translatable
Sonata\TranslationBundle\Model\TranslatableInterface (deprecated).Gedmo\Translatable\Translatable or Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface.sonata_block.yaml:
blocks:
sonata.translation.locale_switcher:
class: Sonata\TranslationBundle\Block\LocaleSwitcherBlock
settings:
locales: ['en', 'fr', 'es']
$provider = $container->get('sonata.translation.locale_provider');
dump($provider->getLocale());
TranslationChecker:
$checker = $container->get(TranslationChecker::class);
$errors = $checker->check($entity);
dump($errors);
config/packages/dev/doctrine.yaml:
doctrine:
dbal:
logging: true
profiling: true
php bin/console cache:clear
Gedmo\Translatable\TranslatableListener or Knp\DoctrineBehaviors\Model\TranslatableListener:
use Gedmo\Translatable\TranslatableListener;
class CustomTranslatableListener extends TranslatableListener
{
public function preFlush(UnitOfWork $uow): void
{
// Custom logic
parent::preFlush($uow);
}
}
Register as a service:
services:
App\Listener\CustomTranslatableListener:
tags:
- { name: doctrine.event_listener, event: preFlush }
SonataTranslationBundle's Twig extensions:
use Sonata\TranslationBundle\Twig\TranslationExtension;
class CustomTranslationExtension extends TranslationExtension
{
public function getTranslatedString($id, array $parameters = [], $domain = null, $locale = null)
{
// Custom logic
return parent::getTranslatedString($id, $parameters, $domain, $locale);
}
}
Override in config/packages/twig.yaml:
twig:
globals:
trans: '@App\Twig\CustomTranslationExtension'
LocaleSwitcherBlock:
use Sonata\TranslationBundle\Block\LocaleSwitcherBlock;
class CustomLocaleSwitcherBlock extends LocaleSwitcherBlock
{
public function executeView(Template $template)
{
How can I help you explore Laravel packages today?