forked from Yuripetusko/koa-i18next-middleware-fixed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (54 loc) · 1.44 KB
/
index.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const Koa = require('koa');
const i18next = require('i18next');
const app = new Koa();
import LD from "koa-i18next-detector";
const i18m = require('./src');
i18next.use(LD).init({
fallbackLng: 'en',
preload: ['en', 'es'],
resources: {
en: {
translation: {
"key": "hello world"
}
},
es: {
translation: {
"key": "es hello world es"
}
}
},
detection: {
order: ['querystring', /*'path', 'cookie',*/ 'header', 'session'],
lookupQuerystring: 'lng',
lookupParam: 'lng', // for route like: 'path1/:lng/result'
lookupFromPathIndex: 0,
lookupCookie: 'i18next',
// cookieExpirationDate: new Date(), // default: +1 year
// cookieDomain: '', // default: current domain.
lookupSession: 'lng',
// cache user language
caches: false // ['cookie']
}
}, (err, t) =>{
// initialized and ready to go!
const hw = i18next.t('key'); // hw = 'hello world'
console.log(hw);
});
app.use(i18m.getHandler(i18next, {
locals: 'locals',
ignoreRoutes: ['/no-lng-route'],
})
);
// response
app.use(async (ctx, next) =>{
await next();
if (ctx.request.lng) {
ctx.body = ctx.response.locals.t(`key`);
} else {
ctx.body = 'No language detected!'
}
});
app.listen(3000, function(){
console.log('server listening');
});