12

I have a hard time overwriting labels that the FOS user bundle for Symfony2 uses.

I'm already overwriting the Form class, but there is no option for elements like "setOption", only getters.

I could just remove an element and than add it again with the proper label but this seems like an overkill. Is there any nice way of overwriting options on form elements, or just translation keys, perhaps?

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Bartosz Rychlicki
  • 1,918
  • 3
  • 20
  • 41

2 Answers2

33

You don't need to overwrite Form classes.

Copy/paste vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/translations/FOSUserBundle.xx.yml files in your app/Resources/translations directory (with the same directory structure and the same file name) and redefine translations to your convenience.

edit: As told by @mario-johnathan, overriding translations is not part of bundle inheritance. See http://symfony.com/doc/current/cookbook/bundles/override.html#translations for official documentation

AlterPHP
  • 12,667
  • 5
  • 49
  • 54
  • 6
    Actually I had to copy it into app/Resources/FOSUserBundle/translation instead of my bundle dir to make it work, – Bartosz Rychlicki Nov 21 '12 at 13:34
  • 3
    Note that you have to enable translations in your framework's `config.yml` for this to work, i.e.: framework: translator: { fallback: "%locale" } – Byte Lab Feb 15 '15 at 16:32
  • 5
    And note that you need to clear the cache (php app/console cache:clear) to reload the translations if you changed it. – Iago Apr 15 '15 at 22:15
  • 8
    I'm using SF2.8 and the correct path to put the file is app/Resources/translations/FOSUserBundle.xx.yml – Mario Radomanana Feb 10 '16 at 12:29
  • 2
    @MarioJohnathan Thank you! Works! – fdrv Nov 06 '16 at 21:14
  • What would be the correct path with SF3.2? I've added two more fields to the Registration Form (name and last name), the fields saved the info correctly but I'm using a placeholder inside fields.html.twig with this code: `placeholder="{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}"` but the translation for my new two fields are empty. Any ideas? – Rodolfo Velasco Apr 06 '17 at 17:40
1

As already said you can put your translations files in :
app/Resources/translations

But if you override it in your parent bundle (src/MyAppBundle/Resources/translations) or in any other bundle, make sure to load your bundle after the overridden bundle in your Kernel :

public function registerBundles()
{
    $bundles = [
        ...
        new FOS\UserBundle\FOSUserBundle(),
        new MyAppBundle\MyAppBundle(),
        ...
    ];
...
}
Jibato
  • 494
  • 4
  • 13