Skip to content

Commit 831c5e3

Browse files
committed
docs: for localization update explaining how variable interpolation works in template files (#1404)
1 parent 69d7e35 commit 831c5e3

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

packages/lit-dev-content/site/docs/v3/localization/best-practices.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,56 @@ render() {
164164
}
165165
```
166166
167+
## Document Variable Positions in Locale Files
168+
169+
When working with templates containing multiple variables, it's important to remember that locale files use positional references (`${0}`, `${1}`, etc.) rather than variable names. Consider adding comments to your locale files to make variable positions clear:
170+
171+
```js
172+
export const templates = {
173+
// ${0} = username, ${1} = count, ${2} = repo name
174+
commit_message: html`${0} made ${1} commits to ${2}`,
175+
176+
// ${0} = error code, ${1} = error message
177+
error_template: html`Error ${0}: ${1}`
178+
}
179+
```
180+
181+
This documentation helps translators understand the context of each variable and reduces errors when working with complex templates across multiple locales.
182+
183+
### Consider Variable Order in Your Template Design
184+
185+
When designing localizable templates, be mindful that different languages may require different variable orders. To make localization easier:
186+
187+
1. Design templates with clear, separable variables
188+
2. Avoid grammatical constructions that rely on specific word order
189+
3. Use IDs for templates that may need reordering of variables
190+
4. Test your UI with locales that have very different grammar structures
191+
192+
For example, this approach gives translators maximum flexibility:
193+
194+
```js
195+
/* In your component */
196+
msg(html`${username} sent ${fileCount} files to ${recipient}`, {
197+
id: 'file_transfer',
198+
desc: 'Message displayed after a successful file transfer'
199+
});
200+
201+
/* In your locale files */
202+
// English (follows Subject-Verb-Object order)
203+
export const templates = {
204+
file_transfer: html`${0} sent ${1} files to ${2}`
205+
}
206+
207+
// Japanese (follows Subject-Object-Verb order)
208+
export const templates = {
209+
file_transfer: html`${0}が${2}に${1}個のファイルを送信しました`
210+
}
211+
```
212+
213+
In this example, the variables can be placed in whatever order is grammatically correct for each language, making for a more natural user experience across different locales.
214+
215+
These practices make your application more resilient to the linguistic variations across different locales.
216+
167217
## Safely re-exporting or re-assigning localize APIs
168218
169219
Static analysis is used to determine when you are calling the `@lit/localize`

packages/lit-dev-content/site/docs/v3/localization/overview.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,51 @@ Localized messages can also be nested inside HTML templates:
135135
html`<button>${msg('Hello World')}</button>`;
136136
```
137137

138+
### Template Variable References in Locale Files
139+
140+
While template expressions in your component code use named variables (e.g., `${name}`, `${count}`),
141+
templates within locale files use **positional parameters** (`${0}`, `${1}`, etc.) rather than variable names.
142+
143+
When a template with an `id` is processed, variables are passed to the localized template in the
144+
order they appear in the original template, and are referenced by their index:
145+
146+
```js
147+
/* In your component */
148+
const address = 'user@host';
149+
msg(str`checking...${address}`, {id: 'check_email'});
150+
151+
/* In your locale file (e.g., locales/en.js) */
152+
export const templates = {
153+
check_email: html`checking ${0}`
154+
}
155+
```
156+
157+
This positional referencing allows translators to reorder variables when needed for different languages:
158+
159+
```js
160+
/* In your component */
161+
const name = 'Maria';
162+
const count = 42;
163+
msg(str`${name} has ${count} messages`, {id: 'user_messages'});
164+
165+
/* In your locale file */
166+
export const templates = {
167+
user_messages: html`${0} has ${1} messages`,
168+
// For languages where order might need to change:
169+
user_messages_reordered: html`Messages (${1}) for ${0}`
170+
}
171+
```
172+
173+
<div class="alert alert-info">
174+
175+
Note that the numeric references (${0}, ${1}, etc.) correspond to the order of variables
176+
in the original template, not their position in the resulting string.
177+
178+
</div>
179+
180+
For more examples of localized templates, see the [runtime mode](/docs/v3/localization/runtime-mode) page.
181+
182+
138183
### Strings with expressions
139184

140185
Strings that contain an expression must be tagged with either `html` or `str` in
@@ -237,7 +282,9 @@ compared to transform mode.
237282
```js
238283
// locales/es-419.ts
239284
export const templates = {
285+
// These IDs are automatically generated from template content
240286
hf71d669027554f48: html`Hola <b>Mundo</b>`,
287+
saed7d3734ce7f09d: html`Hola ${0}`, // Note: Variable references use numeric positions
241288
};
242289
```
243290

packages/lit-dev-content/site/docs/v3/localization/runtime-mode.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,40 @@ customElements.define('my-element', MyElement);
140140

141141
{% endswitchable-sample %}
142142

143+
## Working with Variables in Templates
144+
145+
When working with templates in runtime mode, remember that variables in locale files use positional references:
146+
147+
```js
148+
// In your component
149+
msg(html`<div>User ${userName} made ${count} commits</div>`, {id: 'user_activity'});
150+
151+
// In your locale file (e.g., locales/en.js)
152+
export const templates = {
153+
user_activity: html`<div>User ${0} made ${1} commits</div>`
154+
}
155+
156+
// In another locale file (e.g., locales/ja.js) - order can change if needed
157+
export const templates = {
158+
user_activity: html`<div>${0}さんは${1}回コミットしました</div>`
159+
}
160+
```
161+
162+
This numeric referencing system becomes especially important when:
163+
164+
1. Translating to languages with different word orders
165+
2. Working with formats that have placeholders in different positions
166+
3. Implementing plural forms that may rearrange variables
167+
168+
For more complex templates with many variables, consider adding descriptive comments in your locale files to help translators understand which position corresponds to which variable:
169+
170+
```js
171+
export const templates = {
172+
// ${0} = userName, ${1} = count
173+
user_activity: html`<div>User ${0} made ${1} commits</div>`
174+
}
175+
```
176+
143177
## Status event
144178

145179
The `lit-localize-status` event fires on `window` whenever a locale switch

0 commit comments

Comments
 (0)