translatable error messages

This commit is contained in:
Yuri Kuznetsov
2022-03-30 12:59:13 +03:00
parent 461034f9cc
commit 00d04e0157
4 changed files with 168 additions and 43 deletions
@@ -0,0 +1,80 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2022 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\Exceptions\Error;
use Espo\Core\Utils\Json;
class Body
{
private ?string $messageTranslationLabel = null;
private ?string $messageTranslationScope = null;
/**
* @var ?array<string,string>
*/
private ?array $messageTranslationData = null;
public static function create(): self
{
return new self();
}
/**
* @param ?array<string,string> $data
*/
public function withMessageTranslation(string $label, ?string $scope = null, ?array $data = null): self
{
$obj = clone $this;
$obj->messageTranslationLabel = $label;
$obj->messageTranslationScope = $scope;
$obj->messageTranslationData = $data;
return $obj;
}
public function encode(): string
{
$data = (object) [];
if ($this->messageTranslationLabel) {
$messageTranslationData = (object) ($this->messageTranslationData ?? []);
$data->messageTranslation = (object) [
'label' => $this->messageTranslationLabel,
'scope' => $this->messageTranslationScope,
'data' => $messageTranslationData,
];
}
return Json::encode($data);
}
}
+13 -1
View File
@@ -32,6 +32,7 @@ namespace Espo\Core\Record;
use Espo\Core\ORM\Entity as CoreEntity;
use Espo\Core\Utils\Json;
use Espo\Core\Exceptions\Error\Body as ErrorBody;
use Espo\Core\Exceptions\{
Error,
@@ -1262,7 +1263,18 @@ class Service implements Crud,
$result = $this->getStreamService()->followEntity($entity, $foreignId);
if (!$result) {
throw new ForbiddenSilent("Could not add a user to followers. The user needs to have 'stream' access.");
throw ForbiddenSilent::createWithBody(
"Could not add user to followers.",
ErrorBody::create()
->withMessageTranslation(
'couldNotAddFollowerUserHasNoAccessToStream',
'Stream',
[
'userName' => $user->getUserName(),
]
)
->encode()
);
}
}
@@ -1,7 +1,8 @@
{
"messages": {
"infoMention": "Type **@username** to mention user in the post.",
"infoSyntax": "Available markdown syntax"
"infoSyntax": "Available markdown syntax",
"couldNotAddFollowerUserHasNoAccessToStream": "Could not add the user '{userName}' to the followers. The user does not have 'stream' access to the record."
},
"syntaxItems": {
"code": "code",
+73 -41
View File
@@ -815,17 +815,11 @@ define(
contentType: 'application/json',
});
$(document).ajaxError((event, xhr, options) => {
$(document).ajaxError((e, xhr, options) => {
if (xhr.errorIsHandled) {
return;
}
let statusReason = xhr.getResponseHeader('X-Status-Reason');
let msg;
let closeButton = false;
switch (xhr.status) {
case 0:
if (xhr.statusText === 'timeout') {
@@ -836,6 +830,7 @@ define(
case 200:
Ui.error(this.language.translate('Bad server response'));
console.error('Bad server response: ' + xhr.responseText);
break;
@@ -844,10 +839,11 @@ define(
if (!options.login) {
if (this.auth) {
this.logout();
break
}
else {
console.error('Error 401: Unauthorized.');
}
console.error('Error 401: Unauthorized.');
}
break;
@@ -859,28 +855,12 @@ define(
break;
}
msg = this.language.translate('Error') + ' ' + xhr.status + ': ' +
this.language.translate('Access denied');
if (statusReason) {
msg += '\n' + statusReason;
closeButton = true;
}
Ui.error(msg, closeButton);
this._processErrorAlert(xhr, 'Access denied');
break;
case 400:
msg = this.language.translate('Error') + ' ' + xhr.status + ': ' +
this.language.translate('Bad request');
if (statusReason) {
msg += ': ' + statusReason;
}
Ui.error(msg);
this._processErrorAlert(xhr, 'Bad request');
break;
@@ -891,30 +871,82 @@ define(
break
}
msg = this.language.translate('Error') + ' ' + xhr.status;
this._processErrorAlert(xhr, 'Not found', true);
msg += ': ' + this.language.translate('Not found');
Ui.error(msg);
break;
default:
msg = this.language.translate('Error') + ' ' + xhr.status;
if (statusReason) {
msg += '\n' + statusReason;
closeButton = true;
}
Ui.error(msg, closeButton);
this._processErrorAlert(xhr, null);
}
let statusReason = xhr.getResponseHeader('X-Status-Reason');
if (statusReason) {
console.error('Server side error ' + xhr.status + ': ' + statusReason);
}
});
},
_processErrorAlert: function (xhr, label, noDetail) {
let msg = this.language.translate('Error') + ' ' + xhr.status;
if (label) {
msg += ': ' + this.language.translate(label);
}
let obj = {
msg: msg,
closeButton: false,
};
let isMessageDone = false;
if (noDetail) {
isMessageDone = true;
}
if (!isMessageDone && xhr.responseText && xhr.responseText[0] === '{') {
let data = null;
try {
data = JSON.parse(xhr.responseText);
}
catch (e) {}
if (data && data.messageTranslation && data.messageTranslation.label) {
let msgDetail = this.language.translate(
data.messageTranslation.label,
'messages',
data.messageTranslation.scope
);
let msgData = data.messageTranslation.data || {};
for (let key in msgData) {
msgDetail = msgDetail.replace('{' + key + '}', msgData[key]);
}
obj.msg += '\n' + msgDetail;
obj.closeButton = true;
isMessageDone = true;
}
}
if (!isMessageDone) {
let statusReason = xhr.getResponseHeader('X-Status-Reason');
if (statusReason) {
obj.msg += '\n' + statusReason;
obj.closeButton = true;
isMessageDone = true;
}
}
Ui.error(obj.msg, obj.closeButton);
},
initBroadcastChannel: function () {
this.broadcastChannel = new BroadcastChannel();