Skip to content

Commit c2c9f1e

Browse files
authored
Move strings from js to data attributes so they become translatable. (#4813)
Fixes #4752
1 parent e009d08 commit c2c9f1e

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

hypha/apply/templates/forms/includes/field.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
data-toggle-on="{{ field.field.choices.0.0 }}"
99
data-toggle-off="{{ field.field.choices.1.0 }}"
1010
{% endif %}
11-
{% if widget_type == 'clearable_file_input' or widget_type == 'multi_file_input' or widget_type == 'single_file_field_widget' or widget_type == 'multi_file_field_widget' %}data-js-file-upload{% endif %}
11+
{% if widget_type == 'clearable_file_input' or widget_type == 'multi_file_input' or widget_type == 'single_file_field_widget' or widget_type == 'multi_file_field_widget' %}data-js-file-upload data-required-text="{% trans 'This field is required.' %}"{% endif %}
1212
{% if field.field.group_number > 1 %}
1313
data-hidden="{% if not show_all_group_fields and not field.field.visible %}true{% else %}false{% endif %}"
1414
data-required="{{ field.field.required_when_visible }}"
1515
{% endif %}
1616
{% if field.field.word_limit %}
1717
data-word-limit="{{ field.field.word_limit }}"
18+
data-word-count-characters="{% trans 'characters' %}"
19+
data-word-count-out-of="{% trans 'out of' %}"
20+
data-word-count-close="{% trans '(Close to the limit)' %}"
21+
data-word-count-over="{% trans '(Over the limit)' %}"
1822
{% endif %}
1923
{# fmt:off #}
2024
{% if widget_type == 'text_input' and field.field.max_length %}

hypha/static_src/javascript/file-uploads.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ htmx.onLoad(function () {
3939
input.addEventListener("invalid", function (event) {
4040
event.preventDefault(); // Prevent default browser behavior
4141

42-
let errorMessage = "This field is required.";
42+
const fileUploadEl = input.closest("[data-js-file-upload]");
43+
const errorMessage = fileUploadEl
44+
? fileUploadEl.dataset.requiredText
45+
: "This field is required.";
4346
// Find the closest dff-uploader wrapper and display an error
4447
let container = input.closest(".form__item");
4548

hypha/static_src/javascript/tinymce-word-count.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
*/
1010
function updateWordCount(element) {
1111
const currentCount = parseInt(element.innerText.match(/\d+/)?.[0], 10) || 0;
12-
const limit = parseInt(
13-
element.closest("[data-word-limit]").dataset.wordLimit,
14-
10
15-
);
12+
const fieldset = element.closest("[data-word-limit]");
13+
const limit = parseInt(fieldset.dataset.wordLimit, 10);
1614
const warningThreshold = limit * WARNING_THRESHOLD;
15+
const textCharacters = fieldset.dataset.wordCountCharacters || "characters";
16+
const textOutOf = fieldset.dataset.wordCountOutOf || "out of";
17+
const textClose = fieldset.dataset.wordCountClose || "(Close to the limit)";
18+
const textOver = fieldset.dataset.wordCountOver || "(Over the limit)";
1719

1820
/**
1921
* Clear warning states and classes
@@ -23,21 +25,21 @@
2325
element.classList.remove("word-count-warning", "word-count-warning-2");
2426
}
2527

26-
if (element.textContent.includes("characters")) {
28+
if (element.textContent.includes(textCharacters)) {
2729
clearWarnings();
2830
return;
2931
}
3032

31-
element.dataset.afterWordCount = ` out of ${limit}`;
33+
element.dataset.afterWordCount = ` ${textOutOf} ${limit}`;
3234

3335
if (currentCount <= warningThreshold) {
3436
clearWarnings();
3537
} else if (currentCount <= limit) {
36-
element.dataset.afterWordCount += " (Close to the limit)";
38+
element.dataset.afterWordCount += ` ${textClose}`;
3739
element.classList.remove("word-count-warning-2");
3840
element.classList.add("word-count-warning");
3941
} else {
40-
element.dataset.afterWordCount += " (Over the limit)";
42+
element.dataset.afterWordCount += ` ${textOver}`;
4143
element.classList.add("word-count-warning-2");
4244
}
4345
}

0 commit comments

Comments
 (0)