Skip to content

Commit

Permalink
Add exports to types and helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
eelkevdbos committed Sep 12, 2023
1 parent 6fc6ab2 commit 6d0d2c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elysia-i18next",
"version": "1.0.2",
"version": "1.0.3",
"description": "I18n for Elysia.js via i18next",
"repository": "https:/eelkevdbos/elysia-i18next",
"author": {
Expand Down
36 changes: 20 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import Elysia, { PreContext } from 'elysia'
import { Elysia, PreContext } from 'elysia'
import lib, { InitOptions } from 'i18next'

type I18NextPluginOptions = {
export const t = lib.t

export type I18NextPluginOptions = {
initOptions: InitOptions
detectLanguage: <T extends PreContext>(
ctx: T
) => null | string | Promise<string | null>
}

const detectLanguage = (ctx: PreContext) => {
export const detectLanguage = (ctx: PreContext) => {
if ('language' in ctx.store) {
return ctx.store.language as string | null
}
Expand All @@ -21,17 +23,19 @@ const defaultOptions: I18NextPluginOptions = {
}

export const i18next = (userOptions: Partial<I18NextPluginOptions>) => {
const options: I18NextPluginOptions = {
...defaultOptions,
...userOptions,
}
const i18next = new Elysia({ name: 'elysia-i18next' }).derive(async ctx => {
await lib.init(options.initOptions || {})
const lng = await options.detectLanguage<typeof ctx>(ctx)
if (lng) {
await lib.changeLanguage(lng)
}
return { i18next: lib, t: lib.t }
})
return (app: Elysia) => app.use(i18next)
return (app: Elysia) =>
app.use(
new Elysia({ name: 'elysia-i18next' }).derive(async ctx => {
const options: I18NextPluginOptions = {
...defaultOptions,
...userOptions,
}
await lib.init(options.initOptions || {})
const lng = await options.detectLanguage<typeof ctx>(ctx)
if (lng) {
await lib.changeLanguage(lng)
}
return { i18next: lib, t: lib.t }
})
)
}

0 comments on commit 6d0d2c9

Please sign in to comment.