Variables

Variables are used to reference data that is not known at the time the message is authored, but is passed to the message at runtime (when it is displayed).

In a message, variables are always prefixed with a $ character. The variable name can contain almost all characters that are not spaces or punctuation. A variable name can't start with a number.

Usage Jump to heading

Variables can be interpolated into a message using a {placeholder expression}.

Your name is {$name}.
{ "name": "Alice" }

Variables can be used in {function options} and {markup options}.

You have {42 :number style=currency currency=$currency}.
{ "currency": "USD" }

Declarations Jump to heading

In addition to referring to external input values, variables can be declared within a message. A declaration binds a variable to a value within the scope of a message. This variable can then be used in other expressions within the same message. Declarations are useful for breaking down complex messages into smaller parts, or for reusing values multiple times.

Local declarations Jump to heading

Variables can be declared within a message using a .local statement. The declaration is followed by the variable name, an equals sign, and an expression that evaluates to the value of the variable.

.local $count = {42}
{{The count is: {$count}}}

Expressions inside of a .local statement can contain contain literals and functions. They can also reference other variables (local and external ones). When referencing a variable, they can not reference themselves or variables that are declared syntactically after them — only variables that are declared before them.

.local $score = {0.42}
.local $score_percent = {$score :number style=percent}
{{Your score was {$score_percent}.}}

Input declarations Jump to heading

Input declarations are similar to local declarations, but they are used to redeclare external input values as local variables. This can be useful to assert a specific type or format for an input value (e.g. to ensure that a passed value is always an integer). It can also be useful to ensure that an input variable is always formatted in a specific way.

Input declarations are declared using a .input statement. The declaration is followed by an {expression}, specifically a variable expression. The variable in this expression is both the input variable being referenced, and the name of the local variable being declared.

.input {$count}
{{The count is: {$count}}}
{ "count": 32 }

In the above example, .input is just being used to ensure that the count is always passed. It acts as a form of documentation for both the developer and the translator: it tells the developer that the message expects a count variable, and it tells the translator that the message gets a count variable passed in.

Ideally expressions in .input should also specify a function like :string, :number, or :datetime. These functions are not type assertions. They act as type coercions, converting the external input value to the specified type, by passing the value through the specified function (creating a new {resolved value}). This also means that formatting options can be specified in the function.

.input {$amount :number style=currency currency=USD}
{{The price is: {$amount}.}}
{ "amount": 32 }

Shadowing and redeclaration Jump to heading

Declared variables shadow external input values with the same name.

.local $count = {42}
{{The count is: {$count}}}
{ "count": 32 }

Variables can also only be declared once within a message. Redeclaring a variable will result in an error.

.local $count = {42}
.local $count = {32}
{{The count is: {$count}}}