2

I need to pass a variable with a translation string in Prestashop. In the Backend It is possible to do like

sprintf($this->l('The number is %1$d'), $number);

But I need to do this in the front-end while using SMARTY ? is anyone there to help me ?

Ijas Ameenudeen
  • 9,069
  • 3
  • 41
  • 54

3 Answers3

7

something like this :

{l s='The number is %1$d' sprintf=$number}
skiplecariboo
  • 842
  • 2
  • 9
  • 21
2

1 - On PrestShop v1.7.4 with @skiplecariboo answer {l s='The number is %1$d' sprintf=$number} I'm getting:

Catchable Fatal Error: Argument 2 passed to PrestaShopBundle\Translation\TranslatorComponent::trans() must be of the type array, string given, called in /vagrant/httpdocs/config/smartyfront.config.inc.php on line 210 and defined

2 - With official 3rd party module translation method {l s='The number is %1$d' sprintf=$number mod='my_module'}, I'm getting the same error:

Catchable Fatal Error: Argument 2 passed to PrestaShopBundle\Translation\TranslatorComponent::trans() must be of the type array, string given, called in /vagrant/httpdocs/config/smartyfront.config.inc.php on line 210 and defined

3 - And with official native module translation method {l s='The number is %1$d' sprintf=$number d='Modules.my_module'}, I'm getting:

Unable to translate "The number is %1$d" in module:my_module/my_module.tpl. sprintf() parameter should be an array.

So, solution for me was to set $number variable as an array:

{l s='The number is %1$d' sprintf=[$number] mod='my_module'}

NB: %1$d flag is for decimal, for a string, use: %1$s (source)

Klemart3D
  • 180
  • 9
1

this is how you can passe a variable in the translated string

{l s='Comment: # %id%' mod='ayalinecomments' sprintf=['%id%' => $comment.id_ayalinecomments_comment]}

prestashop will replace %id% by my variable $comment.id_ayalinecomments_comment

ty

lazy_coder
  • 61
  • 1
  • 5