Skip to content

Commit 1bf4ad8

Browse files
committed
11_08_i18n
1 parent 365f982 commit 1bf4ad8

10 files changed

Lines changed: 81 additions & 17 deletions

File tree

‎config/messages/app.properties‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ user.registered=Registered
2121
user.password=Password
2222
user.caloriesPerDay=Daily calorie limit
2323
24+
userTo.name=Name
25+
userTo.email=Email
26+
userTo.password=Password
27+
userTo.caloriesPerDay=Daily calorie limit
28+
2429
meal.title=Meals
2530
meal.edit=Edit meal
2631
meal.add=Add meal
@@ -39,11 +44,23 @@ common.deleted=Record deleted
3944
common.saved=Record saved
4045
common.enabled=Record enabled
4146
common.disabled=Record disabled
42-
common.errorStatus=Error status
43-
common.appError=Application error
4447
common.confirm=Are you sure?
4548
common.save=Save
4649
common.cancel=Cancel
50+
common.search=Search
4751
4852
exception.user.duplicateEmail=User with this email already exists
49-
exception.meal.duplicateDateTime=You already have meal with this date/time
53+
exception.meal.duplicateDateTime=You already have meal with this date/time
54+
55+
error.appError=Application error
56+
error.dataNotFound=Data not found
57+
error.dataError=Data error
58+
error.validationError=Validation error
59+
60+
NotEmpty=[{0}] must not be empty
61+
NotBlank=[{0}] must not be empty
62+
NotNull=[{0}] must not be empty
63+
Email= Invalid format of [{0}]
64+
Range=[{0}] must be between {2} and {1}
65+
Length=[{0}] length must be between {2} and {1}
66+
Size=[{0}] size must be between {2} and {1}

‎config/messages/app_ru.properties‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ user.registered=Зарегистрирован
2121
user.password=Пароль
2222
user.caloriesPerDay=Норма калорий в день
2323

24+
userTo.name=Имя
25+
userTo.email=Почта
26+
userTo.password=Пароль
27+
userTo.caloriesPerDay=Норма калорий в день
28+
2429
meal.title=Моя еда
2530
meal.edit=Редактировать еду
2631
meal.add=Добавить еду
@@ -39,11 +44,23 @@ common.deleted=Запись удалена
3944
common.saved=Запись сохранена
4045
common.enabled=Запись активирована
4146
common.disabled=Запись деактивирована
42-
common.errorStatus=Статус ошибки
43-
common.appError=Ошибка приложения
4447
common.confirm=Вы уверены?
4548
common.save=Сохранить
4649
common.cancel=Отменить
50+
common.search=Искать
4751

4852
exception.user.duplicateEmail=Пользователь с такой почтой уже есть в приложении
49-
exception.meal.duplicateDateTime=У вас уже есть еда с такой датой/временем
53+
exception.meal.duplicateDateTime=У вас уже есть еда с такой датой/временем
54+
55+
error.appError=Ошибка приложения
56+
error.dataNotFound=Данные не найдены
57+
error.dataError=Ошибка в данных
58+
error.validationError=Ошибка проверки данных
59+
60+
NotEmpty=[{0}] не должно быть пустым
61+
NotBlank=[{0}] не должно быть пустым
62+
NotNull=[{0}] не должно быть пустым
63+
Email=Неверный формат [{0}]
64+
Range= [{0}] должно быть между {2} и {1}
65+
Length=Длинна [{0}] должна быть между {2} и {1}
66+
Size=Размер [{0}] должен быть между {2} и {1}

