Skip to content

Commit a4e3fef

Browse files
authored
Merge pull request #47 from PolymerLabs/new-readme
Expand lit-localize README with intro and tutorial
2 parents a826948 + 84372c4 commit a4e3fef

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed

README.md

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

7147
The `lit-localize` module exports the following functions:

rgb_lit.png

53.7 KB
Loading

0 commit comments

Comments
 (0)