Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed missing newLanguageDetector export #3

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it } from 'bun:test'
import { beforeEach, describe, expect, it } from 'bun:test'
import Elysia from 'elysia'
import { i18next } from './index.ts'
import { i18next, newLanguageDetector, LanguageDetector } from './index.ts'
import { createInstance, InitOptions, i18n } from 'i18next'

export const req = (path: string, requestInit?: RequestInit) =>
Expand Down Expand Up @@ -110,4 +110,21 @@ describe('i18next', () => {
const response = await app.handle(req('/'))
expect(await response.text()).toEqual('Bonjour!')
})

it('changes language using a custom LanguageDetector created with newLanguageDetector()', async () => {
const detector: LanguageDetector = newLanguageDetector({
searchParamName: 'x-lang',
storeParamName: 'x-lang',
headerName: 'x-lang',
cookieName: 'x-lang',
pathParamName: 'x-lang',
})

const app = new Elysia()
.use(i18next({ instance, detectLanguage: detector }))
.get('/', ({ t }) => t('greeting'))

const response = await app.handle(req('/?x-lang=en'))
expect(await response.text()).toEqual('Hello!')
})
})
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type LanguageDetector<
T extends Context<RouteSchema> = Context<RouteSchema>,
> = (ctx: T) => null | string | Promise<string | null>

function newLanguageDetector(opts: LanguageDetectorOptions): LanguageDetector {
export function newLanguageDetector(opts: LanguageDetectorOptions): LanguageDetector {
return ({ set, request, params, store }) => {
const url = new URL(request.url)

Expand Down