Skip to content

Commit

Permalink
Update readme and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
eelkevdbos committed Sep 12, 2023
1 parent ae8db56 commit fd7370a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
39 changes: 24 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ Usage
Check out full samples at [`examples`](./examples/) or check out the tests [`tests`](src/index.test.ts).

```ts
import {Elysia} from 'elysia'
import {i18next} from 'elysia-i18next'
import { Elysia } from "elysia";
import { i18next } from "elysia-i18next";

new Elysia()
.use(
i18next({
initOptions: {
lng: 'nl',
lng: "nl",
resources: {
en: {
translation: {
Expand All @@ -36,11 +36,11 @@ new Elysia()
},
},
},
}
})
},
}),
)
.get('/', ({t}) => t('greeting')) // returns "Hallo"
.listen(3000)
.get("/", ({ t }) => t("greeting")) // returns "Hallo"
.listen(3000);
```

Configuration
Expand All @@ -56,16 +56,25 @@ Check out the [i18next documentation](https://www.i18next.com/overview/configura

### detectLanguage

`(ctx: PreContext) => string`
`LanguageDetector`

default:
```ts
(ctx: PreContext) => {
if ('language' in ctx.store) {
return ctx.store.language as string | null
}
return ctx.request.headers.get('accept-language')
}
newLanguageDetector({
searchParamName: 'lang',
storeParamName: 'language',
headerName: 'accept-language',
cookieName: 'lang',
pathParamName: 'lang',
})
```

A language detection function, by default it checks the `language` property on the store, or the `accept-language` header.
A language detection function based on the current request context. By default, it checks the `language` property on the store, `lang` param in the query string, a cookie named `lang`, a path parameter named `lang` or the `accept-language` header.

### instance

`null | i18next.i18n`

Default: `null`

An existing i18next instance. If not provided, the global (module level) i18next instance will be used. The instance will be initialized if it not already was initialized.
16 changes: 16 additions & 0 deletions examples/detect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Elysia } from 'elysia'
import { i18next, newLanguageDetector, LanguageDetector } from 'elysia-i18next'

const myDetector = newLanguageDetector({
searchParamName: 'x-lang',
cookieName: 'x-lang',
headerName: 'x-lang',
pathParamName: 'x-lang',
storeParamName: 'x-lang',
})

new Elysia().use(i18next({ detectLanguage: myDetector })).listen(3000)

const brokenDetector: LanguageDetector = ctx => 'en'

new Elysia().use(i18next({ detectLanguage: brokenDetector })).listen(3000)
7 changes: 7 additions & 0 deletions examples/instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Elysia } from 'elysia'
import { createInstance } from 'i18next'
import { i18next } from 'elysia-i18next'

const i18n = createInstance()

new Elysia().use(i18next({ instance: i18n })).listen(3000)

0 comments on commit fd7370a

Please sign in to comment.