UNPKG

remix-hono

Version:
45 lines 1.6 kB
import { createMiddleware } from "hono/factory"; import { RemixI18Next } from "remix-i18next/server"; const i18nSymbol = Symbol(); const LocaleSymbol = Symbol(); const TSymbol = Symbol(); export function i18next(options) { return createMiddleware(async (c, next) => { let i18n = options instanceof RemixI18Next ? options : new RemixI18Next(options); let locale = await i18n.getLocale(c.req.raw); let t = await i18n.getFixedT(locale); c.set(i18nSymbol, i18n); c.set(LocaleSymbol, locale); c.set(TSymbol, t); await next(); }); } i18next.get = function get(c) { let i18n = c.get(i18nSymbol); if (!i18n) { throw new Error("The i18next middleware must run before calling `i18next.getI18n()`"); } return i18n; }; i18next.getLocale = function getLocale(c) { let locale = c.get(LocaleSymbol); if (!locale) { throw new Error("The i18next middleware must run before calling `i18next.getLocale()`"); } return locale; }; i18next.getFixedT = function getFixedT(c, { namespace } = {}) { // If `namespace` is set, we return a new `t` function that is bound to the // given namespace. Otherwise, we return the default `t` function. if (namespace) { let i18n = i18next.get(c); let locale = i18next.getLocale(c); return i18n.getFixedT(locale, namespace); } let t = c.get(TSymbol); if (!t) { throw new Error("The i18next middleware must run before calling `i18next.getFixedT()`"); } return Promise.resolve(t); }; //# sourceMappingURL=i18next.js.map