Course contentsShow
← Web Development
Lesson 2530 of 2,98245. InternationalizationFree lesson

Interpolation and Variable Substitution

Insert dynamic values into translated strings using placeholders while maintaining grammatical correctness.

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
  • %s or $1 — printf-style placeholders

Why Plain Concatenation Fails

Avoid building translations like "Hello " + name + "!" because:

  1. Word order varies: English might say "5 items," but Japanese puts the number after: "items 5"
  2. Gender agreement: Some languages change adjectives based on the gender of variables
  3. 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.