Your question is Translate Variables With Pluralization and Nesting. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Figma plugin interfaces often need localized strings with dynamic values. Implement a translator that renders a template using a nested values object, supporting simple variables, dotted paths, and pluralization blocks.
Use these forms:
{user.name}{count, plural, one{...} other{...}}For a plural block, render the one branch only when the resolved count equals 1; render other for every other count. Variable values are scalar strings or numbers, and nested objects are accessed with dot-separated paths. Templates are guaranteed to be syntactically valid and contain both one and other branches for every plural block.
Implement translate(template, values), where template is a string and values is a nested dictionary. Return the fully rendered string. A missing path is not included in valid input.
Example 1
Input: template = "I like {pet.name}.", values = {"pet": {"name": "dogs"}}
Output: "I like dogs."
The dotted path resolves values["pet"]["name"].
Example 2
Input: template = "{count, plural, one{{count} dog} other{{count} dogs}}", values = {"count": 2}
Output: "2 dogs"
The count is not 1, so the other branch is selected.
def translate(template, values):