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>
Note: The l
macro does not support escaping the strings for javascript. Instead you can assign the translation to a variable and escape it afterwards:
{assign var='translatedString' value={l s='Text containing single quote' d='Modules.Mymodule.Shop'}}
<script>var text='{$translatedString|escape:'javascript'}';</script>
In .twig
files, you can use the trans
filter from Twig:
<div>{{ 'This product is no longer available.'|trans({}, 'Shop.Notifications.Error') }}</div>
You can also use named placeholders:
<div>{{ 'There are %products_count% items in your cart.'|trans({'%products_count%': cart.products_count}, 'Shop.Theme.Checkout') }}</div>