I want to do translations in your project, so I need to add tags {% trans %} and {% endtrans %}. How can I in Netbeans select the text to translate and automatically (click or keyboard shortcut) to add these tags? I use Symfony with Twig. I can use also other IDE.
You need to install netbeans symfony plugin.
Btw, for translations in twig, you can use this type of syntax:
{{ 'your.translation'|trans }}
Related
I am using Twig for my project as an Template engine.
The thing i want to achieve is Translate name of the months. For instance 30 December to 30 Декабрь
Project wasn't built upon symfony or any other framework. It is just project built upon PHP7 with MVC pattern.
Maybe you can use the Twig Extensions library, which provides several useful extensions for Twig: https://twig-extensions.readthedocs.io/en/latest/.
It is developed by the same people who built the Twig template engine.
You can use the i18n Extension (https://twig-extensions.readthedocs.io/en/latest/i18n.html), then, in your code, you'll use the trans block to mark parts in the template as translatable:
{% trans "Hello World!" %}
{% trans string_var %}
{% trans %}
Hello World!
{% endtrans %}
{% set name = object.name_property %}
{% trans %}
Hello {{ name }}!
{% endtrans %}
If you just want a quick solution for translation of month names and you don't want to overload your project with other dependecies, you can create a simple Twig filter, as described here: https://twig.symfony.com/doc/2.x/advanced.html#filters.
$filter = new Twig_Filter('trans', function ($string) {
// $string is the month name...
// return the translated string
return ... ;
});
$twig = new Twig_Environment($loader);
$twig->addFilter($filter);
Then use it in your template:
{{ 'December'|trans }}
I'm working on the i18n of my website and all our templates are just plain old English. I'm look for a tool (Google didn't help) that can make adding translation tags semi-automatic.
So for example from an input template:
Hello {{ name }}
Log out
It would make:
{% trans with {'%name%': name} from 'app' %}sayhello %name%{% endtrans %}
{% trans %}logout_text{% endtrans %}
And YML:
sayhello %name%: Hello %name%
logout_text: Log out
Since someone needs to add the placeholder names for the translation, I imagine this would be some interactive program parsing Twig templates in the project.
Are you aware of anything like that?
I made my own file with the translations of the bundle fields in my own language. It's working on my FOSUserBundle I created another one for comments bundle but not in FOSUserComment. It's on Translation folder.
Exemple code:
<button data-url="{{ url("fos_comment_remove_thread_comment", {"id": comment.thread.id, "commentId": comment.id, "value": constant('FOS\\CommentBundle\\Model\\CommentInterface::STATE_DELETED')}) }}" class="fos_comment_comment_remove">
{% trans from 'FOSCommentBundle' %}fos_comment_comment_delete{% endtrans %}
</button>
Can I replace {% trans from 'FOSCommentBundle' %}fos_comment_comment_delete{% endtrans %} to work with my translation file?
The translation works fine with defined languages but i copy a file translation and implement my language cause didn't exist on the bundle.
I am not sure what you did there. Can you tell where do you store the files and what names you gave to the translation files?
you can also use {{ 'fos_comment_delete'|trans }}
Update your question so I can update my answer.
I'm using the twig trans tag in my templates, and I want to pass variables in it like so:
{% trans with {
'%link_start%': '<a href="http://www.google.nl/">',
'%link_end%': '</a>'
} %}
This %link_start%disclaimer%link_end% applies to all of our messages.
{% endtrans %}
But this gives me the following exception which points to the twig template at the {% trans with line:
PHP Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unexpected token "punctuation" of value "{" ("end of statement block" expected).' in [twig-template:line]
Even when I copy & paste the examples from the Symfony documentation I get the same exception. So I'm at a loss, what am I doing wrong here?
FYI: I'm using Twig 1.33 with the i18n extension enabled (and I'm not using the Symfony framework)
Twig doesn't support trans with out of the box. It is part of the Symfony translation extension. That explains why even the official Symfony documentation doesn't work - you are not using Symfony.
See this issue: https://github.com/twigphp/Twig-extensions/issues/74. There is a pull request to support trans with, but it hasn't been merged.
You might want to use the Symfony Translation Component in your application. You can use Symfony Components in your application, even without using the full Symfony (Framework) stack.
I haven't tried it, but you can try to use jhogervorst/Twi18n instead.
As a workaround you can use the filter tag with replace.
{% filter replace({'%foo%': 'blue', '%bar%': 'red'}) %}
{% trans %}
I like %foo% and %bar% messages.
{% endtrans %}
{% endfilter %}
In order to override a template for FOSUserBundle, I should create a twig file with the same name conserving the hierarchy. That's fine and it works perfectly.
My problem is that the twig file is not static ( in the desktop version I will render a twig file and in the mobile version we will render another one). I decide which twig to render on the controller by testing on a session variable.
Is there a solution to dynamically change the twig to render in FOSUserBundle without overriding all controllers?
You can test your session variable in twig also. Simply include another template in your twig :
{% if app.session.isMobile %}
{% include '::mobile.html.twig' %}
{% else %}
{% include '::desktop.html.twig' %}
{% endif %}