Skip to main content

Translate CRUD Flash Messages for Sylius Resources

· 3 min read
Simon Krull
Junior Software developer

In Sylius v2, when you create or update a resource in the back office, the flash message in German would look like this:

"Product wurde erfolgreich aktualisiert."

However, "Product" is not the correct wording in German.

Currently, it seems there is no way to translate the resource name itself. However, we can translate the full flash messages for the CRUD actions for the resources. In addition to our own resources, we can also translate the flash messages for the default Sylius resources.

To do this, we have to use the relevant controller action keys.

Translating Your Own Resources

Let’s say we have a custom resource for creating Lego sets:

# config/packages/sylius_resource.yaml
sylius_resource:
resources:
app.lego_set:
classes:
model: App\Entity\LegoSet\LegoSet
repository: App\Repository\LegoSetRepository

We can now define translations for the CRUD flash messages in our translation files:

# translations/flashes.en.yaml
app:
lego_set:
create: LEGO Set was created successfully.
update: LEGO Set was updated successfully.
delete: LEGO Set was deleted successfully.
bulk_delete: LEGO Sets have been deleted successfully.

Translating Default Sylius Resources

To translate flash messages for built-in resources (e.g., Product, Customer), we need the keys used in the resource configuration. You can find them in the SyliusCoreBundle.

For example:

# translations/flashes.en.yaml
sylius:
product:
create: Product was created successfully.
update: Product was updated successfully.
delete: Product was deleted successfully.
bulk_delete: Products have been deleted successfully.
customer:
create: Customer was created successfully.
update: Customer was updated successfully.
delete: Customer was deleted successfully.
bulk_delete: Customers have been deleted successfully.

Debugging the Correct Keys (Pro Tip)

If you are unsure about the correct translation key structure, you can debug this inside the RequestConfiguration class of the SyliusResourceBundle with Xdebug.

Set a breakpoint in the getFlashMessage method and run your application in debug mode. This way, you can step through the code and directly inspect the values of $message, $this->metadata, and $this->parameters to see how the final translation key is built.

Sylius Debug Language Key

If you want to learn more about Xdebug, check out the Xdebug introduction.


Example: Product Attributes

Based on the debug output, you could add translations like this:

# translations/flashes.en.yaml
sylius:
product_attribute:
create: Product Attribute was created successfully.
update: Product Attribute was updated successfully.
delete: Product Attribute was deleted successfully.
bulk_delete: Product Attributes have been deleted successfully.

Wrap-up

By defining flash message translations, you gain full control over the wording in your admin interface. This works for both custom and built-in Sylius resources, and debugging the keys makes it straightforward to extend to any resource.

If you want to learn more about customizing flash messages in Sylius, have a look at the documentation.