Timber & WPML string translation - wordpress

Hi I'm using Timber and what it used to be as simple as
_e('string', 'theme')
to translate strings with WPML, seems to not been working on Timber any idea on how to translate strings?
I have tried the following and nothing is working
{{ _e('string') }} & {{ _('string') }}
{{ _e('string', 'theme') }}
{{ function("icl_translate", 'theme', 'string_identifier', 'string) }}
{{ dump(ICL_LANGUAGE_CODE) }} // Doesn't return anything, so not an option either
Thanks!

Yes I use this;
{{ __('All items', 'theme') }}
And it's working perfectly.

I just did a quick test and was able to get this to work....
Created files with a translation for "thingy" ==> "foobar" in en_US.mo and en_US.po in wp-content/themes/mytheme/languages from there...
single.php
$lang_dir = get_stylesheet_directory().'/languages';
load_theme_textdomain('mytheme', $lang_dir);
Timber::render("single.twig");
single.twig
I like {{ __('thingy', 'mytheme') }}
Output
I like foobar
Try replicating that to see if it works. At least at that point we can isolate things to WPML versus native translate stuff

Related

Is it possible to pass twig variables into templates?

I'm using the Timber plugin for my Wordpress site. For a little button component, i'm trying to pass the variable {{ site.link }} to use as my href.
Like so:
{% include 'component/icon-button.twig' with { href: '{{ site.theme.link }}/faq' } %}
icon-button.twig
<button class="icon-button" data-href="{{ href }}">text</button>
But that only results in {{ site.link }}/faq being output as is, as a string, not the actual URL.
How would i do this?
That's just because when you use include in twig you don't need to use double {{ again. The correct syntax would be:
{% include 'component/icon-button.twig' with { href: site.theme.link ~ '/faq' } %}
The "~" is used for string concatenation in twig. You were passing the whole thing as a string and nothing more =)

How to change template of controller rendered in twig file (Symfony 3)?

I know that we can render the content of a controller in twig file like this:
{{ render(controller('FOSUserBundle:Security:login',{"baseTemplate": true})) }}
However, I don't know if we can pass the new template so that the controller will use it instead of the default. Anyone tried to override template in this way?
I don't really understand the issue here
If you do
{{ render(controller('FOSUserBundle:Security:login',{"baseTemplate": true})) }}
You could aswell do:
{{ render(controller('FOSUserBundle:Security:login',{"template": "your_template.html.twig"})) }}
Or
{{ render(controller('FOSUserBundle:Security:login',{"templateNumber": "4"})) }}
Where templateNumber is used in a condition inside your controller ?

How to apply Twig filters to form_label(form.name), e.g. 'capitalize'?

My problem concerns just applying Twig filter inside a form without getting a 500 server error. Here is the code, which is in the view 'Room/new.html.twig':
{{ form_label(form.name) }}
or
{{ form_label(form.name, 'room.name') }}
Both will output: 'nom de salle' ('room name' in French). That is because, in my translation file 'messages.fr.yml', I have:
room.name: "nom de salle"
Also, I activated the translation in my form type 'RoomType.php' with this code:
->add('name', 'text', array(
'label' => 'room.name'
))
I tried every possible combination I thought of with |capitalize and got either a 500 server error or no change at all. Some examples:
{{ form_label(form.name)|capitalize }}
{{ form_label(form.name, 'room.name'|capitalize) }}
and so many more...
Does anyone know how to do it, it doesn't seem to be in the Symfony doc. Of course, I thought about duplicating the translation to have one in lower case and one starting with a capital letter but that would defeat the object. Plus there are other Twig filters I would like to use with this form_label() function.
Help greatly appreciated, thank you.
I just found the solution. It's so easy that it is embarrassing. I thought I didn't need the |trans as it was already translated but without it, 'room.name' = 'nom de salle' would transform to 'Room.name' instead of 'Nom de salle'. So the correct code is just:
{{ form_label(form.name, 'room.name'|trans|capitalize) }}

Pass variables to Twig Translator

In messages.en.yml:
variable_name: Welcome to %site_name% - %other_info%
In Twig
{{ 'variable_name'|trans( -here- ) }}
It's the -here- part I'm struggling to know how to pass data in. I must do it this way (above is just an example).
Maybe I'm misunderstanding the question but if I understand correctly, you only need to give an array as the first parameter of the trans filter.
{{ 'variable_name'|trans({'%site_name%': 'My Website'}) }}
And of course, values can be variables if you don't put them between quotes :
{{ 'variable_name'|trans({'%site_name%': 'My Website', '%other_info%': page_name}) }}

Symfony2 : Auto htmlentities using Twig

I'm displaying some variable retrieved in my database using Twig :
<p>{{ my_variable }}</p>
The thing is this variable may contain html tags, such as "<br />".
Twig seems to automatically call some htmlentities-like function when displaying variables.
Is there any way to disable it so that when I display a variable containing "Hello<br />world !" I get :
Hello
world !
rather than :
Hello<br />world !
Thanks
Use {{ my_variable|raw }} to prevent my_variable from being automatically escaped.
See Twig documentation: http://twig.sensiolabs.org/doc/filters/raw.html
Try using this
{% autoescape false %}{{ my_variable}}{% endautoescape %}
even better: {{ '<br />|raw('html') }} to avoid unescaping other sensible stuff.
If you just want to use linebreaks in the text stored in your database but don't care to use html , you can also use the nl2br filter as in {{ var|nl2br }}. Allows you to use string linebreak character \n in your text. Filter converts it to <br/>

Resources