SyliusCon 2025
Early Bird Deal
LogoLogo
πŸ›£οΈ RoadmapπŸ’» Sylius DemoπŸ’¬ Community Slack
  • Sylius Documentation
  • Sylius Plugins
  • Sylius Stack
  • πŸ“–Sylius 2.0 Documentation
    • Organization
      • Release Cycle
      • Backwards Compatibility Promise
      • Sylius Team
      • Sylius Roadmap
  • Getting Started with Sylius
    • Installation
    • Basic Configuration
    • Shipping & Payment
    • First Product
    • Customizing the Shop
    • Customizing Business Logic
    • Using API
    • Installing Plugins
    • Deployment
    • Summary
  • The Book
    • Introduction to Sylius
    • Installation
      • System Requirements
      • Sylius CE Installation
        • Sylius CE Installation with Docker
      • βž•Sylius Plus Installation
      • Upgrading Sylius CE
      • Upgrading Sylius Plus
    • Architecture
      • Architecture Overview
      • Architectural Drivers
      • Resource Layer
      • State Machine
      • Translations
      • E-Mails
      • Contact
      • Fixtures
      • Events
    • Configuration
      • Channels
      • Locales
      • Currencies
    • Customers
      • Customer & ShopUser
      • βž•Customer Pools
      • AdminUser
      • Addresses
        • Countries
        • Zones
        • Addresses
        • Address Book
    • Products
      • Products
      • Product Reviews
      • Product Associations
      • Attributes
      • Pricing
      • Catalog Promotions
      • Taxons
      • Inventory
      • βž•Multi-Source Inventory
      • Search
    • Carts & Orders
      • Orders
      • Cart flow
      • Taxation
      • Adjustments
      • Cart Promotions
      • Coupons
      • Payments
      • 🧩Invoices
      • Shipments
    • Support
    • Contributing
      • Contributing Code
        • Submitting a Patch
        • ⚠️Security Issues
        • Coding Standards
        • Conventions
        • Sylius License and Trademark
      • Contributing Translations
      • Key Contributors
  • The Customization Guide
    • Customizing Models
      • How to add a custom model?
      • How to add a custom translatable model?
    • Customizing Forms
      • How to add a live form for a custom model?
    • Customizing Styles
    • Customizing Validation
    • Customizing Menus
    • Customizing Templates
    • Customizing Translations
    • Customizing Flashes
    • Customizing State Machines
    • Customizing Grids
    • Customizing Fixtures
    • Customizing API
    • Customizing Serialization of API
    • Customizing Payments
      • How to integrate a Payment Gateway as a Plugin?
  • πŸ§‘β€πŸ³The Cookbook
  • How to resize images?
  • How to add one image to an entity?
  • How to add multiple images to an entity?
  • Sylius 1.X Documentation
    • πŸ““Sylius 1.x Documentation
Powered by GitBook
LogoLogo

Developer

  • Community
  • Online Course

About

  • Team

Β© 2025 Sylius. All Rights Reserved

On this page
  • Fallback Translations
  • How to add a new translation programmatically?
  • Learn more

Was this helpful?

Edit on GitHub
  1. The Book
  2. Architecture

Translations

Sylius uses the approach of personal translations - where each entity is bound with a translation entity, that has its table (instead of keeping all translations in one table for the whole system). This results in the ProductTranslation class and sylius_product_translation table for the Product entity.

The logic of handling translations in Sylius is in the ResourceBundle

The fields of an entity that are meant to be translatable are saved on the translation entity, only their getters and setters are also on the original model.

Let’s see an example:

Assuming that we would like to have a translatable model of a Supplier, we need a Supplier class and a SupplierTranslation class.

<?php

namespace App\Entity;

use Sylius\Component\Resource\Model\AbstractTranslation;

class SupplierTranslation extends AbstractTranslation
{
    /**
     * @var string
     */
    protected $name;

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }
}

The actual entity has access to its translation by using the TranslatableTrait which provides the getTranslation() method.

Remember that the Translations collection of the entity (from the TranslatableTrait) has to be initialized in the constructor!

<?php

namespace App\Entity;

use Sylius\Component\Resource\Model\TranslatableInterface;
use Sylius\Component\Resource\Model\TranslatableTrait;

class Supplier implements TranslatableInterface
{
    use TranslatableTrait {
        __construct as private initializeTranslationsCollection;
    }

    public function __construct()
    {
        $this->initializeTranslationsCollection();
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->getTranslation()->getName();
    }

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->getTranslation()->setName($name);
    }
}

Fallback Translations

The getTranslation() method gets a translation for the current locale, while we are in the shop, but we can also manually impose the locale - getTranslation('pl_PL') will return a Polish translation if there is a translation in this locale.

But when the translation for the chosen locale is unavailable, instead the translation for the fallback locale (the one that was either set in config/services.yaml or using the setFallbackLocale() method from the TranslatableTrait on the entity) is used.

How to add a new translation programmatically?

You can programmatically add a translation to any of the translatable resources in Sylius. Let’s see how to do it in the example of a ProductTranslation.

// Find a product to add a translation to it

/** @var ProductInterface $product */
$product = $this->container->get('sylius.repository.product')->findOneBy(['code' => 'radiohead-mug-code']);

// Create a new translation of the product, give it a translated name, and slug in the chosen locale

/** @var ProductTranslation $translation */
$translation = new ProductTranslation();

$translation->setLocale('pl_PL');
$translation->setName('Kubek Radiohead');
$translation->setSlug('kubek-radiohead');

// Add the translation to your product
$product->addTranslation($translation);

// Remember to save the product after adding the translation
$this->container->get('sylius.manager.product')->flush();

Learn more

PreviousState MachineNextE-Mails

Last updated 8 months ago

Was this helpful?

Locales - concept documentation