@duvdu-v1/duvdu 1.1.67 → 1.1.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,12 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.globalValidatorMiddleware = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
4
9
|
const express_validator_1 = require("express-validator");
|
|
5
10
|
const validation_error_1 = require("../errors/validation-error");
|
|
11
|
+
const loadLanguageFile = (lang) => {
|
|
12
|
+
console.log(lang);
|
|
13
|
+
try {
|
|
14
|
+
const filePath = path_1.default.join(__dirname, `../languages/${lang}.json`);
|
|
15
|
+
const data = fs_1.default.readFileSync(filePath, 'utf8');
|
|
16
|
+
return JSON.parse(data);
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
console.error(`Error loading language file for ${lang}:`, error);
|
|
20
|
+
// Default to English if there's an error loading the requested language
|
|
21
|
+
const defaultPath = path_1.default.join(__dirname, '../languages/en.json');
|
|
22
|
+
const data = fs_1.default.readFileSync(defaultPath, 'utf8');
|
|
23
|
+
return JSON.parse(data);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
6
26
|
const globalValidatorMiddleware = (req, res, next) => {
|
|
7
27
|
const errors = (0, express_validator_1.validationResult)(req);
|
|
8
|
-
if (!errors.isEmpty())
|
|
9
|
-
|
|
28
|
+
if (!errors.isEmpty()) {
|
|
29
|
+
const lang = req.lang;
|
|
30
|
+
const language = loadLanguageFile(lang);
|
|
31
|
+
const translatedErrors = errors.array().map(error => (Object.assign(Object.assign({}, error), { msg: language[error.msg] || error.msg })));
|
|
32
|
+
return next(new validation_error_1.ValidationError(translatedErrors));
|
|
33
|
+
}
|
|
10
34
|
req.body = (0, express_validator_1.matchedData)(req, { locations: ['body'] });
|
|
11
35
|
req.params = (0, express_validator_1.matchedData)(req, { locations: ['params'] });
|
|
12
36
|
req.query = (0, express_validator_1.matchedData)(req, { locations: ['query'] });
|
|
@@ -12,10 +12,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.languageHeaderMiddleware = void 0;
|
|
13
13
|
const languageHeaderMiddleware = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
14
|
var _a;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
req.lang = ['ar', 'en'].includes(lang) ? lang : 'en';
|
|
18
|
-
}
|
|
15
|
+
const lang = ((_a = req.headers['lang']) === null || _a === void 0 ? void 0 : _a.toString()) || '';
|
|
16
|
+
req.lang = ['ar', 'en'].includes(lang) ? lang : 'en';
|
|
19
17
|
next();
|
|
20
18
|
});
|
|
21
19
|
exports.languageHeaderMiddleware = languageHeaderMiddleware;
|