‎src/main/java/ru/javawebinar/topjava/util/exception/ErrorInfo.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
public class ErrorInfo {
44
private final String url;
55
private final ErrorType type;
6+
private final String typeMessage;
67
private final String[] details;
78

8-
public ErrorInfo(CharSequence url, ErrorType type, String... details) {
9+
public ErrorInfo(CharSequence url, ErrorType type, String typeMessage, String... details) {
910
this.url = url.toString();
1011
this.type = type;
12+
this.typeMessage = typeMessage;
1113
this.details = details;
1214
}
1315
}
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
package ru.javawebinar.topjava.util.exception;
22

33
public enum ErrorType {
4-
APP_ERROR,
5-
DATA_NOT_FOUND,
6-
DATA_ERROR,
7-
VALIDATION_ERROR
4+
APP_ERROR("error.appError"),
5+
DATA_NOT_FOUND("error.dataNotFound"),
6+
DATA_ERROR("error.dataError"),
7+
VALIDATION_ERROR("error.validationError");
8+
9+
private final String errorCode;
10+
11+
ErrorType(String errorCode) {
12+
this.errorCode = errorCode;
13+
}
14+
15+
public String getErrorCode() {
16+
return errorCode;
17+
}
818
}

‎src/main/java/ru/javawebinar/topjava/web/ExceptionInfoHandler.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ public ErrorInfo internalError(HttpServletRequest req, Exception e) {
8888
}
8989

9090
// https://stackoverflow.com/questions/538870/should-private-helper-methods-be-static-if-they-can-be-static
91-
private static ErrorInfo logAndGetErrorInfo(HttpServletRequest req, Exception e, boolean logStackTrace, ErrorType errorType, String... details) {
91+
private ErrorInfo logAndGetErrorInfo(HttpServletRequest req, Exception e, boolean logStackTrace, ErrorType errorType, String... details) {
9292
Throwable rootCause = ValidationUtil.logAndGetRootCause(log, req, e, logStackTrace, errorType);
9393
return new ErrorInfo(req.getRequestURL(), errorType,
94+
messageSourceAccessor.getMessage(errorType.getErrorCode()),
9495
details.length != 0 ? details : new String[]{ValidationUtil.getMessage(rootCause)});
9596
}
9697
}

‎src/main/java/ru/javawebinar/topjava/web/GlobalExceptionHandler.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5+
import org.springframework.context.support.MessageSourceAccessor;
56
import org.springframework.http.HttpStatus;
67
import org.springframework.web.bind.annotation.ControllerAdvice;
78
import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -17,14 +18,22 @@
1718
public class GlobalExceptionHandler {
1819
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
1920

21+
private final MessageSourceAccessor messageSourceAccessor;
22+
23+
public GlobalExceptionHandler(MessageSourceAccessor messageSourceAccessor) {
24+
this.messageSourceAccessor = messageSourceAccessor;
25+
}
26+
2027
@ExceptionHandler(Exception.class)
2128
public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
2229
log.error("Exception at request " + req.getRequestURL(), e);
2330
Throwable rootCause = ValidationUtil.logAndGetRootCause(log, req, e, true, ErrorType.APP_ERROR);
2431

2532
HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
2633
ModelAndView mav = new ModelAndView("exception",
27-
Map.of("exception", rootCause, "message", ValidationUtil.getMessage(rootCause), "status", httpStatus));
34+
Map.of("exception", rootCause, "message", ValidationUtil.getMessage(rootCause),
35+
"typeMessage", messageSourceAccessor.getMessage(ErrorType.APP_ERROR.getErrorCode()),
36+
"status", httpStatus));
2837
mav.setStatus(httpStatus);
2938

3039
// Interceptor is not invoked, put userTo

‎src/main/webapp/WEB-INF/jsp/exception.jsp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<div class="container text-center">
1313
<br>
1414
<h4 class="my-3">${status}</h4>
15-
<h2><spring:message code="common.appError"/></h2>
15+
<h2>${typeMessage}</h2>
1616
<h4 class="my-5">${message}</h4>
1717
</div>
1818
</div>

‎src/main/webapp/WEB-INF/jsp/fragments/i18n.jsp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
i18n["addTitle"] = '<spring:message code="${param.page}.add"/>';
99
i18n["editTitle"] = '<spring:message code="${param.page}.edit"/>';
1010
11-
<c:forEach var='key' items='${["common.deleted", "common.saved", "common.enabled", "common.disabled", "common.errorStatus", "common.confirm"]}'>
11+
<c:forEach var='key' items='${["common.deleted", "common.saved", "common.enabled", "common.disabled", "common.search", "common.confirm"]}'>
1212
i18n['${key}'] = '<spring:message code="${key}"/>';
1313
</c:forEach>
1414
</script>

‎src/main/webapp/resources/css/style.css‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ tr[data-user-enabled="false"] {
3232
border: 1px solid #9feba6;
3333
}
3434

35+
/*https://stackoverflow.com/a/53855189/548473*/
36+
#noty_layout__bottomRight {
37+
width: 385px !important;
38+
}
39+
3540
/*https://getbootstrap.com/docs/4.0/examples/sticky-footer/sticky-footer.css*/
3641
html {
3742
position: relative;

‎src/main/webapp/resources/js/topjava.common.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ function makeEditable(datatableOpts) {
99
"dataSrc": ""
1010
},
1111
"paging": false,
12-
"info": true
12+
"info": true,
13+
"language": {
14+
"search": i18n["common.search"]
15+
}
1316
}
1417
);
1518
form = $('#detailsForm');
@@ -108,7 +111,7 @@ function failNoty(jqXHR) {
108111
closeNoty();
109112
var errorInfo = jqXHR.responseJSON;
110113
failedNote = new Noty({
111-
text: `<span class='fa fa-lg fa-exclamation-circle'></span> &nbsp;${i18n['common.errorStatus']}: ${jqXHR.status}<br>${errorInfo.type}<br>${errorInfo.details.join("<br>")}`,
114+
text: `<span class='fa fa-lg fa-exclamation-circle'></span> &nbsp;${errorInfo.typeMessage}<br>${errorInfo.details.join("<br>")}`,
112115
type: "error",
113116
layout: "bottomRight"
114117
});

0 commit comments

Comments
 (0)