One of the main needs for localization is translating wordings to the another language. PrestaShop is capable of translating wordings to any language, as long as it has the appropriate translations available.
PrestaShop 1.7 features a new translation system, based on the Symfony Translation component.
This documentation is intended for Core and Native module translation.
If you are a module developer, read the module translation documentation instead.
The translation system is less complicated than it can seem to be at first glance. To start working with it, you should get familiarized with the following concepts:
To translate wordings in PHP files, you first need an instance of the Translator
service (explained below). Then, you can use the trans()
method to translate your wording:
<?php
echo $translator->trans('This product is no longer available.', [], 'Shop.Notifications.Error');
The trans()
method takes three arguments:
$id
– The wording you want to translate. Keep in mind that it has to be exactly the same as the one in the default catalogue, or the translation won’t work.$parameters
– An array of replacements, if any. (Learn more about translation placeholders).$domain
– The [translation domain][translation-domains] for that wording.$replacements
to be optional. For more, see FrameworkBundleAdminController.Controllers include a helper method named trans()
that calls the translator internally:
<?php
// legacy controllers
$this->trans('This product is no longer available.', [], 'Shop.Notifications.Error');
// Symfony-based controllers (FrameworkBundleAdminController)
$this->trans('This product is no longer available.', 'Shop.Notifications.Error', []);
If you are outside a controller, and after careful consideration you think you absolutely need some stuff translated, then you can add it as a dependency of your class:
<?php
// SomeService.php
namespace PrestaShop\PrestaShop\Core\Foo\Bar;
use Symfony\Component\Translation\TranslatorInterface;
class SomeService
{
private $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
}
Then, inject it into your service using the Dependency Container:
# services.yml
prestashop.core.foo.bar.some_service:
class: 'PrestaShop\PrestaShop\Core\Foo\Bar\SomeService'
arguments:
- '@translator'
And finally, use the translator at will:
<?php
// SomeService.php
$this->translator->trans('This product is no longer available.', [], 'Shop.Notifications.Error');
In .tpl
files, use the l
(lower case “L”) macro:
<div>{l s='This product is no longer available.' d='Shop.Notifications.Error'}</div>
If you have have replacements to peform in your wording, then there are two options:
Anonymous placeholders (eg. %s
)
<div>{l s='List of products by supplier %s' sprintf=[$supplier.name] d='Shop.Theme.Catalog'}</div>
Named placeholders (eg. %my_placeholder%
)
<div>{l s='There are %products_count% items in your cart.' sprintf=['%products_count%' => $cart.products_count] d='Shop.Theme.Checkout'}</div>
In .twig
files, you can use the trans
filter from Twig:
<div>{{ 'Sort by'|trans({}, 'Admin.Actions') }}</div>
For information on more advanced features, head on to the Symfony translator component documentation.