Skip to content
tezvyn:
Menu

How to structure translation keys for dynamic localized strings

Source: unicode-org.github.ioMediumHow cards are made

Tests ICU MessageFormat and safe interpolation. Outline: one key per sentence with named placeholders; sanitize variables pre-format; use pattern-level plural and select. Red flag: concatenating fragments or injecting raw user input into templates.

What's really being asked

This question tests whether you understand that localization is not just swapping words but preserving grammatical structure across languages. Interviewers want to see that you know translators must receive complete sentences with placeholders rather than fragments to assemble, because word order, pluralization, and gender agreement change by locale. They also want to know you treat user-generated content as untrusted data that must be sanitized before interpolation, preventing injection attacks.

The full answer

First, store each user-facing message as a single translation key containing a complete sentence. Use named placeholders such as {username} and {count} rather than positional ones, because named arguments give translators context and allow them to rearrange clauses for their language. Second, keep all formatting logic out of the component and inside the message pattern. Use ICU MessageFormat complex arguments like plural, select, and selectordinal so the same key handles one message, many messages, or gendered forms without any branching in your code. Third, sanitize or escape dynamic values in application code before passing them into the formatter. If the output targets HTML, either escape the substituted values before they reach the DOM or use a framework that binds text content safely rather than innerHTML. Fourth, separate translation resources from code. Load keys from JSON or YAML files so translators can work on full strings in context without touching source files and without risking broken syntax.

The mistakes people make

A major red flag is concatenating translated fragments in code, such as t('welcome') plus username plus t('messages'), because this destroys the ability to localize word order and produces broken grammar in many languages. Another is using template literals or string concatenation with raw user input, which opens XSS vectors. Passing unsanitized HTML inside translation values is also dangerous. Hardcoding plural or gender branches in the component rather than the message pattern is wrong because many languages have more than two plural forms, and some have complex gender agreement rules that belong in the translation layer.

What usually comes next

Expect the interviewer to ask how you would handle languages with multiple plural categories like Russian or Arabic. They may also ask what to do when a design requires inline markup such as bold or links inside a translated sentence, or how you would handle gendered pronouns and agreement. Performance of loading large message bundles on the client is another common tangent.

A concrete example

Suppose the English source key is welcome_back: Welcome back, {username}. You have {count, plural, one {# new message} other {# new messages}}. The component calls format('welcome_back', { username: escapeHtml(user.name), count: newMessages }). The translator sees the full sentence and can move {username} to the end for their language, or adjust the plural forms to match the six plural categories required by Arabic. The application never concatenates fragments and never injects raw user input into the DOM.

Interview question

Which approach correctly structures a localized string that displays a user name and a dynamic message count across languages with different word orders and plural rules?

  • a.Use positional placeholders and handle plural branching with if/else logic in the component based on the current locale.
  • b.Split the sentence into separate keys for the greeting, user name, and message count, then concatenate them in the component.
  • c.Allow translators to provide HTML fragments and render the final string with innerHTML to preserve formatting.
  • d.Store the sentence as one key with named placeholders, keep ICU plural logic in the pattern, and sanitize variables before interpolation.Correct
Why?

A single key with named placeholders lets translators rearrange clauses and handle complex plural forms via ICU MessageFormat, while sanitizing inputs prevents injection. Option B is a red flag because concatenating fragments destroys grammatical flexibility across locales.

Just read this? Test yourself on what you have been reading.

Read the original → unicode-org.github.io

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles