-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
395 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# run the sample | ||
|
||
``` | ||
$ npm i | ||
$ npm start | ||
``` | ||
|
||
open: http://localhost:8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const Koa = require('koa') | ||
const router = require('@koa/router')() | ||
const koaBody = require('koa-body').default | ||
const serve = require('koa-static') | ||
const mount = require('koa-mount') | ||
const i18next = require('i18next') | ||
// const i18nextMiddleware = require('i18next-http-middleware') | ||
const i18nextMiddleware = require('../../../i18next-http-middleware') | ||
const Backend = require('i18next-fs-backend') | ||
// const Backend = require('../../../i18next-fs-backend') | ||
|
||
const app = new Koa() | ||
app.use(koaBody({ | ||
jsonLimit: '1kb' | ||
})) | ||
const port = process.env.PORT || 8080 | ||
|
||
i18next | ||
.use(Backend) | ||
// .use(languageDetector) | ||
.use(i18nextMiddleware.LanguageDetector) | ||
.init({ | ||
// debug: true, | ||
// detection: { | ||
// order: ['customDetector'] | ||
// }, | ||
backend: { | ||
// eslint-disable-next-line no-path-concat | ||
loadPath: __dirname + '/locales/{{lng}}/{{ns}}.json', | ||
// eslint-disable-next-line no-path-concat | ||
addPath: __dirname + '/locales/{{lng}}/{{ns}}.missing.json' | ||
}, | ||
fallbackLng: 'en', | ||
// nonExplicitSupportedLngs: true, | ||
// supportedLngs: ['en', 'de'], | ||
load: 'languageOnly', | ||
saveMissing: true | ||
}) | ||
|
||
app.use(i18nextMiddleware.koaPlugin(i18next)) | ||
|
||
router.get('/', ctx => { | ||
ctx.body = JSON.stringify({ | ||
'ctx.language': ctx.language, | ||
'ctx.i18n.language': ctx.i18n.language, | ||
'ctx.i18n.languages': ctx.i18n.languages, | ||
'ctx.i18n.languages[0]': ctx.i18n.languages[0], | ||
'ctx.t("home.title")': ctx.t('home.title') | ||
}, null, 2) | ||
}) | ||
|
||
|
||
router.get('/missingtest', ctx => { | ||
ctx.t('nonExisting', 'some default value') | ||
ctx.body = 'check the locales files...' | ||
}) | ||
|
||
// loadPath for client: http://localhost:8080/locales/{{lng}}/{{ns}}.json | ||
app.use(mount('/locales', serve('./locales'))) | ||
|
||
// or instead of static | ||
// router.get('/locales/:lng/:ns', i18nextMiddleware.getResourcesHandler(i18next)) | ||
// loadPath for client: http://localhost:8080/locales/{{lng}}/{{ns}} | ||
|
||
// missing keys make sure the body is parsed (i.e. with [body-parser](https://github.com/expressjs/body-parser#bodyparserjsonoptions)) | ||
router.post('/locales/add/:lng/:ns', i18nextMiddleware.missingKeyHandler(i18next)) | ||
// The client can be configured with i18next-http-backend, for example like this: | ||
// import HttpBackend from 'i18next-http-backend' | ||
// i18next.use(HttpBackend).init({ | ||
// lng: 'en', | ||
// fallbackLng: 'en', | ||
// backend: { | ||
// loadPath: 'http://localhost:8080/locales/{{lng}}/{{ns}}.json', | ||
// addPath: 'http://localhost:8080/locales/add/{{lng}}/{{ns}}' | ||
// } | ||
// }) | ||
|
||
app.use(router.routes()) | ||
|
||
app.listen(port, () => { | ||
console.log(`Server is listening on port ${port}`) | ||
}) | ||
|
||
// curl localhost:8080 -H 'Accept-Language: de-de' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"home": { | ||
"title": "Hallo Welt!" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"home": { | ||
"title": "Hello World!" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"home": { | ||
"title": "Bonjour le monde!" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "i18next-express-basic", | ||
"version": "1.0.0", | ||
"description": "Node Express server with i18next.", | ||
"main": "index.js", | ||
"type": "commonjs", | ||
"scripts": { | ||
"start": "node index.js" | ||
}, | ||
"dependencies": { | ||
"@koa/router": "12.0.0", | ||
"i18next": "23.5.1", | ||
"i18next-fs-backend": "2.2.0", | ||
"i18next-http-middleware": "3.3.2", | ||
"koa": "2.14.2", | ||
"koa-body": "6.0.1", | ||
"koa-mount": "4.0.0", | ||
"koa-static": "5.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import expect from 'expect.js' | ||
import i18nextMiddleware from '../index.js' | ||
import i18next from 'i18next' | ||
import Koa from 'koa' | ||
import Router from '@koa/router' | ||
import request from 'supertest' | ||
|
||
const router = Router() | ||
i18next.init({ | ||
fallbackLng: 'en', | ||
preload: ['en', 'de'], | ||
saveMissing: true | ||
}) | ||
|
||
describe('addRoute koa', () => { | ||
describe('and handling a request', () => { | ||
const app = new Koa() | ||
app.use(i18nextMiddleware.koaPlugin(i18next)) | ||
let server | ||
|
||
before((done) => { | ||
server = app.listen(7002, done) | ||
}) | ||
after((done) => server.close(done)) | ||
|
||
it('should return the appropriate resource', (done) => { | ||
app.use(i18nextMiddleware.handle(i18next)) | ||
const handle = (ctx) => { | ||
expect(ctx).to.have.property('lng', 'en') | ||
expect(ctx).to.have.property('locale', 'en') | ||
expect(ctx).to.have.property('language', 'en') | ||
expect(ctx).to.have.property('languages') | ||
expect(ctx.languages).to.eql(['en']) | ||
expect(ctx).to.have.property('i18n') | ||
expect(ctx).to.have.property('t') | ||
expect(ctx.t('key')).to.eql('key') | ||
ctx.body = ctx.t('key') | ||
} | ||
i18nextMiddleware.addRoute(i18next, '/myroute/:lng/:ns', ['en'], router, 'get', handle) | ||
|
||
app.use(router.routes()) | ||
|
||
request(server) | ||
.get('/myroute/en/test') | ||
.expect('Content-Language', 'en') | ||
.expect(200, (err, res) => { | ||
expect(err).not.to.be.ok() | ||
expect(res.text).to.eql('key') | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import expect from 'expect.js' | ||
import i18nextMiddleware from '../index.js' | ||
import i18next from 'i18next' | ||
import Koa from 'koa' | ||
import Router from '@koa/router' | ||
import request from 'supertest' | ||
|
||
const router = Router() | ||
i18next.init({ | ||
fallbackLng: 'en', | ||
preload: ['en', 'de'], | ||
saveMissing: true, | ||
resources: { | ||
en: { | ||
translation: { hi: 'there' } | ||
} | ||
} | ||
}) | ||
|
||
describe('getResourcesHandler koa', () => { | ||
describe('handling a request', () => { | ||
const app = new Koa() | ||
let server | ||
|
||
before((done) => { | ||
server = app.listen(7002, done) | ||
}) | ||
after((done) => server.close(done)) | ||
|
||
it('should return the appropriate resource', (done) => { | ||
router.get('/', i18nextMiddleware.getResourcesHandler(i18next)) | ||
|
||
app.use(router.routes()) | ||
|
||
request(server) | ||
.get('/') | ||
.query({ | ||
lng: 'en', | ||
ns: 'translation' | ||
}) | ||
.expect('content-type', /json/) | ||
.expect('cache-control', 'no-cache') | ||
.expect('pragma', 'no-cache') | ||
.expect(200, (err, res) => { | ||
expect(err).not.to.be.ok() | ||
expect(res.body).to.have.property('en') | ||
expect(res.body.en).to.have.property('translation') | ||
expect(res.body.en.translation).to.have.property('hi', 'there') | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.