Message-ID: <570170805.378834.1711676159819.JavaMail.root@confluence-doc2-production> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_378833_906858360.1711676159812" ------=_Part_378833_906858360.1711676159812 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
The module's text strings are written in English, but you might want Fre= nch, Spanish or Polish shop owners to use your module too. You therefore ha= ve to translate those strings into those languages, both the front office a= nd the back office strings. Ideally, you should translate your module in al= l the languages that are installed on your shop. This could be a tedious ta= sk, but Smarty and PrestaShop's own translation tool make it far easier.
In short, PrestaShop implements its own translation mechanism, through t=
he use of the l
(lowercase L) method, used to encapsulate the =
strings to be translated.. This method is applied in a different way depend=
ing of the file type.
Strings in PHP files will need to be displayed through =
the $this->l('My string.')
method call, which comes from th=
e Module
abstract class, and thus is available in all modules.=
... $this->displayName =3D $this->l('My module'); $this->description =3D $this->l('Description of my module.'); ...=20
There are specific context where $this->l()
will not wor=
k: when your module has a front-end controller, that controller's strings m=
ust be put in a $this->module->l('My string', =
'filename')
method call.
For instance, in the /bankwire/controllers/front/validation.php file:
die($this->module->l('This payment method is not available.'=
, 'validation'));
This is a very specific case, and you should not often have to use it. K= eep to $this->l(), unless your code breaks because of it when in a Front= Controller context.
Strings in TPL files will need to be turned into dynami=
c content using the {l s=3D'My string' mod=3D'modulename'}
function call, which Smarty will replace by the translation for the chose=
n language. In our sample module, the mymodule.tpl
file...
<li&g= t; <a href=3D"{$base_dir}modules/mymodule/mymodule_page.php" title=3D"Cli= ck this link">Click me!</a> </li> <!-- Block mymodule --> <div id=3D"mymodule_block_left" class=3D"block"> <h4>{l s=3D'Welcome!' mod=3D'mymodule'}</h4> <div class=3D"block_content"> <p>Hello,=20 {if isset($my_module_name) && $my_module_name} {$my_module_name} {else} World {/if} ! =20 </p> =20 <ul> <li><a href=3D"{$my_module_link}" title=3D"Click this link"&= gt;Click me!</a></li> </ul> </div> </div> <!-- /Block mymodule -->=09=20
...becomes:
<li&g= t; <a href=3D"{$base_dir}modules/mymodule/mymodule_page.php" title=3D"{l = s=3D'Click this link' mod=3D'mymodule'}">{l s=3D'Click me!' mod=3D'mymod= ule'}</a> </li> <!-- Block mymodule --> <div id=3D"mymodule_block_left" class=3D"block"> <h4>{l s=3D'Welcome!' mod=3D'mymodule'}</h4> <div class=3D"block_content"> <p> {if !isset($my_module_name) || !$my_module_name} {capture name=3D'my_module_tempvar'}{l s=3D'World' mod=3D'mymodule'= }{/capture} {assign var=3D'my_module_name' value=3D$smarty.capture.my_module_te= mpvar} {/if} {l s=3D'Hello %1$s!' sprintf=3D$my_module_name mod=3D'mymodule'} = =20 </p> =20 <ul> <li><a href=3D"{$my_module_link}" title=3D"{l s=3D'Click th= is link' mod=3D'mymodule'}">{l s=3D'Click me!' mod=3D'mymodule'}</a&g= t;</li> </ul> </div> </div> <!-- /Block mymodule -->=20
...and the display.tpl
file:
Welcome = to this page!=20
...becomes:
{l s=3D'= Welcome to this page!' mod=3D'mymodule'}=20
Translating complex code
As we can see, the basis of template file translation is to enclose them=
in the {l s=3D'The string' mod=3D'name_of_the_module'}
. The c=
hanges in display.tpl
and in mymodule.tpl
's link =
and title texts are thus easy to understand. But added a trickier block of =
code for the "Hello World!" string: an if
/else
/
Here is the original code:
Hello,= =20 {if isset($my_module_name) && $my_module_name} {$my_module_name} {else} World {/if} !=20
As you can see, we need to get the "Hello World" string translatable, bu=
t also to cater for the fact that there is a variable. As explained in the =
"Translations in PrestaShop 1.5" chapter, variables are to be marked using =
sprintf()
markers, such as %s or %1$s.
Making "Hello %s!" translatable words in easy: we just need to use this = code:
{l s=3D'= Hello %s!' sprintf=3D$my_module_name mod=3D'mymodule'}=20
But in our case, we also need to make sure that the %s is replaced by "W=
orld" in case the "my_module_name" value does not exist... and we must make=
"World" translatable too. This can be achieved by using Smarty {capt=
ure}
function, which collects the output of the template between the=
tags into a variable instead of displaying, so that we can use it later on=
. We are going to use it in order to replace the variable with the translat=
ed "World" if the variable is empty or absent, using a temporary variable. =
Here is the final code:
{if !iss= et($my_module_name) || !$my_module_name} {capture name=3D'my_module_tempvar'}{l s=3D'World' mod=3D'mymodule'}{/cap= ture} {assign var=3D'my_module_name' value=3D$smarty.capture.my_module_tempvar} {/if} {l s=3D'Hello %s!' sprintf=3D$my_module_name mod=3D'mymodule'}=20
Notice that we always use the mod
parameter. This is used b=
y PrestaShop to assert which module the string belongs to. The translation =
tool needs it in order to match the string to translate with its translatio=
n. This parameter is mandatory for module translation.
Strings are delimited with single quotes. If a string contains single qu=
otes, they should be escaped using a backslash (\
).
This way, strings can be directly translated inside PrestaShop:
The page that loads displays all the strings for all the currently-insta=
lled modules. Modules that have all their strings already translated have t=
heir fieldset closed, whereas if at least one string is missing in a module=
's translation, its fieldset is expanded.
In order to translate your mo=
dule's strings (the ones that were "marked" using the l()
meth=
od), simply find your module in the list (use the browser's in-page search)=
, and fill the empty fields.
Once all strings for your module are correctly translated, click on eith= er the "Save and stay" button or the "Save" button at the bottom of your li= st of strings.
PrestaShop then saves the translations in a new file, named using the /mymodule/fr.php
<?php global $_MODULE; $_MODULE =3D array(); $_MODULE['<{mymodule}prestashop>mymodule_2ddddc2a736e4128ce1cdfd22b04= 1e7f'] =3D 'Mon module'; $_MODULE['<{mymodule}prestashop>mymodule_d6968577f69f08c93c209bd8b6b3= d4d5'] =3D 'Description du module.'; $_MODULE['<{mymodule}prestashop>mymodule_533937acf0e84c92e787614bbb16= a7a0'] =3D '=C3=8Ates-vous certain de vouloir d=C3=A9sinstaller ce module ?= Vous perdrez tous vos r=C3=A9glages !'; $_MODULE['<{mymodule}prestashop>mymodule_0f40e8817b005044250943f57a21= c5e7'] =3D 'Aucun nom fourni'; $_MODULE['<{mymodule}prestashop>mymodule_fe5d926454b6a8144efce13a44d0= 19ba'] =3D 'Valeur de configuration non valide.'; $_MODULE['<{mymodule}prestashop>mymodule_c888438d14855d7d96a2724ee9c3= 06bd'] =3D 'R=C3=A9glages mis =C3=A0 jour'; $_MODULE['<{mymodule}prestashop>mymodule_f4f70727dc34561dfde1a3c529b6= 205c'] =3D 'R=C3=A9glages'; $_MODULE['<{mymodule}prestashop>mymodule_2f6e771db304264c8104cb7534bb= 80cd'] =3D 'Valeur de configuration'; $_MODULE['<{mymodule}prestashop>mymodule_c9cc8cce247e49bae79f15173ce9= 7354'] =3D 'Enregistrer'; $_MODULE['<{mymodule}prestashop>mymodule_630f6dc397fe74e52d5189e2c80f= 282b'] =3D 'Retour =C3=A0 la liste'; $_MODULE['<{mymodule}prestashop>display_86e88cbccafa83831b4c6685501c6= e58'] =3D 'Bienvenue sur cette page !'; $_MODULE['<{mymodule}prestashop>mymodule_9a843f20677a52ca79af90312314= 7af0'] =3D 'Bienvenue !'; $_MODULE['<{mymodule}prestashop>mymodule_f5a7924e621e84c9280a9a27e1bc= b7f6'] =3D 'Monde'; $_MODULE['<{mymodule}prestashop>mymodule_3af204e311ba60e6556822eac143= 7208'] =3D 'Bonjour %s !'; $_MODULE['<{mymodule}prestashop>mymodule_c66b10fbf9cb6526d0f7d7a602a0= 9b75'] =3D 'Cliquez sur ce lien'; $_MODULE['<{mymodule}prestashop>mymodule_f42c5e677c97b2167e7e6b1e0028= ec6d'] =3D 'Cliquez-moi !';=20
This file must not be edited manually! It can only be e= dited through the PrestaShop translation tool.
Now that we have a French translation, we can click on the French flag i= n the front office, and get the expected result: the module's strings are n= ow in French.
They are also translated in French when the back office is in French.