admin管理员组

文章数量:1379725

I'm working on come code that uses Smarty for templating. I have a php function that creates an array of strings, and this function is called from a Smarty { } block. Within that Smarty block I need to separately translate each string in the array (using |@translate) and then join/combine the elements of the array into a single comma-delimited string. I'd prefer to do this in a compact way.

So, for example, if I have a php array containing ["Hello", "beautiful", "world"] and the translation is to French then the output string should be "Bonjour,beau,monde".

I'm sure this is simple but I'm new to php/smarty and I'm struggling to find a compact way to do this. I'd appreciate some help. Thanks!

I'm working on come code that uses Smarty for templating. I have a php function that creates an array of strings, and this function is called from a Smarty { } block. Within that Smarty block I need to separately translate each string in the array (using |@translate) and then join/combine the elements of the array into a single comma-delimited string. I'd prefer to do this in a compact way.

So, for example, if I have a php array containing ["Hello", "beautiful", "world"] and the translation is to French then the output string should be "Bonjour,beau,monde".

I'm sure this is simple but I'm new to php/smarty and I'm struggling to find a compact way to do this. I'd appreciate some help. Thanks!

Share Improve this question asked Mar 19 at 20:51 Andy JohnsonAndy Johnson 8,1814 gold badges37 silver badges54 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

https://smarty-php.github.io/smarty/stable/designers/language-modifiers/

When working with arrays, the |@foo modifier syntax indicates to pass the whole array to the function callback, this is useful when performing counts eg {$myArray|@count}

In your situation you can omit the @ prefix and create a callback function that works for a single string translation, and it will be applied to each item in the array. Or keep using the @, and handle the iteration inside of your callback function, and return the whole array, or invoke the implode function here to get your string output.

The other approach is to avoid performing too much work in the smarty template engine, and perform your translations prior to assignment in your native PHP code.

本文标签: Translate and combine a PHP string array using SmartyStack Overflow