Skip to content

Commit

Permalink
Fix i18n in js strings
Browse files Browse the repository at this point in the history
  • Loading branch information
int-y1 authored and hieplpvip committed Sep 27, 2024
1 parent 1b8e08b commit b863ed7
Show file tree
Hide file tree
Showing 35 changed files with 129 additions and 122 deletions.
9 changes: 8 additions & 1 deletion judge/jinja2/format.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.utils.html import format_html
import html

from django.utils.html import escapejs, format_html
from django.utils.safestring import mark_safe

from . import registry
Expand All @@ -12,3 +14,8 @@ def bold(text):
@registry.function
def safe(text):
return mark_safe(text)


@registry.filter
def htmltojs(text):
return format_html("'{0}'", escapejs(html.unescape(text)))
6 changes: 3 additions & 3 deletions templates/admin/judge/contest/change_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
django.jQuery(function ($) {
$('.rerate-link').appendTo('div#bottombar').show();
$('.rejudge-link').click(function () {
return confirm('{{ _('Are you sure you want to rejudge ALL the submissions?') }}');
return confirm({{ _('Are you sure you want to rejudge ALL the submissions?')|htmltojs }});
});
$('.rescore-link').click(function () {
return confirm('{{ _('Are you sure you want to rescore ALL the submissions?') }}');
return confirm({{ _('Are you sure you want to rescore ALL the submissions?')|htmltojs }});
});
$('.resend-link').click(function () {
return confirm('{{ _('Are you sure you want to resend this announcement?') }}');
return confirm({{ _('Are you sure you want to resend this announcement?')|htmltojs }});
});
});
</script>
Expand Down
4 changes: 2 additions & 2 deletions templates/blog/blog-post.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<a href="javascript:blog_upvote({{ post.id }})"
class="upvote-link fa fa-chevron-up fa-fw{% if post.vote_score == 1 %} voted{% endif %}"></a>
{% else %}
<a href="javascript:alert('{{ _('Please log in to vote')|escapejs }}')" title="{{ _('Please log in to vote') }}"
<a href="javascript:alert({{ _('Please log in to vote')|htmltojs }})" title="{{ _('Please log in to vote') }}"
class="upvote-link fa fa-chevron-up fa-fw"></a>
{% endif %}
<br>
Expand All @@ -16,7 +16,7 @@
<a href="javascript:blog_downvote({{ post.id }})"
class="downvote-link fa fa-chevron-down fa-fw{% if post.vote_score == -1 %} voted{% endif %}"></a>
{% else %}
<a href="javascript:alert('{{ _('Please log in to vote')|escapejs }}')" title="{{ _('Please log in to vote') }}"
<a href="javascript:alert({{ _('Please log in to vote')|htmltojs }})" title="{{ _('Please log in to vote') }}"
class="downvote-link fa fa-chevron-down fa-fw"></a>
{% endif %}
</div>
Expand Down
4 changes: 2 additions & 2 deletions templates/blog/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<a href="javascript:blog_upvote({{ post.id }})"
class="upvote-link fa fa-chevron-up fa-fw{% if post.vote_score == 1 %} voted{% endif %}"></a>
{% else %}
<a href="javascript:alert('{{ _('Please log in to vote')|escapejs }}')" title="{{ _('Please log in to vote') }}"
<a href="javascript:alert({{ _('Please log in to vote')|htmltojs }})" title="{{ _('Please log in to vote') }}"
class="upvote-link fa fa-chevron-up fa-fw"></a>
{% endif %}
<br>
Expand All @@ -28,7 +28,7 @@
<a href="javascript:blog_downvote({{ post.id }})"
class="downvote-link fa fa-chevron-down fa-fw{% if post.vote_score == -1 %} voted{% endif %}"></a>
{% else %}
<a href="javascript:alert('{{ _('Please log in to vote')|escapejs }}')" title="{{ _('Please log in to vote') }}"
<a href="javascript:alert({{ _('Please log in to vote')|htmltojs }})" title="{{ _('Please log in to vote') }}"
class="downvote-link fa fa-chevron-down fa-fw"></a>
{% endif %}
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/blog/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

