Dynamic Content in Translations: Placeholders Done Right
Static translations are easy:
Welcome to our app
But what about:
Welcome back, John! You have 5 new messages.
That "John" and "5" need to be dynamic. Here's how to do it right.
Laravel Placeholders
Laravel supports two placeholder syntaxes:
Curly braces:
__('messages.greeting', ['name' => $user->name])
// Translation: "Hello, {name}!"
Colon prefix:
__('messages.count', ['count' => $messages])
// Translation: "You have :count new messages"
Both work. Pick one and be consistent.
The Problem with Hardcoding
Don't do this:
"Hello, " . $user->name . "!"
Why? Because in German, the name might go at the end. In Japanese, it might need honorifics. Hardcoding locks you into English word order.
Placeholders with AI Translation
Here's the magic: LangSyncer's AI preserves placeholders automatically.
Source (English):
Hello, {name}! You have {count} new messages.
AI translates to Spanish:
¡Hola, {name}! Tienes {count} mensajes nuevos.
Notice:
{name}stays as{name}(not{nombre}){count}stays as{count}(not{cantidad})- Word order changed naturally for Spanish
Complex Examples
Pluralization:
{count, plural, =0 {No messages} =1 {One message} other {# messages}}
Dates:
Last updated on {date}
Pass formatted dates from your backend for proper localization.
Multiple placeholders:
{user} invited {invitee} to {project}
Best Practices
- Use descriptive placeholder names:
{userName}not{u} - Keep placeholders language-neutral: They're code, not content
- Document what each placeholder contains: Future translators will thank you
- Test with long values: "Dr. Alexandra von Rothschild III" might break your UI
The Code Scanner Knows
When you run php artisan translator:scan --ai, the AI detects dynamic content:
Found: "Welcome, John!"
Suggests:
- Key:
greeting - Value:
Welcome, {name}! - Note: Replace
Johnwith$namevariable
Smart enough to know "John" is a placeholder, not literal text.