digitalstate/platform-locale-bundle
Installation
Add the bundle to your composer.json:
composer require digitalstate/platform-locale-bundle
Register it in config/bundles.php:
return [
// ...
DigitalState\PlatformLocaleBundle\DigitalStatePlatformLocaleBundle::class => ['all' => true],
];
Enable OroLocaleBundle Dependency
Ensure oro/locale-bundle is installed and configured as this bundle extends its functionality:
composer require oro/locale-bundle
First Use Case: Localized Entity Attributes in Twig
Use the localized_value Twig filter to display localized entity attributes dynamically:
{{ entity.title|localized_value }}
Ensure your entity has a getTranslations() method (Oro-compatible) or a localizedFields configuration.
Dynamic Localization in Views
{% for product in products %}
<h3>{{ product.name|localized_value }}</h3>
<p>{{ product.description|localized_value }}</p>
{% endfor %}
app.request.locale).Integration with OroPlatform
Localization system for entity translations. Example entity setup:
// src/Entity/Product.php
use Oro\ORM\Mapping as ORM;
#[ORM\Entity]
class Product {
#[ORM\ManyToMany(targetEntity: Translation::class)]
#[ORM\JoinColumn(fieldName: "translations")]
private $translations;
}
Customizing Localized Fields
# config/oro/locale.yml
oro_locale:
localized_fields:
- { entity: App\Entity\Product, field: name }
- { entity: App\Entity\Product, field: description }
Service-Based Localization
DigitalState\PlatformLocaleBundle\Twig\LocalizedValueExtension service for programmatic use:
use DigitalState\PlatformLocaleBundle\Twig\LocalizedValueExtension;
class MyService {
public function __construct(
private LocalizedValueExtension $localizedValueExtension
) {}
public function getLocalizedTitle($entity) {
return $this->localizedValueExtension->getLocalizedValue($entity, 'title');
}
}
Missing OroLocaleBundle
Class 'Oro\Bundle\LocaleBundle\...' not found.oro/locale-bundle and ensure it’s registered in bundles.php.Incorrect Entity Translation Structure
localized_value filter returns null or throws UndefinedIndexException.getTranslations() method or localizedFields config).Locale Not Detected
// app/Http/Middleware/SetLocale.php
public function handle($request, Closure $next) {
app('oro_locale')->setLocale($request->getPreferredLanguage());
return $next($request);
}
Check Available Locales Dump Oro’s configured locales to verify setup:
dd(app('oro_locale')->getAvailableLocales());
Inspect Entity Translations Debug the raw translations for an entity:
$entity = $entityManager->getRepository(Product::class)->find(1);
dd($entity->getTranslations()->toArray());
Override Twig Extension
Extend the LocalizedValueExtension to add custom logic:
// src/Twig/ExtendedLocalizedValueExtension.php
use DigitalState\PlatformLocaleBundle\Twig\LocalizedValueExtension;
class ExtendedLocalizedValueExtension extends LocalizedValueExtension {
public function getLocalizedValue($entity, $field, $locale = null) {
$value = parent::getLocalizedValue($entity, $field, $locale);
return strtoupper($value); // Example: Force uppercase
}
}
Register it in services.yaml:
services:
app.twig.localized_value_extension:
class: App\Twig\ExtendedLocalizedValueExtension
tags: ['twig.extension']
php bin/console cache:clear
JOIN to fetch translations upfront and avoid N+1 queries:
$query = $entityManager->createQuery('SELECT p, t FROM App\Entity\Product p JOIN p.translations t WHERE t.locale = :locale');
$query->setParameter('locale', $request->getLocale());
How can I help you explore Laravel packages today?