$(function () {
$('#delete-button').click(function () {
return confirm('{{ _('Are you sure you want to delete this blog post?') }}');
return confirm({{ _('Are you sure you want to delete this blog post?')|htmltojs }});
});
});
</script>
Expand Down
20 changes: 10 additions & 10 deletions templates/comments/base-media-js.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
var reply_id = 'reply-' + parent;
if ($comment_reply.find('#' + reply_id).length == 0) {
var $reply_form = $('#new-comment').clone(false).prop('id', reply_id);
$reply_form.find('h3').html('{{ _('Replying to comment') }}');
$reply_form.find('h3').html({{ _('Replying to comment')|htmltojs }});
$reply_form.prepend('<a class="close">x</a>');
$reply_form.appendTo($comment_reply);
$reply_form.find('form.comment-submit-form input#id_parent').val(parent);
Expand Down Expand Up @@ -83,12 +83,12 @@
$comment.find('.next-revision').css({visibility: show_revision == max_revision ? 'hidden' : ''});
var $content = $comment.find('.content').html(body);

var edit_text = '{{ _('edit {edits}') }}'.replace("{edits}", show_revision);
var edit_text = {{ _('edit {edits}')|htmltojs }}.replace("{edits}", show_revision);

if (show_revision == 0) {
edit_text = '{{ _('original') }}';
edit_text = {{ _('original')|htmltojs }};
} else if (show_revision == max_revision && max_revision == 1) {
edit_text = '{{ _('edited') }}';
edit_text = {{ _('edited')|htmltojs }};
}

$comment.find('.comment-edit-text').text(' ' + edit_text + ' ');
Expand All @@ -112,7 +112,7 @@
on_success();
},
error: function (data, textStatus, jqXHR) {
alert('{{ _('Could not vote: {error}') }}'.replace('{error}', data.responseText));
alert({{ _('Could not vote: {error}')|htmltojs }}.replace('{error}', data.responseText));
}
});
}
Expand Down Expand Up @@ -148,15 +148,15 @@
var $comments = $('.comments');
$comments.find('a.hide-comment').click(function (e) {
e.preventDefault();
if (!(e.ctrlKey || e.metaKey || confirm('{{ _('Are you sure you want to hide this comment?') }}')))
if (!(e.ctrlKey || e.metaKey || confirm({{ _('Are you sure you want to hide this comment?')|htmltojs }})))
return;

var id = $(this).attr('data-id');
$.post('{{ url('comment_hide') }}', {id: id}).then(function () {
$('#comment-' + id).remove();
$('#comment-' + id + '-children').remove();
}).catch(function () {
alert('{{ _('Could not hide comment.') }}');
alert({{ _('Could not hide comment.')|htmltojs }});
});
});

Expand All @@ -180,12 +180,12 @@
if (window.add_code_copy_buttons)
window.add_code_copy_buttons($area);
var $edits = $comment.find('.comment-edits').first();
$edits.text('{{ _('updated') }}');
$edits.text({{ _('updated')|htmltojs }});
}).fail(function () {
alert('{{ _('Failed to update comment body.') }}');
alert({{ _('Failed to update comment body.')|htmltojs }});
});
}).fail(function (data) {
alert('{{ _('Could not edit comment: {error}') }}'.replace('{error}', data.responseText));
alert({{ _('Could not edit comment: {error}')|htmltojs }}.replace('{error}', data.responseText));
});
});
},
Expand Down
4 changes: 2 additions & 2 deletions templates/comments/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h2><i style="padding-right: 0.3em" class="fa fa-comments"></i>{{ _('Comments')
<a href="javascript:comment_upvote({{ node.id }})"
class="upvote-link fa fa-chevron-up fa-fw{% if node.vote_score == 1 %} voted{% endif %}"></a>
{% else %}
<a href="javascript:alert('{{ _('Please log in to vote')|escapejs }}')" title="{{ _('Please log in to vote') }}"
<a href="javascript:alert({{ _('Please log in to vote')|htmltojs }})" title="{{ _('Please log in to vote') }}"
class="upvote-link fa fa-chevron-up fa-fw"></a>
{% endif %}
<br>
Expand All @@ -42,7 +42,7 @@ <h2><i style="padding-right: 0.3em" class="fa fa-comments"></i>{{ _('Comments')
<a href="javascript:comment_downvote({{ node.id }})"
class="downvote-link fa fa-chevron-down fa-fw{% if node.vote_score == -1 %} voted{% endif %}"></a>
{% else %}
<a href="javascript:alert('{{ _('Please log in to vote')|escapejs }}')" title="{{ _('Please log in to vote') }}"
<a href="javascript:alert({{ _('Please log in to vote')|htmltojs }})" title="{{ _('Please log in to vote') }}"
class="downvote-link fa fa-chevron-down fa-fw"></a>
{% endif %}
</div>
Expand Down
6 changes: 3 additions & 3 deletions templates/common-content.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
.append(copyButton = $('<span>', {
'class': 'btn-clipboard',
'data-clipboard-text': $(this).text(),
'title': '{{ _('Click to copy') }}'
}).text('{{ _('Copy') }}')));
'title': {{ _('Click to copy')|htmltojs }}
}).text({{ _('Copy')|htmltojs }})));

