UNPKG

@softkit/i18n

Version:

This library is a simple wrapper based on [nestjs-i18n](https://nestjs-i18n.com/)

111 lines 3.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.I18nMiddleware = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const core_1 = require("@nestjs/core"); const i18n_constants_1 = require("../i18n.constants"); const i18n_service_1 = require("../services/i18n.service"); const i18n_error_1 = require("../i18n.error"); const i18n_context_1 = require("../i18n.context"); const utils_1 = require("../utils"); const type_guards_1 = require("../utils/type-guards"); const ExecutionContextMethodNotImplemented = new i18n_error_1.I18nError("Method not implemented. @softkit/i18n creates a fake Http context since it's using middleware to resolve your language. Nestjs middlewares don't have access to the ExecutionContext."); let I18nMiddleware = class I18nMiddleware { constructor(i18nOptions, i18nResolvers, i18nService, moduleRef) { this.i18nOptions = i18nOptions; this.i18nResolvers = i18nResolvers; this.i18nService = i18nService; this.moduleRef = moduleRef; } async use(req, res, next) { let language = null; // Skip middleware if language is already resolved if (req.i18nLang) { return next(); } req.i18nService = this.i18nService; for (const r of this.i18nResolvers) { const resolver = await this.getResolver(r); language = await resolver.resolve(new MiddlewareHttpContext(req, res, next)); if (language !== undefined) { break; } } req.i18nLang = language || this.i18nOptions.fallbackLanguage; // Pass down language to handlebars if (req.app) { req.app.locals.i18nLang = req.i18nLang; } req.i18nContext = new i18n_context_1.I18nContext(req.i18nLang, this.i18nService); if (this.i18nOptions.skipAsyncHook) { next(); } else { i18n_context_1.I18nContext.create(req.i18nContext, next); } } async getResolver(r) { if ((0, utils_1.shouldResolve)(r)) { if ((0, type_guards_1.isResolverWithOptions)(r)) { const resolver = r; return this.moduleRef.get(resolver.use); } else { return this.moduleRef.get(r); } } else { return r; } } }; exports.I18nMiddleware = I18nMiddleware; exports.I18nMiddleware = I18nMiddleware = tslib_1.__decorate([ (0, common_1.Injectable)(), tslib_1.__param(0, (0, common_1.Inject)(i18n_constants_1.I18N_OPTIONS)), tslib_1.__param(1, (0, common_1.Inject)(i18n_constants_1.I18N_RESOLVERS)), tslib_1.__metadata("design:paramtypes", [Object, Array, i18n_service_1.I18nService, core_1.ModuleRef]) ], I18nMiddleware); class MiddlewareHttpContext { constructor(req, res, next) { this.req = req; this.res = res; this.next = next; } getClass() { throw ExecutionContextMethodNotImplemented; } getHandler() { throw ExecutionContextMethodNotImplemented; } getArgs() { throw ExecutionContextMethodNotImplemented; } getArgByIndex() { throw ExecutionContextMethodNotImplemented; } switchToRpc() { throw ExecutionContextMethodNotImplemented; } switchToHttp() { return this; } switchToWs() { throw ExecutionContextMethodNotImplemented; } getType() { return 'http'; } getRequest() { return this.req; } getResponse() { return this.res; } getNext() { return this.next; } } //# sourceMappingURL=i18n.middleware.js.map