### Install elysia-i18next and i18next Source: https://github.com/eelkevdbos/elysia-i18next/blob/main/README.md Installs the necessary packages for Elysia i18next integration using bun. ```bash bun add elysia-i18next i18next ``` -------------------------------- ### Custom i18next Instance with Elysia Source: https://context7.com/eelkevdbos/elysia-i18next/llms.txt This example shows how to configure elysia-i18next with a dedicated i18next instance, rather than relying on the global singleton. This allows for isolated configurations and resources per application or route. ```typescript import { Elysia } from "elysia"; import { createInstance } from "i18next"; import { i18next } from "elysia-i18next"; const customInstance = createInstance({ lng: "fr", resources: { en: { translation: { greeting: "Hello!" } }, fr: { translation: { greeting: "Bonjour!" } }, }, }); const app = new Elysia() .use(i18next({ instance: customInstance })) .get("/", ({ t }) => t("greeting")) .listen(3000); // GET / → "Bonjour!" ``` -------------------------------- ### Elysia.js Basic i18next Integration Source: https://github.com/eelkevdbos/elysia-i18next/blob/main/README.md Demonstrates a basic setup of Elysia.js with elysia-i18next. It configures i18next with English and Dutch translations and provides a simple endpoint that returns a translated greeting based on the default language. ```typescript import { Elysia } from "elysia"; import { i18next } from "elysia-i18next"; new Elysia() .use( i18next({ initOptions: { lng: "nl", resources: { en: { translation: { greeting: "Hi", }, }, nl: { translation: { greeting: "Hallo", }, }, }, }, }), ) .get("/", ({ t }) => t("greeting")) // returns "Hallo" .listen(3000); ``` -------------------------------- ### Elysia Translation with Interpolation using i18next Source: https://context7.com/eelkevdbos/elysia-i18next/llms.txt This example demonstrates how to leverage i18next's interpolation features within an Elysia application. It shows how to pass dynamic values to translation keys to create personalized messages. ```typescript import { Elysia } from "elysia"; import { i18next } from "elysia-i18next"; const app = new Elysia() .use( i18next({ initOptions: { lng: "en", resources: { en: { translation: { welcome: "Welcome, {{name}}!", items: "You have {{count}} items in your cart.", }, }, es: { translation: { welcome: "¡Bienvenido, {{name}}!", items: "Tienes {{count}} artículos en tu carrito.", }, }, }, }, }) ) .get("/user/:name", ({ t, params }) => t("welcome", { name: params.name }) ) .get("/cart/:count", ({ t, params }) => t("items", { count: parseInt(params.count) }) ) .listen(3000); // GET /user/John → "Welcome, John!" // GET /cart/5 → "You have 5 items in your cart." // GET /cart/5?lang=es → "Tienes 5 artículos en tu carrito." ``` -------------------------------- ### Initialize i18next Plugin with Elysia Source: https://context7.com/eelkevdbos/elysia-i18next/llms.txt Initializes the elysia-i18next plugin to provide translation capabilities to Elysia.js routes. It requires defining language resources and fallback language. The translation function `t` is made available in the route handler context. ```typescript import { Elysia } from "elysia"; import { i18next } from "elysia-i18next"; const app = new Elysia() .use( i18next({ initOptions: { lng: "nl", resources: { en: { translation: { greeting: "Hello", farewell: "Goodbye", }, }, nl: { translation: { greeting: "Hallo", farewell: "Tot ziens", }, }, }, fallbackLng: "en", }, }) ) .get("/", ({ t }) => t("greeting")) .get("/bye", ({ t }) => t("farewell")) .listen(3000); // GET / → "Hallo" // GET /bye → "Tot ziens" ``` -------------------------------- ### Custom Language Detector Configuration with Elysia Source: https://context7.com/eelkevdbos/elysia-i18next/llms.txt Demonstrates how to create and use a custom language detector in elysia-i18next by specifying parameter names for query, cookie, header, and path. This provides flexibility in how the user's language preference is identified. ```typescript import { Elysia } from "elysia"; import { i18next, newLanguageDetector } from "elysia-i18next"; const customDetector = newLanguageDetector({ searchParamName: "locale", cookieName: "user_lang", headerName: "x-user-language", pathParamName: "locale", storeParamName: "userLanguage", }); const app = new Elysia() .use( i18next({ detectLanguage: customDetector, initOptions: { lng: "en", resources: { en: { translation: { greeting: "Hello!" } }, es: { translation: { greeting: "¡Hola!" } }, }, }, }) ) .get("/", ({ t }) => t("greeting")) .listen(3000); // GET /?locale=es → "¡Hola!" // GET / (with header x-user-language: es) → "¡Hola!" ``` -------------------------------- ### Default Language Detector Configuration Source: https://github.com/eelkevdbos/elysia-i18next/blob/main/README.md Shows the default configuration for the language detector in elysia-i18next. This detector prioritizes language based on query parameters, cookies, path parameters, and the 'accept-language' header. ```typescript newLanguageDetector({ searchParamName: 'lang', storeParamName: 'language', headerName: 'accept-language', cookieName: 'lang', pathParamName: 'lang', }) ``` -------------------------------- ### Accessing i18next Instance in Elysia Source: https://context7.com/eelkevdbos/elysia-i18next/llms.txt This snippet shows how to directly access the i18next instance within Elysia route handlers. This allows for performing advanced i18next operations like changing language dynamically or accessing the translation store. ```typescript import { Elysia } from "elysia"; import { i18next } from "elysia-i18next"; const app = new Elysia() .use( i18next({ initOptions: { lng: "en", resources: { en: { translation: { greeting: "Hello" }, common: { button: "Click me" }, }, }, }, }) ) .get("/", ({ i18n }) => ({ greeting: i18n.t("greeting"), button: i18n.t("common:button"), currentLanguage: i18n.language, availableLanguages: Object.keys(i18n.store.data), })) .get("/switch/:lang", async ({ i18n, params, t }) => { await i18n.changeLanguage(params.lang); return t("greeting"); }) .listen(3000); // GET / → {"greeting":"Hello","button":"Click me","currentLanguage":"en","availableLanguages":["en"]} ``` -------------------------------- ### Custom Language Detection Function in Elysia Source: https://context7.com/eelkevdbos/elysia-i18next/llms.txt Illustrates how to implement a fully custom language detection strategy by providing a function to elysia-i18next. This function receives the request object and returns the detected language code. ```typescript import { Elysia } from "elysia"; import { i18next, LanguageDetector } from "elysia-i18next"; const detectFromSubdomain: LanguageDetector = ({ request }) => { const hostname = new URL(request.url).hostname; const subdomain = hostname.split(".")[0]; if (subdomain === "en") return "en"; if (subdomain === "fr") return "fr"; if (subdomain === "de") return "de"; return "en"; }; const app = new Elysia() .use( i18next({ detectLanguage: detectFromSubdomain, initOptions: { lng: "en", resources: { en: { translation: { greeting: "Hello!" } }, fr: { translation: { greeting: "Bonjour!" } }, de: { translation: { greeting: "Hallo!" } }, }, }, }) ) .get("/", ({ t }) => t("greeting")) .listen(3000); // http://en.example.com/ → "Hello!" // http://fr.example.com/ → "Bonjour!" // http://de.example.com/ → "Hallo!" ``` -------------------------------- ### Detect Language from Elysia Store State Source: https://context7.com/eelkevdbos/elysia-i18next/llms.txt Enables language detection by reading the language preference from Elysia's application state (store). This provides a way to manage language settings within the application's context. The translation function `t` is accessible in route handlers. ```typescript import { Elysia } from "elysia"; import { i18next } from "elysia-i18next"; const app = new Elysia() .state("language", "en") .use( i18next({ initOptions: { lng: "nl", resources: { en: { translation: { greeting: "Hello!" } }, nl: { translation: { greeting: "Hallo!" } }, }, }, }) ) .get("/", ({ t }) => t("greeting")) .listen(3000); // GET / → "Hello!" (uses state value "en" instead of default "nl") ``` -------------------------------- ### Detect Language from Query Parameter Source: https://context7.com/eelkevdbos/elysia-i18next/llms.txt Configures elysia-i18next to detect the user's preferred language from a query parameter (e.g., `?lang=en`). This allows users to switch languages by modifying the URL. The plugin makes the translation function `t` available in route handlers. ```typescript import { Elysia } from "elysia"; import { i18next } from "elysia-i18next"; const app = new Elysia() .use( i18next({ initOptions: { lng: "nl", resources: { en: { translation: { greeting: "Hello!" } }, nl: { translation: { greeting: "Hallo!" } }, fr: { translation: { greeting: "Bonjour!" } }, }, }, }) ) .get("/", ({ t }) => t("greeting")) .listen(3000); // GET / → "Hallo!" // GET /?lang=en → "Hello!" // GET /?lang=fr → "Bonjour!" ``` -------------------------------- ### Detect Language from Accept-Language Header Source: https://context7.com/eelkevdbos/elysia-i18next/llms.txt Enables language detection based on the `Accept-Language` HTTP header sent by the client. This is a common method for servers to infer user language preferences. The `t` function is accessible within route handlers. ```typescript import { Elysia } from "elysia"; import { i18next } from "elysia-i18next"; const app = new Elysia() .use( i18next({ initOptions: { lng: "nl", resources: { en: { translation: { greeting: "Hello!" } }, nl: { translation: { greeting: "Hallo!" } }, }, }, }) ) .get("/", ({ t }) => t("greeting")) .listen(3000); // curl http://localhost:3000/ → "Hallo!" // curl -H "Accept-Language: en" http://localhost:3000/ → "Hello!" ``` -------------------------------- ### Detect Language from Path Parameter Source: https://context7.com/eelkevdbos/elysia-i18next/llms.txt Extracts the language code from a URL path parameter (e.g., `/en/greet`). This method is useful for language-specific URLs and SEO. The `t` function is made available in route handlers to perform translations based on the detected language. ```typescript import { Elysia } from "elysia"; import { i18next } from "elysia-i18next"; const app = new Elysia() .use( i18next({ initOptions: { lng: "nl", resources: { en: { translation: { greeting: "Hello!" } }, nl: { translation: { greeting: "Hallo!" } }, fr: { translation: { greeting: "Bonjour!" } }, }, }, }) ) .get("/:lang/greet", ({ t }) => t("greeting")) .listen(3000); // GET /en/greet → "Hello!" // GET /fr/greet → "Bonjour!" // GET /nl/greet → "Hallo!" ``` -------------------------------- ### Detect Language from Cookie Source: https://context7.com/eelkevdbos/elysia-i18next/llms.txt Allows language detection by reading a specified cookie (e.g., `lang`). This snippet demonstrates setting a cookie value before the request is handled, enabling persistent language preferences. The translation function `t` is available in route handlers. ```typescript import { Elysia } from "elysia"; import { i18next } from "elysia-i18next"; const app = new Elysia() .onBeforeHandle(({ cookie: { lang } }) => { lang.value = "en"; }) .use( i18next({ initOptions: { lng: "nl", resources: { en: { translation: { greeting: "Hello!" } }, nl: { translation: { greeting: "Hallo!" } }, }, }, }) ) .get("/", ({ t }) => t("greeting")) .listen(3000); // GET / (with cookie lang=en) → "Hello!" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.