10

I'm having trouble to translate a string in Twig. I'm building a theme for a multilingual webshop.

A user can create a USP (Unique Selling Point). Problem is that it won't translate when you have a different language.

So the usp is called like this in the template {{ theme.usp }} The outcome of that is for example "Free shipping".

To translate a string in the system you have to use a {{ 'Free shipping' | t }} filter.

Is there any way to get {{ theme.usp }} translated. I thought this would be useful but I don't know how to incorporate this. How to concatenate strings in twig

What I did was:

{% set usp = {{ theme.usp }} %}
{{ usp | t }}

Doing this gives me an error since {{ theme.usp }} has to be between ''. Doing that doesn't gives me 'Free shipping' as outcome but 'theme.usp'.

Anyone a suggestion?

Community
  • 1
  • 1
Meules
  • 1,349
  • 4
  • 24
  • 71

3 Answers3

15

Try:

{{ theme.usp|trans }}

or if it must be filtered by t then

{{ theme.usp|t  }}
pazulx
  • 2,369
  • 1
  • 17
  • 17
  • Yes i tried that. Doesn't give the desired result. I think it's more about transforming {{ theme.usp }} to an object which then can be translated with a 't' function?? – Meules Jul 23 '14 at 09:59
  • is `theme.usp` an object or a string? – pazulx Jul 23 '14 at 10:05
6

Easy way to do this is to use one of filters (which are in fact PHP functions at the end) that simply returns string as in PHP docs.

For example, I had a problem with explicitly setting a value in a choice (SELECT/OPTION) form type, as it required a string, but I got an int as a value. I simply did this:

{{ form_widget(form.scope, {'value': scope|trim}) }}

scope was int, but trim (and other PHP functions, here as Twig filters) automatically makes it a string. Yeah, it's another missing functionality in Symfony2, btw.

forsberg
  • 1,681
  • 1
  • 21
  • 27
-5

Try this

{{ usp|print_r }}

It worked for me.