Skip to content

Commit aa694f0

Browse files
committed
update localization docs
1 parent d41f60d commit aa694f0

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

.github/workflows/weblate-reformat.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Formats and builds frontend UI translation files on pull requests
2+
# from https://github.com/weblate/browsertrix/tree/weblate-browsertrix-browsertrix
3+
#
4+
# Pull requests are automatically created by Hosted Weblate.
5+
# See https://docs.browsertrix.com/develop/localization/
16
name: Weblate Reformat
27
on:
38
pull_request_target

frontend/docs/docs/develop/localization.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Adding support for a new language involves a small code change. If you'd like to
1515
To add a new language directly through code change:
1616

1717
1. Look up the [BCP 47 language tag](https://www.w3.org/International/articles/language-tags/index.en#registry) and add it to the `targetLocales` field in `lit-localize.json`.
18+
1819
```js
1920
{
2021
// ...
@@ -27,9 +28,11 @@ To add a new language directly through code change:
2728
```
2829

2930
2. Generate a new XLIFF file by running:
31+
3032
```sh
3133
yarn localize:extract
3234
```
35+
3336
This will add an `.xlf` file named after the new language tag to the `/xliff` directory.
3437

3538
3. Open a pull request with the changes.
@@ -54,4 +57,76 @@ render() {
5457
}
5558
```
5659

57-
See [Lit's message types documentation](https://lit.dev/docs/localization/overview/#message-types) page for details.
60+
### Handling expressions in strings
61+
62+
Expressions can be included in strings:
63+
64+
```js
65+
import { msg, str } from "@lit/localize";
66+
67+
msg(str`Welcome, ${name}.`)
68+
```
69+
70+
Translators will see the string expression as-written in code. To aid translations, avoid calculations in expressions and choose a descriptive variable name.
71+
72+
```js
73+
// Instead of this:
74+
//
75+
// msg(str`This file exceeds the maximum of ${5 * 1000 * 1000} bytes.`).
76+
77+
// Try this:
78+
const bytes = 5 * 1000 * 1000;
79+
80+
msg(str`This file exceeds the maximum of ${bytes} bytes.`).
81+
```
82+
83+
Dates and numbers should be localized and pluralized in source code before being assigned to the message string.
84+
85+
For example:
86+
87+
```js
88+
import localize from "@/utils/localize";
89+
import { pluralOf } from "@/utils/pluralize";
90+
91+
const date = localize.date(new Date());
92+
const count = 200;
93+
const number_of_URLs = `${localize.number(count)} ${pluralOf("URLs", count)}`;
94+
95+
msg(str`You have ${number_of_URLs} pending as of ${date}.`);
96+
```
97+
98+
!!! Tip "Tip: Include a message description for translators."
99+
You can add additional context for translators using the `desc` option when the variable name may be ambiguous by itself.
100+
101+
Building on the previous example:
102+
103+
```js
104+
msg(str`You have ${number_of_URLs} pending as of ${date}.`, {
105+
desc: "`number_of_URLs` example: '1,000 URLs'"
106+
});
107+
```
108+
109+
### Handling HTML in strings
110+
111+
Lit supports HTML in translation strings. However, try to avoid including markup in strings by using multiple `msg()`s, as HTML makes it more difficult for translators to update strings in the Weblate interface, as well as more performance overhead.
112+
113+
```js
114+
// Instead of this:
115+
//
116+
// msg(html`Would you like to continue? <button>Continue</button>`)
117+
118+
// Do this:
119+
html`
120+
${msg("Would you like to continue?")} <button>${msg("Continue")}</button>
121+
`
122+
```
123+
124+
When markup is unavoidable, prefer assigning the template to a variable.
125+
126+
```js
127+
const log_in = html`<a href="/log-in">${msg("log in")}</a>`
128+
129+
msg(html`Please ${log_in} to access this page.`, {
130+
desc: "`log_in` is a link to the log in page"
131+
})
132+
```

0 commit comments

Comments
 (0)