Skip to content

Commit a4bf10a

Browse files
committed
fix: translate field label in error messages
1 parent 261e536 commit a4bf10a

20 files changed

+75
-55
lines changed

inc/abstractfield.class.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,15 @@ public function getTranslatableStrings(array $options = []) : array {
332332

333333
return $strings;
334334
}
335+
336+
/**
337+
* Translates the label of the field into the current language
338+
*
339+
* @return string
340+
*/
341+
protected function getTtranslatedLabel(): string {
342+
$form = PluginFormcreatorForm::getByItem($this->question);
343+
$domain = PluginFormcreatorForm::getTranslationDomain($form->getID());
344+
return __($this->getLabel(), $domain);
345+
}
335346
}

inc/field/actorfield.class.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public function isValid(): bool {
254254
// If the field is required it can't be empty
255255
if ($this->isRequired() && count($this->value) === 0) {
256256
Session::addMessageAfterRedirect(
257-
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
257+
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()),
258258
false,
259259
ERROR
260260
);
@@ -264,7 +264,7 @@ public function isValid(): bool {
264264
// If an item has been removed by sanitization, then the data is not valid
265265
if (count($sanitized) != count($this->value)) {
266266
Session::addMessageAfterRedirect(
267-
sprintf(__('Invalid value: %s', 'formcreator'), $this->getLabel()),
267+
sprintf(__('Invalid value: %s', 'formcreator'), $this->getTtranslatedLabel()),
268268
false,
269269
ERROR
270270
);
@@ -287,7 +287,7 @@ public function isValidValue($value): bool {
287287
$user = new User();
288288
if (!$user->getFromDB($item)) {
289289
Session::addMessageAfterRedirect(
290-
sprintf(__('User not found or invalid email address: %s', 'formcreator'), $this->getLabel()),
290+
sprintf(__('User not found or invalid email address: %s', 'formcreator'), $this->getTtranslatedLabel()),
291291
false,
292292
ERROR
293293
);

inc/field/checkboxesfield.class.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function isValid(): bool {
187187
// If the field is required it can't be empty
188188
if ($this->isRequired() && count($value) <= 0) {
189189
Session::addMessageAfterRedirect(
190-
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
190+
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()),
191191
false,
192192
ERROR
193193
);
@@ -223,14 +223,14 @@ public function isValidValue($value): bool {
223223
$rangeMin = $parameters['range']->fields['range_min'];
224224
$rangeMax = $parameters['range']->fields['range_max'];
225225
if ($rangeMin > 0 && count($value) < $rangeMin) {
226-
$message = sprintf(__('The following question needs at least %d answers', 'formcreator'), $rangeMin);
227-
Session::addMessageAfterRedirect($message . ' ' . $this->getLabel(), false, ERROR);
226+
$message = sprintf(__('The following question needs at least %d answers: %s', 'formcreator'), $rangeMin, $this->getTtranslatedLabel());
227+
Session::addMessageAfterRedirect($message, false, ERROR);
228228
return false;
229229
}
230230

231231
if ($rangeMax > 0 && count($value) > $rangeMax) {
232-
$message = sprintf(__('The following question does not accept more than %d answers', 'formcreator'), $rangeMax);
233-
Session::addMessageAfterRedirect($message . ' ' . $this->getLabel(), false, ERROR);
232+
$message = sprintf(__('The following question does not accept more than %d answers: %s', 'formcreator'), $rangeMax, $this->getTtranslatedLabel());
233+
Session::addMessageAfterRedirect($message, false, ERROR);
234234
return false;
235235
}
236236
}

inc/field/datefield.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function isValid(): bool {
112112
// If the field is required it can't be empty
113113
if ($this->isRequired() && (strtotime($this->value) == '')) {
114114
Session::addMessageAfterRedirect(
115-
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
115+
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()),
116116
false,
117117
ERROR
118118
);

inc/field/datetimefield.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function isValid(): bool {
115115
// If the field is required it can't be empty
116116
if ($this->isRequired() && (strtotime($this->value) == '')) {
117117
Session::addMessageAfterRedirect(
118-
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
118+
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()),
119119
false,
120120
ERROR
121121
);

inc/field/dropdownfield.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public function isValid(): bool {
438438
$dropdown = new $itemtype();
439439
if ($this->isRequired() && $dropdown->isNewId($this->value)) {
440440
Session::addMessageAfterRedirect(
441-
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
441+
__('A required field is empty:', 'formcreator') . ' ' . $this->getTtranslatedLabel(),
442442
false,
443443
ERROR
444444
);
@@ -460,7 +460,7 @@ public function isValidValue($value): bool {
460460

461461
if (!$isValid) {
462462
Session::addMessageAfterRedirect(
463-
__('Invalid value for ', 'formcreator') . ' ' . $this->getLabel(),
463+
__('Invalid value for ', 'formcreator') . ' ' . $this->getTtranslatedLabel(),
464464
false,
465465
ERROR
466466
);

inc/field/emailfield.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function isValidValue($value): bool {
8585

8686
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
8787
Session::addMessageAfterRedirect(
88-
sprintf(__('This is not a valid e-mail: %s', 'formcreator'), $this->getLabel()),
88+
sprintf(__('This is not a valid e-mail: %s', 'formcreator'), $this->getTtranslatedLabel()),
8989
false,
9090
ERROR
9191
);

inc/field/filefield.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public function isValid(): bool {
171171
$key = '_formcreator_field_' . $this->question->getID();
172172
if (($this->isRequired() && (!isset($this->uploads[$key]) || count($this->uploads[$key]) < 1))) {
173173
Session::addMessageAfterRedirect(
174-
sprintf(__('A required file is missing: %s', 'formcreator'), $this->getLabel()),
174+
sprintf(__('A required file is missing: %s', 'formcreator'), $this->getTtranslatedLabel()),
175175
false,
176176
ERROR
177177
);

inc/field/floatfield.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function getDocumentsForTarget(): array {
121121
public function isValid(): bool {
122122
if ($this->isRequired() && $this->value == '') {
123123
Session::addMessageAfterRedirect(
124-
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
124+
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getTtranslatedLabel()),
125125
false,
126126
ERROR
127127
);
@@ -138,7 +138,7 @@ public function isValidValue($value): bool {
138138

139139
if (!empty($value) && !is_numeric($value)) {
140140
Session::addMessageAfterRedirect(
141-
sprintf(__('This is not a number: %s', 'formcreator'), $this->getLabel()),
141+
sprintf(__('This is not a number: %s', 'formcreator'), $this->getTtranslatedLabel()),
142142
false,
143143
ERROR
144144
);

inc/field/integerfield.class.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function isValidValue($value): bool {
5656
}
5757

5858
if (!empty($value) && !ctype_digit((string) $value)) {
59-
Session::addMessageAfterRedirect(sprintf(__('This is not an integer: %s', 'formcreator'), $this->getLabel()), false, ERROR);
59+
Session::addMessageAfterRedirect(sprintf(__('This is not an integer: %s', 'formcreator'), $this->getTtranslatedLabel()), false, ERROR);
6060
return false;
6161
}
6262

@@ -68,7 +68,7 @@ public function isValidValue($value): bool {
6868
if ($regex !== null && strlen($regex) > 0) {
6969
if (!preg_match($regex, $value)) {
7070
Session::addMessageAfterRedirect(
71-
sprintf(__('Specific format does not match: %s', 'formcreator'), $this->getLabel()),
71+
sprintf(__('Specific format does not match: %s', 'formcreator'), $this->getTtranslatedLabel()),
7272
false,
7373
ERROR
7474
);
@@ -82,13 +82,13 @@ public function isValidValue($value): bool {
8282
$rangeMin = $parameters['range']->fields['range_min'];
8383
$rangeMax = $parameters['range']->fields['range_max'];
8484
if ($rangeMin > 0 && $value < $rangeMin) {
85-
$message = sprintf(__('The following number must be greater than %d: %s', 'formcreator'), $rangeMin, $this->getLabel());
85+
$message = sprintf(__('The following number must be greater than %d: %s', 'formcreator'), $rangeMin, $this->getTtranslatedLabel());
8686
Session::addMessageAfterRedirect($message, false, ERROR);
8787
return false;
8888
}
8989

9090
if ($rangeMax > 0 && $value > $rangeMax) {
91-
$message = sprintf(__('The following number must be lower than %d: %s', 'formcreator'), $rangeMax, $this->getLabel());
91+
$message = sprintf(__('The following number must be lower than %d: %s', 'formcreator'), $rangeMax, $this->getTtranslatedLabel());
9292
Session::addMessageAfterRedirect($message, false, ERROR);
9393
return false;
9494
}

0 commit comments

Comments
 (0)