-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
37 lines (30 loc) · 984 Bytes
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import express from 'express';
import {setDefaultLocale, addLocale, loc} from '../../src';
import translator from '../../src/express';
const app = express()
const port = 3000;
// lets say our developpers use latin to write original strings in code
setDefaultLocale('la');
// add translations for actual languages...
addLocale('fr-FR', {
hello: 'Bonjour tout le monde !',
arg: 'Vous êtes ici: {0}',
});
addLocale('en-US', {
hello: 'Hello world !',
arg: 'You are here: {0}',
});
// use the translator midlleware
app.use(translator());
// Try browsing http://localhost:3000/
app.get('/', (req, res) => {
res.json({
answer: loc('hello')`Salve mundi !`,
});
});
// Try browsing http://localhost:3000/something
app.get('/:arg', (req, res) => {
res.send(loc('arg')`Hic es ${req.params.arg}`)
});
app.listen(port, () => console.log(`Localized app listening on port ${port}!
Try visiting http://localhost:3000/ or http://localhost:3000/somewhere`))