$(copyButton.get(0)).mouseleave(function () {
$(this).attr('class', 'btn-clipboard');
Expand All @@ -41,7 +41,7 @@

curClipboard.on('success', function (e) {
e.clearSelection();
showTooltip(e.trigger, '{{ _('Copied!') }}');
showTooltip(e.trigger, {{ _('Copied!')|htmltojs }});
});

curClipboard.on('error', function (e) {
Expand Down
2 changes: 1 addition & 1 deletion templates/contest/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
});

var noResults = function () {
return '{{ _('Press Enter to select multiple users...') }}';
return {{ _('Press Enter to select multiple users...')|htmltojs }};
};

$(document).one('click', '#id_private_contestants + .select2', function (e) {
Expand Down
12 changes: 6 additions & 6 deletions templates/contest/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@

{% if not request.official_contest_mode %}
$('.join-warning').click(function () {
return confirm('{{ _('Are you sure you want to join?') }}\n' +
'{{ _('Joining a contest for the first time starts your timer, after which it becomes unstoppable.') }}'
return confirm({{ _('Are you sure you want to join?')|htmltojs }} + '\n' +
{{ _('Joining a contest for the first time starts your timer, after which it becomes unstoppable.')|htmltojs }}
{% if request.in_contest %}
+ '\n{{ _('Joining this contest will leave %(contest)s.', contest=request.participation.contest.name) }}'
+ '\n' + {{ _('Joining this contest will leave %(contest)s.', contest=request.participation.contest.name)|htmltojs }}
{% endif %}
);
});
{% endif %}

{% if request.in_contest %}
$('.spectate-warning').click(function () {
return confirm('{{ _('Are you sure you want to spectate?') }}\n' +
'{{ _('Spectating this contest will leave %(contest)s.', contest=request.participation.contest.name) }}');
return confirm({{ _('Are you sure you want to spectate?')|htmltojs }} + '\n' +
{{ _('Spectating this contest will leave %(contest)s.', contest=request.participation.contest.name)|htmltojs }});
});
{% endif %}

$('.register-warning').click(function () {
return confirm('{{ _('Are you sure you want to register?') }}');
return confirm({{ _('Are you sure you want to register?')|htmltojs }});
});

{% if hide_private_contests is not none %}
Expand Down
14 changes: 7 additions & 7 deletions templates/contest/media-js.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<script type="text/javascript">
$(function () {
$('.leaving-forever').click(function () {
return confirm('{{ _('Are you sure you want to leave?') }}\n' +
'{{ _('You cannot come back to a virtual participation. You will have to start a new one.') }}');
return confirm({{ _('Are you sure you want to leave?')|htmltojs }} + '\n' +
{{ _('You cannot come back to a virtual participation. You will have to start a new one.')|htmltojs }});
});

{% if not request.official_contest_mode %}
$('.first-join').click(function () {
return confirm('{{ _('Are you sure you want to join?') }}\n' +
'{{ _('Joining a contest starts your timer, after which it becomes unstoppable.') }}');
return confirm({{ _('Are you sure you want to join?')|htmltojs }} + '\n' +
{{ _('Joining a contest starts your timer, after which it becomes unstoppable.')|htmltojs }});
});
{% endif %}

{% if request.in_contest %}
$('.contest-join').click(function () {
return confirm('{{ _('Are you sure you want to join?') }}\n' +
'{{ _('Joining this contest will leave %(contest)s.', contest=request.participation.contest.name) }}');
return confirm({{ _('Are you sure you want to join?')|htmltojs }} + '\n' +
{{ _('Joining this contest will leave %(contest)s.', contest=request.participation.contest.name)|htmltojs }});
});
{% endif %}

$('.register-warning').click(function () {
return confirm('{{ _('Are you sure you want to register?') }}');
return confirm({{ _('Are you sure you want to register?')|htmltojs }});
});
});
</script>
4 changes: 2 additions & 2 deletions templates/contest/moss.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
<script type="text/javascript">
$(function () {
$('.contest-moss').click(function () {
return confirm('{{ _('Are you sure you want to MOSS the contest?') }}');
return confirm({{ _('Are you sure you want to MOSS the contest?')|htmltojs }});
});
$('.contest-moss-delete').click(function () {
return confirm('{{ _('Are you sure you want to delete the MOSS results?') }}');
return confirm({{ _('Are you sure you want to delete the MOSS results?')|htmltojs }});
});
});
</script>
Expand Down
2 changes: 1 addition & 1 deletion templates/contest/prepare-data.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
check($(this), 300);
});
$('#prepare-download').click(function() {
return confirm("{{ _('Are you sure you want to prepare a download?') }}");
return confirm({{ _('Are you sure you want to prepare a download?')|htmltojs }});
});
});
$(document).ready(function () {
Expand Down
10 changes: 5 additions & 5 deletions templates/contest/ranking.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
theme: '{{ DMOJ_SELECT2_THEME }}',
multiple: true,
closeOnSelect: false,
placeholder: '{{ _('Search organizations') }}',
placeholder: {{ _('Search organizations')|htmltojs }},
});

var selection = $('#org-check-list').data().select2.selection;
Expand Down Expand Up @@ -368,12 +368,12 @@
window.enableAdminOperations = function () {
$('a.disqualify-participation').click(function (e) {
e.preventDefault();
if (e.ctrlKey || e.metaKey || confirm("{{ _('Are you sure you want to disqualify this participation?') }}"))
if (e.ctrlKey || e.metaKey || confirm({{ _('Are you sure you want to disqualify this participation?')|htmltojs }}))
$(this).closest('form').submit();
})
$('a.un-disqualify-participation').click(function (e) {
e.preventDefault();
if (e.ctrlKey || e.metaKey || confirm("{{ _('Are you sure you want to un-disqualify this participation?') }}"))
if (e.ctrlKey || e.metaKey || confirm({{ _('Are you sure you want to un-disqualify this participation?')|htmltojs }}))
$(this).closest('form').submit();
})
};
Expand Down Expand Up @@ -411,8 +411,8 @@
header.push(clean_text($col.text()));
} else if ($col.hasClass('username')) {
// Username and Full name
header.push(clean_text('{{ _('Username') }}'));
header.push(clean_text('{{ _('Full Name') }}'));
header.push(clean_text({{ _('Username')|htmltojs }}));
header.push(clean_text({{ _('Full Name')|htmltojs }}));
} else {
// Point
var name = $col.find('.problem-code').text();
Expand Down
2 changes: 1 addition & 1 deletion templates/leave-warning.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
$(function() {
$('form').dirty({
preventLeaving: true,
leavingMessage: '{{ _('Changes you made may not be saved.') }}',
leavingMessage: {{ _('Changes you made may not be saved.')|htmltojs }},
});
});
</script>
6 changes: 3 additions & 3 deletions templates/organization/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
<script type="text/javascript">
$(function () {
$('.leave-organization').click(function () {
return confirm('{{ _('Are you sure you want to leave this organization?') }}\n' +
return confirm({{ _('Are you sure you want to leave this organization?')|htmltojs }} + '\n' +
{% if organization.is_open %}
'{{ _('You will have to rejoin to show up on the organization leaderboard.') }}'
{{ _('You will have to rejoin to show up on the organization leaderboard.')|htmltojs }}
{% else %}
'{{ _('You will have to request membership in order to join again.') }}'
{{ _('You will have to request membership in order to join again.')|htmltojs }}
{% endif %}
);
});
Expand Down
2 changes: 1 addition & 1 deletion templates/organization/users.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<script type="text/javascript">
$(function () {
$('form.kick-form').find('a.button').click(function () {
if (confirm("{{ _('Are you sure you want to kick this user?') }}")) {
if (confirm({{ _('Are you sure you want to kick this user?')|htmltojs }})) {
$(this).parent().submit();
}
return false;
Expand Down
Loading

0 comments on commit b863ed7

Please sign in to comment.