Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form Widget Translations #8

Closed
nurikabe opened this issue Nov 22, 2011 · 27 comments
Closed

Form Widget Translations #8

nurikabe opened this issue Nov 22, 2011 · 27 comments

Comments

@nurikabe
Copy link

Form widget labels from FOSUserBundle, eg:

{{ form_widget(form) }}

no longer translate when activating MopaBootstrapBundle:Form:fields.html.twig for the whole project. MopaBootstrapBundle:Form:fields.html.twig doesn't seem to know how to translate them.

@phiamo
Copy link
Owner

phiamo commented Nov 22, 2011

humm, i am using FOSUserBundle with Bootstrap bundle too.
for me this works ...
do you include the theme in the template or globally via config.yml ?

i have seen differences maybe its from that.

@nurikabe
Copy link
Author

Just in the template, for example at the top of edit_content.html.twig:

{% form_theme form theme %}

<form action="{{ path('fos_user_profile_edit') }}" {{ form_enctype(form) }} method="POST" class="fos_user_profile_edit">
    {{ form_widget(form) }}
    <div>
        <input type="submit" value="{{ 'profile.edit.submit'|trans({}, 'FOSUserBundle') }}" />
    </div>
</form>

@phiamo
Copy link
Owner

phiamo commented Nov 22, 2011

hmm first of al can you check if translation is working at all?

framework:
...
    translator:      { fallback: de }
...
    session:
        default_locale: %locale%
        auto_start:     true

i have a simple test setup here, i will try to extens it with fosuserbundle tonight and check, atm i cant see any reason for that in BootstrapBundle ...

@nurikabe
Copy link
Author

Yes, working. It's only a problem with {{ form_widget(form) }}. If I call form labels explicitly, for example {{ form_label(form.current) }} in profile, the translation works as expected.

The problem goes away altogether if I disable the twig configuration, eg:

twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%
    #form:
    #    resources:    [MopaBootstrapBundle:Form:fields.html.twig]

@phiamo
Copy link
Owner

phiamo commented Nov 22, 2011

humpf ... i will check that ... no idea right now where that comes from

@phiamo
Copy link
Owner

phiamo commented Nov 22, 2011

humm this is really strang ...
i just set up the FosUserBundle into mine, imported the register route
copied the whole templates dir from views/Registration
to app/FosUserBundle/views/Registration/
and activated the form extension like you did in config.yml and it works ...

template looks like that:
register_content.html.twig

{% form_theme form theme %}

Mine
<form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register">
    {{ form_widget(form) }}
    <div>
        <input type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}" />
    </div>
</form>

and i get the output as expected:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
    </head>
    <body>
        <div>
                            <a href="/app_dev.php/login">Login</a>
                    </div>


        <div>

Mine
<form action="/app_dev.php/register/"  method="POST" class="fos_user_registration_register">
                <div id="fos_user_registration_form">
        <fieldset>
                    <input type="hidden" id="fos_user_registration_form__token" name="fos_user_registration_form[_token]" value="4237147b6b29a72cb1feabc72ae53678a3dc024f" /><div class="clearfix "><label for="fos_user_registration_form_username" class=" required">
    Username
    *
            </label><div class="input"><input type="text" id="fos_user_registration_form_username" name="fos_user_registration_form[username]" required="required" maxlength="255" pattern=".{2,255}" value="" /><span class="help-block"></span></div></div><div class="clearfix "><label for="fos_user_registration_form_email" class=" required">
    Email
    *
            </label><div class="input"><input type="email" id="fos_user_registration_form_email" name="fos_user_registration_form[email]" required="required" value="" /><span class="help-block"></span></div></div><div class="clearfix "><label for="fos_user_registration_form_plainPassword_first" class=" required">
    First
    *
            </label><div class="input"><input type="password" id="fos_user_registration_form_plainPassword_first" name="fos_user_registration_form[plainPassword][first]" required="required" value="" /><span class="help-block"></span></div></div><div class="clearfix "><label for="fos_user_registration_form_plainPassword_second" class=" required">
    Second
    *
            </label><div class="input"><input type="password" id="fos_user_registration_form_plainPassword_second" name="fos_user_registration_form[plainPassword][second]" required="required" value="" /><span class="help-block"></span></div></div>    

            </fieldset>
    </div>

    <div>
        <input type="submit" value="Register" />
    </div>
</form>

</body>
</html>

@phiamo
Copy link
Owner

phiamo commented Nov 22, 2011

could you please check you have up -to date symfony and fosuserbundle

@phiamo
Copy link
Owner

phiamo commented Nov 22, 2011

