Translations

Contents
Trans strings
Set the locale
Translation files
Plural forms

Trans strings

The translator turns a message id such as hello into the text of the current language. Call trans with the id wherever user facing output is produced.

Keep ids short and readable so templates and controllers share the same vocabulary. Add the default language text first, then translate it step by step.

namespace App\Controller; use Symfony\Contracts\Translation\TranslatorInterface; class HomeController { public function index(TranslatorInterface $translator): string { return $translator->trans("hello"); } }

Set the locale

The locale selects which language the translator uses for the current request. Common sources are the URL prefix, the user profile, or an explicit switch link.

Set the locale on the request early so all later translator calls pick it up. Store the choice in the session or profile to keep it stable across pages.

namespace App\Controller; use Symfony\Component\HttpFoundation\Request; class LocaleController { public function change(Request $request): void { $request->setLocale("fr"); } }

Translation files

Translation files map message ids to texts per language inside the translations folder. One file per language keeps reviews simple and avoids merge conflicts.

Name files with the domain and locale pattern so Symfony loads them automatically. Rebuild the cache after edits when the new text does not appear.

translations/messages.en.yaml translations/messages.fr.yaml

Plural forms

Plural messages change with the count, for example one item versus many items. Pass the count as a named parameter so the translator can select the correct form.

Use the count placeholder in code and define singular and plural variants in the translation file. Test with zero, one, and several values because rules differ by language.

namespace App\Controller; use Symfony\Contracts\Translation\TranslatorInterface; class CartController { public function count(TranslatorInterface $translator, int $total): string { return $translator->trans("items.count", ["%count%" => $total]); } }

Next: Dotenv and env

Article author: Arthur Isaev

Related articles
Development with PHP
What is Symfony and where it runs
Install Symfony
Your first page
Routes
Controllers
Twig basics
Twig advanced
Doctrine setup
Entities
Migrations
Repositories
Relations
Forms
Validation
Services and DI
JSON controllers
Serializer
Security intro
Users
Login form
Voters
Console commands
Events
Messenger
Mailer
Cache
Sessions
CSRF
Translations
Dotenv and env
Profiler
Testing
Fixtures
Pagination
File uploads
API Platform in one lesson
DQL
Transactions
Migrations advanced
Security advanced
HTTP client
Scheduler
Webhooks
Asset Mapper
Deployment
Performance
Capstone: blog
Capstone: JSON API
Modern Symfony tour
Hero roadmap: the whole course on one page
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: