0

I'm using Phalcon with Volt for my multilanguage application.

I've setup a translation file which contains the following variables:

'numbervalidation-field' => '%field% is a number',
'Teams' => 'Teams',

And this is how I print 'Teams is a number' correctly

<?php echo $t->_("numbervalidation-field", array("field" => $t->_('Teams'))); ?>

I just want to be able to print the same using volt, I've tried several things but nothing works, this was my latest attempt:

{{t['numbervalidation-field', ['field': t['Teams']]]}}

Help please, I couldn't find how to do this.

Pablo Santamaria
  • 727
  • 1
  • 8
  • 20

2 Answers2

0

All you need to do is to assign your $t variable, which is the translating object, to $this->view->t in your view's controller.

Open your controller file, find an appropriate action and add

$this->view->t = $t;

somewhere where you can use $t to translate your strings. It's a good idea to put it in a base controller so you can use it in every view.

Then you're free to use translation with {{ t._('SOMETHING') }}. You can also add an array with fields to replace as a second parameter in your volt translating function like this:

{{ t._('CODE',[parameters]) }}

where [parameters] is an array that has replacements for string fields with values by keys.

Luke
  • 2,350
  • 6
  • 26
  • 41
  • OP is asking how to use the translator **with variables**. Your example is just a normal translation. If possible, extend your example with the use of translation variables! – Timothy Aug 31 '16 at 13:09
  • @Timothy Sure, will do soon – Luke Aug 31 '16 at 13:11
  • Thanks @Luke, looking forward to seeing it with variables – Pablo Santamaria Sep 06 '16 at 12:49
  • Hi @Luke, when we say variables, we mean one of the parameters is a translation variable, as you can see in the question I've tried {{t['numbervalidation-field', ['field': t['Teams']]]}} but it doesn't work, so how can I put a variable as parameter? Thanks, I appreciate your help. – Pablo Santamaria Oct 17 '16 at 02:20
0

You can use a filter for ease of use, assuming your Translation service is $di->t, that would read:

$volt->getCompiler()->addFilter('trans', function ($resolvedArgs, $exprArgs) {
    return sprintf('$this->t->query(%s)', $resolvedArgs);
});

Then, in the volt template, you can do:

{{ 'name'|trans }}

In your case, you'll have two translations, so it could read:

{{ 'numbervalidation-field'|trans({'field': ('Teams'|trans)}) }}
Yvan
  • 2,539
  • 26
  • 28