fix: translate api error messages to user lang, closes #10623,

isekai-main
Barış Soner Uşaklı 3 years ago
parent a4784ed357
commit b17a81bfb0

@ -63,8 +63,7 @@ define('forum/topic/votes', [
delta: delta,
}, function (err) {
if (err) {
// TODO: err.message is currently hardcoded in helpers/api.js
if (err.message === 'A valid login session was not found. Please log in and try again.') {
if (!app.user.uid) {
ajaxify.go('login');
return;
}

@ -460,7 +460,7 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {
Object.assign(response, { params });
}
const returnPayload = await helpers.generateError(statusCode, message);
const returnPayload = await helpers.generateError(statusCode, message, res);
returnPayload.response = response;
if (global.env === 'development') {
@ -471,7 +471,7 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {
res.status(statusCode).json(returnPayload);
} else if (!payload) {
// Non-2xx statusCode, generate predefined error
const returnPayload = await helpers.generateError(statusCode);
const returnPayload = await helpers.generateError(statusCode, null, res);
res.status(statusCode).json(returnPayload);
}
};
@ -495,15 +495,21 @@ async function generateBannedResponse(res) {
return response;
}
helpers.generateError = async (statusCode, message) => {
helpers.generateError = async (statusCode, message, res) => {
async function translateMessage(message) {
const { req } = res;
const settings = req.query.lang ? null : await user.getSettings(req.uid);
const language = String(req.query.lang || settings.userLang || meta.config.defaultLang);
return await translator.translate(message, language);
}
if (message && message.startsWith('[[')) {
message = await translator.translate(message);
message = await translateMessage(message);
}
const payload = {
status: {
code: 'internal-server-error',
message: message || await translator.translate(`[[error:api.${statusCode}]]`),
message: message || await translateMessage(`[[error:api.${statusCode}]]`),
},
response: {},
};

Loading…
Cancel
Save