btw is your bundle defined as child of fosuserbundle ? or how do you overwrite the templates? also in app/Resources?
seems to me like an namespace prob of the translator, which i cannot yet reproduce :(

@nurikabe
Copy link
Author

I'm pretty sure both are up-to-date.

What happens when you try to override the translation? For example, add this to app/Resources/translations/FOSUserBundle.en.xliff:

<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file source-language="en" datatype="plaintext" original="file.ext">
        <body>
            <trans-unit id="1">
                <source>fos_user_registration_form_username</source>
                <target>This Is A Translated Username Label</target>
            </trans-unit>
        </body>
    </file>
</xliff>

where translator fallback is "en".

@nurikabe
Copy link
Author

Can you elaborate on what you mean by "child of fosuserbundle"? The User entity in my UserBundle does extend FOS\UserBundle\Entity\User.

@phiamo
Copy link
Owner

phiamo commented Nov 22, 2011

okokok :)
i think i found out whats happening ;)

first of all to the thinks above :
If i use your xliff nothing happens :( ..... but i know now why ...
And i wanted to know if you are using the getParent feature described in fosuserbundles readme or not but i think that doesnt matter anyways

Now to the problem source ...
we are using both the fragment:
{% form_theme form theme %}
in our templates ... this referes to : FOSUserBundle::form.html.twig
which adds the translation namespace into the form framework:

{{ id|trans([], 'FOSUserBundle') }}

(if you want to have a look into vendors/bundles/FOS/UserBundle/Resources/views/form.html.twig ...

but since we are using the config option in config.yml to get BootstrapForm Theme this one here wont get used, so the nice translation namespace isnt used so the labels arent translated :(

And now some solution, i am not sure right now what would be the best, but i assume some Attribute on the form, where it sould take the namespace from, would do it, or perhabs something like
{{ form_widget(form, {'trans_namespace': 'FOSUserBundle' }) }}

to get the namespace into our form rendering template

if you have any ideas they are very welcome, i will now try getting a fast solution up. probably not the best but at least to solve the prob for now.

@phiamo
Copy link
Owner

phiamo commented Nov 23, 2011

OK i found our field_label_attr_aware_label using generic label instead of field label ... so now our attr merging also can be extended the way fosuserbundle does it and it should be solved ... please pull and reopen this ticket if it doent work for you

@phiamo phiamo closed this as completed Nov 23, 2011
@nurikabe
Copy link
Author

Yep, that fixes it. Thanks a ton!

@phiamo
Copy link
Owner

phiamo commented Nov 23, 2011

np you're welcome!

@alch3m1st
Copy link

I have the exact same issue and I can't understand how to solve it. Can you elaborate a little on the solution ?

@phiamo
Copy link
Owner

phiamo commented May 14, 2012

@alch3m1st what exatly is the prob atm ? what solved it?
there have been different issues in that thread :/

@alch3m1st
Copy link

The labels used in the registration form by the FOSUserBundle (as nurikabe describes) are not translated (according to FOSUserBundle.en.yml,the config.yml translation fallback is EN).

My bundle extends FOSUserBundle using getParent and my app uses MopaBootstrapBundle:Form:fields.html.twig form theme from MopaBootstrapBundle.

@phiamo
Copy link
Owner

phiamo commented May 15, 2012

which form exactly ?

@alch3m1st
Copy link

My bundle extends this form

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Registration/register_content.html.twig

I remove this part

{% form_theme form theme %}

to extend the form using the default MopaBootstrapBundle:Form:fields.html.twig

and now I get the desired theme but loose the traslation.

This snippet here

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/form.html.twig

must be responsible for the translation of the labels on the FOSUserBundle...but I am not sure how to fix it using MopaBootstrapBundle form theme

@phiamo
Copy link
Owner

phiamo commented May 15, 2012

humm i use bundle extension (overriding getParent) and that works perfect ....
probably if you are not using that, you need to create your own translation file ?
can you explain a bit more in detail, how you are connecting the fos_user_conten block the layout and the register_content.html.twig together?

@alch3m1st
Copy link

I have followed the docs and extended my bundle using FOSUserBundle (translation files, controllers, templates), because I needed some customization.

I have copied this template to my bundle in order to extend it :

{% form_theme form theme %}

<div class="container">
    <div class="row" style="padding-top:25px">
        <form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register form-horizontal">
            <div class="span7 offset3">
                {{ form_widget(form) }}
                <div>
                    <button type="submit" class="btn btn-primary">{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}</button>
                </div>
            </div>
        </form>
    </div>
</div>

This is what i get :
http://tinypic.com/view.php?pic=2e308ra&s=6
i)translation works
ii)labels are not displayed properly (the miss the control-label class attribute)

I remove the form theme in order to use the mopabootstrapbundle (i use the config.yml directive)

{# form_theme form theme #}

<div class="container">
    <div class="row" style="padding-top:25px">
        <form action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register form-horizontal">
            <div class="span7 offset3">
                {{ form_widget(form) }}
                <div>
                    <button type="submit" class="btn btn-primary">{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}</button>
                </div>
            </div>
        </form>
    </div>
</div>

This is what I get:
http://tinypic.com/view.php?pic=ddlz4z&s=6
i)proper rendering
ii)no translation

