Skip to content

Commit 9264ddb

Browse files
committed
9_08_js_i18n
1 parent 260bf37 commit 9264ddb

5 files changed

Lines changed: 37 additions & 14 deletions

File tree

‎config/messages/app.properties‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,11 @@ meal.calories=Calories
2626

2727
common.add=Add
2828
common.select=Select
29+
common.deleted=Record deleted
30+
common.saved=Record saved
31+
common.enabled=Record enabled
32+
common.disabled=Record disabled
33+
common.errorStatus=Error status
34+
common.confirm=Are you sure?
2935
common.save=Save
3036
common.cancel=Cancel

‎config/messages/app_ru.properties‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,11 @@ meal.calories=Калории
2626

2727
common.add=Добавить
2828
common.select=Выбрать
29+
common.deleted=Запись удалена
30+
common.saved=Запись сохранена
31+
common.enabled=Запись активирована
32+
common.disabled=Запись деактивирована
33+
common.errorStatus=Статус ошибки
34+
common.confirm=Вы уверены?
2935
common.save=Сохранить
3036
common.cancel=Отменить

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<div class="modal-dialog">
3838
<div class="modal-content">
3939
<div class="modal-header">
40-
<h4 class="modal-title"><spring:message code="user.add"/></h4>
40+
<h4 class="modal-title" id="modalTitle"></h4>
4141
<button type="button" class="close" data-dismiss="modal" onclick="closeNoty()">&times;</button>
4242
</div>
4343
<div class="modal-body">
@@ -78,4 +78,13 @@
7878
</div>
7979
<jsp:include page="fragments/footer.jsp"/>
8080
</body>
81+
<script type="text/javascript">
82+
const i18n = {}; // https://learn.javascript.ru/object
83+
i18n["addTitle"] = '<spring:message code="user.add"/>';
84+
i18n["editTitle"] = '<spring:message code="user.edit"/>';
85+
86+
<c:forEach var="key" items='${["common.deleted","common.saved","common.enabled","common.disabled","common.errorStatus","common.confirm"]}'>
87+
i18n["${key}"] = "<spring:message code="${key}"/>";
88+
</c:forEach>
89+
</script>
8190
</html>

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,30 @@ function makeEditable(datatableApi) {
1313
}
1414

1515
function add() {
16+
$("#modalTitle").html(i18n["addTitle"]);
1617
form.find(":input").val("");
1718
$("#editRow").modal();
1819
}
1920

2021
function updateRow(id) {
2122
form.find(":input").val("");
23+
$("#modalTitle").html(i18n["editTitle"]);
2224
$.get(ctx.ajaxUrl + id, function (data) {
2325
$.each(data, function (key, value) {
24-
form.find("input[name='" + key + "']").val(value);
26+
form.find(`input[name='${key}']`).val(value);
2527
});
2628
$('#editRow').modal();
2729
});
2830
}
2931

3032
function deleteRow(id) {
31-
if (confirm('Are you sure?')) {
33+
if (confirm(i18n['common.confirm'])) {
3234
$.ajax({
3335
url: ctx.ajaxUrl + id,
3436
type: "DELETE"
3537
}).done(function () {
3638
ctx.updateTable();
37-
successNoty("Deleted");
39+
successNoty("common.deleted");
3840
});
3941
}
4042
}
@@ -51,7 +53,7 @@ function save() {
5153
}).done(function () {
5254
$("#editRow").modal("hide");
5355
ctx.updateTable();
54-
successNoty("Saved");
56+
successNoty("common.saved");
5557
});
5658
}
5759

@@ -64,10 +66,10 @@ function closeNoty() {
6466
}
6567
}
6668

67-
function successNoty(text) {
69+
function successNoty(key) {
6870
closeNoty();
6971
new Noty({
70-
text: "<span class='fa fa-lg fa-check'></span> &nbsp;" + text,
72+
text: `<span class='fa fa-lg fa-check'></span> &nbsp;${i18n[key]}`,
7173
type: 'success',
7274
layout: "bottomRight",
7375
timeout: 1000
@@ -76,20 +78,20 @@ function successNoty(text) {
7678

7779
function renderEditBtn(data, type, row) {
7880
if (type === "display") {
79-
return "<a onclick='updateRow(" + row.id + ");'><span class='fa fa-pencil'></span></a>";
81+
return `<a onclick='updateRow(${row.id});'><span class='fa fa-pencil'></span></a>`;
8082
}
8183
}
8284

8385
function renderDeleteBtn(data, type, row) {
8486
if (type === "display") {
85-
return "<a onclick='deleteRow(" + row.id + ");'><span class='fa fa-remove'></span></a>";
87+
return `<a onclick='deleteRow(${row.id});'><span class='fa fa-remove'></span></a>`;
8688
}
8789
}
8890

8991
function failNoty(jqXHR) {
9092
closeNoty();
9193
failedNote = new Noty({
92-
text: "<span class='fa fa-lg fa-exclamation-circle'></span> &nbsp;Error status: " + jqXHR.status + (jqXHR.responseJSON ? "<br>" + jqXHR.responseJSON : ""),
94+
text: `<span class='fa fa-lg fa-exclamation-circle'></span> &nbsp;${i18n['common.errorStatus']}: ${jqXHR.status}${jqXHR.responseJSON ? '<br>' + jqXHR.responseJSON : ''}`,
9395
type: "error",
9496
layout: "bottomRight"
9597
});

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ function enable(chkbox, id) {
1414
$.ajax({
1515
url: userAjaxUrl + id,
1616
type: "POST",
17-
data: "enabled=" + enabled
17+
data: `enabled=${enabled}`
1818
}).done(function () {
1919
chkbox.closest("tr").attr("data-user-enabled", enabled);
20-
successNoty(enabled ? "Enabled" : "Disabled");
20+
successNoty(enabled ? "common.enabled" : "common.disabled");
2121
}).fail(function () {
2222
$(chkbox).prop("checked", !enabled);
2323
});
@@ -41,7 +41,7 @@ $(function () {
4141
"data": "email",
4242
"render": function (data, type, row) {
4343
if (type === "display") {
44-
return "<a href='mailto:" + data + "'>" + data + "</a>";
44+
return `<a href="mailto:${data}">${data}</a>`;
4545
}
4646
return data;
4747
}
@@ -53,7 +53,7 @@ $(function () {
5353
"data": "enabled",
5454
"render": function (data, type, row) {
5555
if (type === "display") {
56-
return "<input type='checkbox' " + (data ? "checked" : "") + " onclick='enable($(this)," + row.id + ");'/>";
56+
return `<input type='checkbox' ${data ? 'checked' : ''} onclick='enable($(this),${row.id});'/>`;
5757
}
5858
return data;
5959
}

0 commit comments

Comments
 (0)