Interpolation and Variable Substitution
You'll learn how to insert dynamic values like usernames, counts, or dates into translated strings while preserving grammatical correctness across languages.
The Core Problem
When translating your app, you can't just have static strings — you need to inject dynamic data like "Hello, Alice" or "You have 5 new messages." However, different languages place variables in different positions and may change their form based on the values (like singular vs. plural).
How Interpolation Works
Interpolation is the process of replacing placeholders in a translation string with actual runtime values. Think of it like Mad Libs: you have a sentence template with blanks, and you fill them in with the right words.
Most translation systems use special markers for placeholders:
{{username}}— common in many frameworks{count}— simpler syntax%sor$1— printf-style placeholders
Why Plain Concatenation Fails
Avoid building translations like "Hello " + name + "!" because:
- Word order varies: English might say "5 items," but Japanese puts the number after: "items 5"
- Gender agreement: Some languages change adjectives based on the gender of variables
- Pluralization: "1 message" vs "2 messages" varies wildly across languages
Instead, your translation files should contain complete templates:
{
"greeting": "Hello, {{name}}!",
"itemCount": "You have {{count}} items"
}
Your internationalization library then substitutes {{name}} and {{count}} at runtime while respecting each locale's grammar rules.
Key Takeaway: Use placeholder-based interpolation in translation strings rather than string concatenation to allow translators control over variable placement and enable proper grammatical agreement across languages.