But I need a proper rendering + translation.

@phiamo
Copy link
Owner

phiamo commented May 15, 2012

ot sure if i got you completely right, i have the same result as in pic two, but the translation is english and you need another one, can you explain me how i can most easily switch to another one? and do i need your translation files for that?
btw translation with domains does require the files in the same translation domain, afaik so if you create them in your translation domain that might not work.

but i thats just what i remember, probably we can find that out together...

@phiamo phiamo reopened this May 15, 2012
@alch3m1st
Copy link

I just need to define my labels.
FOSUB happends to use translation files (which is nice)

If I ignore the translation files I could:

  1. Hardcopy the labels inside the RegistrationFormType

e.g.

$builder->add('username', 'text', array('label' =>'Choose you desired username' , 'error_delay'=>true ))
  1. Hardcopy the labels in the twig template

e.g

{{ form_label(form.username, 'Choose you desired username')) }}

If i wanted to use the translation files, I could:
3) Hardcopy the label translation keys in the twig template

{{ form_label(form.username, "fos_user_registration_form_username"|trans({}, 'FOSUserBundle')) }}
  1. Get the translator service inside the RegistrationFormType and use translation keys

something like this...

$builder->add('username', 'text', array('label' =>$translator->trans(fos_user_registration_form_username", array(), 'FOSUserBundle') , 'error_delay'=>true ))

All the abode techniques work (except from 4 which I have not tried yet)

Is there any other _easier_ way to use the translation files (And I mean the ones that I extended from FOSUB)?
There are also translation files for verification messages...so I dont think that the abode techniques are viable solutions.
I must be missing sth.

@alch3m1st
Copy link

I believe I resolved it (maybe the way you suggested before this issue was closed first time):

Inside my bundle's layout.html.twig (which extends FOSUserBundle and uses MopaBootstrapBundle form theme) I extended the label field :

{% if form is defined%}
    {% form_theme form _self %}
    {% block form_label %}
    {% spaceless %}
        {% if label_render %}
                {% if single_control %}
                    {% set label_attr = label_attr|merge({'for': id}) %}
                {% endif %}
            {% if required %}
                {% set label_attr = label_attr|merge({'class': label_attr.class|default('') ~ ' required'}) %}
            {% endif %}
            {% set label_attr = label_attr|merge({'class': label_attr.class|default('') ~ ' control-label'}) %}
            <label{% for attrname,attrvalue in label_attr %} {{attrname}}="{{attrvalue}}"{% endfor %}>
            {{ id|trans({}, 'FOSUserBundle') }}
            {% if required %}*{% endif %}
            {% if widget_add_btn %}
                {{ block('form_widget_add_btn') }}
            {% endif %}
            {% if help_label %}
                <p class="help-block">{{ help_label|trans({}, translation_domain) }}</p>
            {% endif %}
            </label>
        {% endif %}
    {% endspaceless %}
    {% endblock form_label %}
{% endif %}

Now I have both the translation and the proper form theme.

I did this because FOSUserBundle extends the default SF2 form theme and changes label to id (in order to user is as a translation key).

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/form.html.twig

@phiamo I don't know why this worked of you out of the box. Thank you for you time and MopaBootstrapBundle.

@phiamo
Copy link
Owner

phiamo commented May 21, 2012

hmm this isnt too nice ... i will have that in mind, i need it soon to ... maybe i can come up with a proper solution, but for the moment i believe your prob is solved

@phiamo phiamo closed this as completed May 21, 2012
@yosmanyga
Copy link
Contributor

Also you can try using both themes to render form:

  1. Override FOSUserBundle::form.html.twig (add class="control-label")
...
<label class="control-label" ...>
...
  1. Use both themes
{% form_theme form 'MopaBootstrapBundle:Form:fields.html.twig' 'FOSUserBundle::form.html.twig' %}

@titus13
Copy link

titus13 commented Aug 4, 2012

Hello

I'm traying to resolve this translation issue but no way !

I'm using version 2.0.x and it seems to me that solution has been applyed in the master branch only !? isn't it ?

In case I am right, could you please apply on the 2.0.x branch !? Or explain to me how I could apply myself ?

In case I am wrong, could you re-explain to me how you solved this issue ?

Thank u very much by advance ! :))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants