Skip to main content

Variable Slots

Variable slots let you translate phrases containing dynamic values — numbers, dates, names — without losing the variable parts.

How it works

Loco detects inline HTML elements inside a phrase and replaces them with typed slot tokens before storing or sending to AI.

HTML elementDefault slot typeExample token
<var>number or decimal (by value){{number:0}}
<time>date (via datetime attr){{date:0}}
<strong>, <b>text{{text:0}}
<span>, <em>text{{text:0}}

The index (:0, :1, …) is sequential within the phrase.

Example

<p>Blood pressure: <var>120</var>/<var>80</var> for <strong>John</strong></p>

Extracted key:

Blood pressure: {{number:0}}/{{number:1}} for {{text:2}}

After translation (French):

Pression artérielle: {{number:0}}/{{number:1}} pour {{text:2}}

At runtime the slot values (120, 80, John) are rehydrated back into the translated string.

Slot type detection

TypeValue ruleSpecificity score
decimalContains exactly one . with digits either side+3
numberAll digits+3
date<time datetime="..."> attribute present+2
textAnything else+1

Higher specificity wins when a concrete phrase matches multiple stored patterns.

Pattern matching

When a concrete phrase is registered (e.g., "5 tasks"), the server checks stored patterns:

  1. Load all patterns containing {{type:N}} tokens
  2. Convert each to a regex ({{number:0}}\d+, {{text:0}}.+)
  3. Sort by total specificity score (descending)
  4. Test the concrete phrase against each regex
  5. If matched → remap the concrete key to the pattern; return the mapping in keyMap

The client then uses the remapped key for translation lookup.

Static slot rejection

If all slot values in a candidate match are purely alphabetic (no digits or special chars), the match is rejected. This prevents false positives:

"Add Results" matching "{{text:0}} Results"
→ slot value "Add" is all-alphabetic → rejected → keeps "Add Results" as a new key

In the translation JSON

Slot tokens must be preserved verbatim in translated values:

{ "key": "Welcome back, {{text:0}}", "context": "", "value": "Bon retour, {{text:0}}" }
{ "key": "{{number:0}} items in cart", "context": "", "value": "{{number:0}} articles dans le panier" }

AI translations preserve slot tokens automatically — Loco strips them before sending to AI and restores them in the response.