Zug Zwang Zug Zwang - 1 month ago 11
PHP Question

How to correctly handle functions in the translation files? (PHP templates)

I've searched for this in the manual but I found nothing.
I have for example this view:

//view.html.php
<?php $view->extend('::base.html.php') ?>
<?php $view['slots']->start('real-content') ?>

<?php echo $view['translator']->trans('user.name') ?>

<?php $view['slots']->stop('real-content') ?>


How can I insert the function
getUsername()
in the translation file in order to be rendered correctly?
Is this the best way?

//messages.en.php
'user.name' => 'Name: %user->getUsername()%'

Answer

You should separate the translation of user.name (the "label") and the actual call to the function returning the name value:

view.html.php

<?php $view->extend('::base.html.php') ?>
<?php $view['slots']->start('real-content') ?>

<?php echo $view['translator']->trans('user.name') . $user->getName(); ?>

<?php $view['slots']->stop('real-content') ?>

messages.en.php

'user.name' => 'Name: '
Comments