How to translate labels on a bundle - php

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.

Related

Automatically add translate tags to Twig for Symfony translate component

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?

Symfony 3 - FOSUserBundle - register.html.twig - template not found

I'm pretty new to Symfony development and i just tried to use the FOSUserBundle. I followed the guide Getting Started With FOSUserBundle on the official Symfony website.
Afterwards i wanted to test the functionality of the installation and visited the link www.linktomytestproject.dev/login which worked flawlessly. But when i tried to visit www.linktomytestproject.dev/register to test the registration functionality, i got the following errormessage:
Unable to find template "register_content.html.twig" (looked into: /home/vagrant/mytestproject/app/Resources/views, /home/vagrant/mytestproject/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form) in #FOSUser/Registration/register.html.twig at line 4.
500 Internal Server Error - Twig_Error_Loader
I checked the file register.html.twig, which contains the following code:
{% extends "#FOSUser/layout.html.twig" %}
{% block fos_user_content %}
{% include "register_content.html.twig" %}
{% endblock fos_user_content %}
When I change the third line into:
{% include "FOSUserBundle:Registration:register_content.html.twig" %}
The file is found correctly and i can use the registration functionality as intended.
But the change i made is inside the vendor folder, which obviously isn't affected by any means of version control. So i guess there has to be a place somewhere in the configuration files, where this file/foldermapping is affected.
I would really appreciate it, if someone could help me out with this question, even though this is probably a really basic problem.
First override the FOSUserBundle http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html
After that, you can create view folders like in vendor and you can use views like that;
{% include "YourBundle:Registration:register_content.html.twig" %}
It's a bug a issue is open on github
Is a bundle issue, in my project I change on the file
\vendor\friendsofsymfony\user-bundle\Resources\views\Registration\register.html.twig
this
{% include "register_content.html.twig" %}
for this
{% include "#FOSUser/Registration/register_content.html.twig" %}

Trans application with Netbeans

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 }}

Symfony2 fos login, register and forgot password in one view

I need to apply a purchased template to our dashboard. In this template, the login, register and forgot password forms are all under the same view, and switching between them using simple JQuery.
I have been looking for a nice, not-too-flashy way of combining all three forms into one, but I came up empty.
My standing options (as I see them), and why I don't like any of them:
Take the views from the fos bundle, copy them to /app/Resources/FOSUserBundle/views/, remove the {% extend %} part and {% include %} them in my own login view. Reason for dislike: to me this looks a little like a quick-n-dirty fix - "that part's not working? Let's break it off!" :)
Extend the fos bundle, accept an extra parameter in the LoginAction and RegisterAction, use {% render %} with parameters in my own login view. Reason for dislike: extending a whole bundle and modifying two different controllers just to change the way it renders feels like bad MVC.
XHR load everything. Reason for dislike: this approach makes sense when using inner pages, but for pages that reload anyway it just doesn't make sense.
TL;DR version: I'm looking for a non-hack way of including the login, register and forgot password form in one page.
Any help would be greatly appreciated!
I found a solution with which I am comfortable with for my current project. The advantages and disadvantages of the proposed solution upfront:
Advantages:
few LOC to implement
FOSUserBundle update proof (does not override the view scripts*)
Disadvantages:
performance overhead due to subrequests
only forms can be displayed, form submission (and subsequently error handling upon submission) will always go to the pages provided by FOSUserBundle
still feels like a quick-n-dirty fix, but better than other options
* only needs to override the layout.html.twig file
With that being said, here is what I have done:
Render the form in your template
Use embedded controllers to render the forms you need:
<div>
<h2>Login</h2>
{{ render(controller('FOSUserBundle:Security:login', { embeddedForm: true})) }}
</div>
<div>
<h2>Reset</h2>
{{ render(controller('FOSUserBundle:Resetting:request', { embeddedForm: true})) }}
</div>
Override FOSUserBundle layout
As I use the routes provided by the bundle, I had to override the FOSUserBundle layout template file to extend the standard layout of my application. As the overriden FOSUserBundle layout file extends the main applications layout file the layout would be repeated for each call {{ render ... }}. To prevent that, we need to dynamically disarm the extended layout file. Here is what the overriden layout file looks like:
{# app/Resources/FOSUserBundle/views/layout.html.twig #}
{% if app.request.get('embeddedForm') %}
{% set layout = 'AcmeBundle::layout-content.html.twig' %}
{% else %}
{% set layout = 'AcmeBundle::layout.html.twig' %}
{% endif %}
{% extends layout %}
{% block content %}
{% block fos_user_content %}{% endblock %}
{% endblock %}
Create the AcmeBundle::layout-content.html.twig file
This layout should only render the content block of the FOSUserBundle view scripts and is such short and simple:
{# src/Acme/DemoBundle/Resources/views/layout-content.html.twig #}
{% block content %}{% endblock %}
Now the forms will render nicely with all dependencies (CSRF and so forth). Submitting the form will however take you to the FOSUserBundle actions.
Alternative solution:
This answer describes how to manually implement the forms and link them to the FOSUserBundle controller.

Symfony2 Translation using trans_default_domain

I'd like to translate a part of my twig template which is not in my trans_default_domain
For exemple:
{% trans_default_domain 'FOSUserBundle' %}
{{ 'test.trans'|trans({}, 'ProjectMainBundle') }}
So test.trans is translated in ProjectMainBundle but I always have test.trans in my text.
Edit:
test.trans is in src/Project/MainBundle/Resources/translations/messages.en
It works everywhere but it doesn't work when I am trying to get my trans with a trans_default_domain
You are storing the translation in a file called messages.en.yml which means according to the naming conventions for translations these translations have the domain messages and not ProjectMainBundle.
Therefore the translator doesn't find a translation if you're trying to use the domain ProjectMainBundle and returns the string itself.
Each message file must be named according to the following path:
domain.locale.loader
Your translations should be stored in #AcmeYourBundle/Resources/translations/<domain>.<locale>.yml ( or php, xliff, ... ).
Remember to clear your cache after renaming.

Categories