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 badges1 Answer
Reset to default 1https://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
版权声明:本文标题:Translate and combine a PHP string array using Smarty - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744432477a2606022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论