Skip to content

Commit 8a32971

Browse files
committed
Expand lit-localize README with intro and tutorial
1 parent 144d71b commit 8a32971

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

README.md

+133-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,139 @@
22

33
[![Published on npm](https://img.shields.io/npm/v/lit-localize.svg)](https://www.npmjs.com/package/lit-localize) [![Test Status](https://github.com/PolymerLabs/lit-localize/workflows/tests/badge.svg?branch=master)](https://github.com/PolymerLabs/lit-localize/actions?query=workflow%3Atests+branch%3Amaster+event%3Apush)
44

5-
WIP
5+
<img src="./rgb_lit.png" height="100" align="right"></img>
6+
7+
###### [API](#api) | [Tutorial](#tutorial) | [API](#api)
8+
9+
> lit-localize is a library and command-line tool for localizing/translating web
10+
> applications that are based on lit-html and LitElement.
11+
12+
## Features
13+
14+
- 🌐 Localize your lit-html and LitElement applications
15+
- 🔥 Safely embed HTML markup within localized templates
16+
- 📄 Standard XLIFF interchange format
17+
- 🆓 Generate a zero-overhead bundle for each locale
18+
- 🔁 ... or dynamically load locales and automatically re-render
19+
20+
## Tutorial
21+
22+
1. Install lit-localize. You get both a client library and a command-line tool.
23+
You'll always use both together.
24+
25+
```bash
26+
npm install --save lit-localize
27+
```
28+
29+
2. Set up a TypeScript lit-html project if you don't have one already:
30+
31+
```bash
32+
npm install --save lit-html
33+
npm install --save-dev typescript
34+
npx tsc --init
35+
```
36+
37+
You'll want these TypeScript settings:
38+
39+
```json
40+
"compilerOptions": {
41+
"target": "es2018",
42+
"module": "esnext",
43+
"moduleResolution": "node"
44+
}
45+
```
46+
47+
You'll also need this directory to exist:
48+
49+
```bash
50+
mkdir xliff
51+
```
52+
53+
3. Create an `index.ts`, and declare a localizable template using the `msg`
54+
function. The first argument is a unique identifier for this template, and
55+
the second is a string or lit-html template.
56+
57+
```typescript
58+
import {html, render} from 'lit-html';
59+
import {msg} from 'lit-localize';
60+
61+
render(
62+
html`<p>${msg('greeting', html`Hello <b>World</b>!`)}</p>`,
63+
document.body
64+
);
65+
```
66+
67+
4. Make a JSON config file at the root of your project called
68+
`lit-localize.json`. In this example we're using _transform_ mode, but you
69+
can also use _runtime_ mode.
70+
71+
```json
72+
{
73+
"$schema": "https://raw.githubusercontent.com/PolymerLabs/lit-localize/master/config.schema.json",
74+
"sourceLocale": "en",
75+
"targetLocales": ["es-419"],
76+
"tsConfig": "tsconfig.json",
77+
"output": {
78+
"mode": "transform"
79+
},
80+
"interchange": {
81+
"format": "xliff",
82+
"xliffDir": "xliff/"
83+
}
84+
}
85+
```
86+
87+
5. Run the lit-localize CLI:
88+
89+
```bash
90+
npx lit-localize
91+
```
92+
93+
6. Take a look at the generated XLIFF file `xliff/es-419.xlf`. Note that we have
94+
a `<source>` template extracted from your source code, but we don't have a
95+
localized version yet. Also note that embedded HTML markup has been encoded
96+
into `<ph>` tags.
97+
98+
```xml
99+
<trans-unit id="greeting">
100+
<source>Hello <ph id="0">&lt;b></ph>World<ph id="1">&lt;/b></ph>!</source>
101+
</trans-unit>
102+
```
103+
104+
7. Edit `xliff/es-419.xlf` to add a `<target>` tag containing a localized
105+
version of the template. Usually you would use a tool or service to generate
106+
this tag by feeding it this XLIFF file.
107+
108+
```xml
109+
<trans-unit id="greeting">
110+
<source>Hello <ph id="0">&lt;b></ph>World<ph id="1">&lt;/b></ph>!</source>
111+
<target>Hola <ph id="0">&lt;b></ph>Mundo<ph id="1">&lt;/b></ph>!</target>
112+
</trans-unit>
113+
```
114+
115+
8. Run `lit-localize` again:
116+
117+
```bash
118+
npx lit-localize
119+
```
120+
121+
9. Now take a look at the generated file `es-419/index.js`:
122+
123+
```javascript
124+
import {html, render} from 'lit-html';
125+
render(html`<p>Hola <b>Mundo</b>!</p>`, document.body);
126+
```
127+
128+
and `en/index.js`:
129+
130+
```javascript
131+
import {html, render} from 'lit-html';
132+
render(html`<p>Hello <b>World</b>!</p>`, document.body);
133+
```
134+
135+
Note that the localized template has been substituted into your code, the
136+
`msg` call has been removed, the lit-html template has been reduced to its
137+
simplest form, and the `lit-localize` module is no longer imported.
6138

7139
## API
8140

rgb_lit.png

53.7 KB
Loading

0 commit comments

Comments
 (0)