Repository navigation
Template Syntax
This page describes syntax used in template strings, which can be seen in translation files.
The default template parser supports placeholder substitution and inline translations with a flexible
syntax inspired by printf-style formatting and custom templates.
Any text outside of % sequences is treated as plain text.
Hello, world! β "Hello, world!"
-
Syntax:
%s - Description: Represents the next sequential argument. The parser automatically assigns it the next index.
"Hello, %s!" β first argument replaces %s
-
Syntax:
%nwherenis a 1-based argument index. - Description: Explicitly references an argument at the given index.
"Hello, %1!" β replaces with the first argument
-
Syntax:
%{...} -
Description: Enclosed in
%{and}, these placeholders can include optional index, sequential marker, conversion, and formatting specifications.
| Component | Description |
|---|---|
index |
Optional, a number starting from 1, explicitly selects an argument |
's' |
Optional, indicates the next sequential argument (equivalent to %s) |
!conversion |
Optional, a single character representing a conversion |
:formatSpec |
Optional, defines formatting rules (via FormatPattern) |
"%{1!r}" β argument 1 with conversion 'r'
"%{s!r}" β next sequential argument with conversion 'r'
"%{:0.2f}" β next sequential argument with format specifier 0.2f
"%{s}" β next sequential argument, default format
"%{}" β equivalent to "%s", next sequential argument
"%{:}" β equivalent to "%s", next sequential argument
Notes:
- Explicit indexes (
1,2, β¦) override sequential indexing. -
's'is used to explicitly indicate sequential arguments inside%{...}. - If no index or
's'is specified, the argument will use the next sequential index. -
%{},%{s}, and%{:}are all functionally equivalent to%s.
-
Syntax:
%{t:key[:depth][:lang]} - Description: Represents a translation entry with optional recursion depth and language override.
To perform inlining, TemplateFormatter gets text by specified key in
specified language (or current one) and with same output type.
It directly usesparse(RenderType<T> ft, @Nullable Context ctx, String lang, String key, List<Object> args)method
to get object to insert.
Render type of inlined text will be same as originally requested.
When remaining depth is 0 or less, <key> is outputted, where key is key of translation which meant to be inlined.
| Component | Description |
|---|---|
key |
Translation key (required) |
depth |
Optional, maximum depth of recursion (default = 1). Applied only if not inside recursion already |
lang |
Optional, language override |
"%{t:greeting}" β translate key "greeting"
"%{t:greeting:2}" β translate key "greeting" with depth = 2
"%{t:greeting::fr}" β translate key "greeting" in French
"%{t:greeting:3:es}" β translate key "greeting", depth 3, Spanish
-
Syntax:
%% - Description: Represents a literal percent sign.
"Progress: 50%%" β "Progress: 50%"
Using advanced argument syntax, formatting rules can be changed for that argument.
Format syntax is almost same as
Python 3.11 Format Specification Mini-Language
so you can use it for syntax reference.
Differences are:
-
zflag are not supported - each argument converter can handle this format with its own rules so illegal combinations isn't checked when parsing.
By default, there are 3 conversion modes.
-
Numbers are converted to string using basic.toString()method - Other objects are converted to
Stringusing basic.toString()method and then quoted.
When used, string values are quoted using Python-like rules:
- Single quotes (
') are preferred when possible - If the string already contains single quotes, double quotes (
") are used instead - If both quote types are present, the less frequent one is chosen
- The chosen quote character and backslashes (
\) are escaped
This ensures the shortest and most readable quoted representation while remaining unambiguous.
Example:
hello β 'hello'
he'llo β "he'llo"
he"l'lo β 'he"l\'lo'
Outputs hash code of argument in hexadecimal format.
If argument is null, null is outputted.
If conversion is H, resulting string is uppercased.
This matches %h/%H behaviour from
String.format
Examples:
!h with "zz" -> "f40"
!H with "zz" -> "F40"
!H with null -> "null"
!H with null -> "NULL"
If argument is not a string, it is converted using String.valueOf method. No quoting performed.