Josselin Dionisi - Freelance developer

Using "locales" with Sylius for multiple languages (internationalization)

ionicons-v5-k Josselin Dionisi Apr 19, 2023
99 reads Level: intermediate

A new article on Sylius is available.

I was sure I'd already told you about Sylius, but it seems I haven't, so here goes!

Sylius is an e-commerce solution based on our beloved Symfony framework (we've already told you all about it on the blog, though?).

Imagine being able to manage an online store and configure it from start to finish, all while benefiting from all the advantages and functionalities offered by Symfony - doesn't that sound dreamy?

Well, that's not all, because Sylius is well thought-out and allows you to manage several stores in one, in different languages.

Channels

This concept is called Channels, and each channel corresponds to a third-party store. Note that this can also simply mean differentiating between different types of store, such as a physical store and its online version. Channels are not themselves necessarily linked to different languages.

When you set up your store for the first time, you need to configure the desired channels. All this is done in the back-office in a simple and intuitive way, and you can switch from one channel to another using Symfony's debug toolbar (you know that little black rectangle at the bottom of the page when you're in dev mode?).

Locales

Now, we're only going to talk about different languages here. You'll need to define all the languages that will be available on your Sylius (all stores included). Once you've done that, you can associate each "active" language with one of your channels. For example, you could have one channel in French and English, but another in German only. Any combination is possible.

Small error to avoid: If you change the main locale of the default channel, you may get the error "Channel could not be found". Check that you have entered a hostname in the store's back office (either localhost or the site's domain).

Well, that's all very well, but it's still basic configuration in an admin panel. We're developers, so when do we get into the code?

Well, to continue on the subject of channels and locales, how do you import products into Sylius from an external source and place them correctly in each channel?

The answer lies in one Symfony entity...

Channel pricing

As with any online store, you'll need to configure categories and products. What's important in the case of a multi-lingual (and multi-channel) site is to be able to add products aimed at certain audiences and with different data.

For example, a price of €10 for a product in France will be worth €11 in Spain. The same product might not be available in Germany. All this can be set via channel pricing, which is the cornerstone of your store.

Conclusion

So much for a first look at Sylius and a case where the official documentation is not necessarily very exhaustive. I'm sure I'll be writing more articles along the same lines, to help you save time and research certain situations... ?