UNPKG

stackpress

Version:

Incept is a content management framework.

40 lines (39 loc) 1.79 kB
import Language from './Language.js'; export default function plugin(ctx) { if (!ctx.config.get('language')) return; ctx.on('config', async (_req, _res, ctx) => { ctx.register('language', Language.configure(ctx.config.path('language.key', 'locale'), ctx.config.path('language.languages', {}))); }); ctx.on('listen', async (_req, _res, ctx) => { const language = ctx.plugin('language'); ctx.on('request', async (req, res, ctx) => { const key = ctx.config.path('language.key', 'locale'); const defaultLocale = ctx.config.path('language.locale', 'en_US'); let locale = req.data(key); if (language.locales.includes(locale)) { language.load(req, defaultLocale).update(locale, res); } const pathArray = req.url.pathname.split('/'); locale = pathArray[1]; if (language.locales.includes(locale)) { language.load(req, defaultLocale).update(locale, res); pathArray.splice(1, 1); const request = ctx.request({ resource: req.resource, body: req.body || undefined, headers: Object.fromEntries(req.headers.entries()), mimetype: req.mimetype, data: req.data(), method: req.method, query: Object.fromEntries(req.query.entries()), post: Object.fromEntries(req.post.entries()), session: req.session.data, url: new URL(req.url.origin + pathArray.join('/')) }); await ctx.resolve(req.method, pathArray.join('/'), request, res); } }); }); } ;