Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine
TRACK

Symfony 3 >

Customizing Text via Translations

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

Create a new user: aquanaut2@gmail.com, aquanaut2, turtles, turtles. Hey, a pretty flash message!

The user has been created successfully

Tip

If you don't see this, make sure you're rendering success flash messages in your base template: http://knpuniversity.com/screencast/symfony-forms/save-redirect-set-flash#rendering-the-flash-message

Well, that message lacks some pizzaz! How can we give it some personality? I mean, it's probably being set deep in some PHP file somewhere. Do we need to override that file?

Nope! Every string you see from this bundle is being passed through the translator. So to change text, you just need to translate it!

Back in PHPStorm, I'll close a few tabs, then press Shift+Shift and look for FOSUserBundle.en.yml. Whenever FOSUserBundle translates something, it translates it through a domain called FOSUserBundle... which just means that when you translate its strings, they'll live in a file called FOSUserBundle.{language}.yml.

Search for "The user has been created successfully". There it is! Under registration, flash, user_created.

Overriding the Translation

Let's breathe some life into this translation! To do that, inside app/Resources/translations, create a new file: FOSUserBundle.en.yml. Then, add the same keys: registration, flash, and user_created with the message:

Welcome! Now let's do some science!

registration:
flash:
user_created: "Welcome! Now let's do some science!"

Since this is a brand new translation file, you'll need to clear you cache for Symfony to see it:

php bin/console cache:clear

I know, you should almost never need to clear cache in the dev environment: this is one of those really rare things. When you update this translation file, you will not need to clear the cache.

Cool! Go back to /register and repeat: aquanaut3, aquanaut3@gmail.com, turtles, turtles... and yes! Let's do some science! Great idea!

Except, instead of science, let's customize some forms!

Leave a comment!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.3.*", // v3.3.18
        "doctrine/orm": "^2.5", // v2.7.0
        "doctrine/doctrine-bundle": "^1.6", // 1.10.3
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.5
        "symfony/swiftmailer-bundle": "^2.3", // v2.5.4
        "symfony/monolog-bundle": "^2.8", // v2.12.1
        "symfony/polyfill-apcu": "^1.0", // v1.3.0
        "sensio/distribution-bundle": "^5.0", // v5.0.18
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.25
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "knplabs/knp-markdown-bundle": "^1.4", // 1.5.1
        "doctrine/doctrine-migrations-bundle": "^1.1", // v1.3.2
        "composer/package-versions-deprecated": "^1.11", // 1.11.99
        "friendsofsymfony/user-bundle": "^2.0" // v2.0.0
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.1.4
        "symfony/phpunit-bridge": "^3.0", // v3.2.7
        "nelmio/alice": "^2.1", // v2.3.1
        "doctrine/doctrine-fixtures-bundle": "^2.3", // v2.4.1
        "symfony/web-server-bundle": "^3.3"
    }
}
userVoice