Extra Fields: Change extra field and extra field option's translation to look for camel case variable, then display text in po files then if nothing exist in translation then use original display text - refs BT#21533

pull/5378/head
NicoDucou 7 months ago
parent a15772b63c
commit b5f713621f
  1. 2
      public/main/inc/ajax/model.ajax.php
  2. 34
      public/main/inc/lib/extra_field.lib.php
  3. 8
      public/main/inc/lib/extra_field_option.lib.php

@ -2413,7 +2413,7 @@ switch ($action) {
get_lang('No') get_lang('No')
); );
foreach ($result as $item) { foreach ($result as $item) {
$item['display_text'] = $item['displayText']; $item['display_text'] = ExtraField::translateDisplayName($item['variable'], $item['displayText']);
$item['value_type'] = $obj->get_field_type_by_id($item['valueType']); $item['value_type'] = $obj->get_field_type_by_id($item['valueType']);
$item['changeable'] = $item['changeable'] ? $checkIcon : $timesIcon; $item['changeable'] = $item['changeable'] ? $checkIcon : $timesIcon;
$item['visible_to_self'] = $item['visibleToSelf'] ? $checkIcon : $timesIcon; $item['visible_to_self'] = $item['visibleToSelf'] ? $checkIcon : $timesIcon;

@ -639,7 +639,7 @@ class ExtraField extends Model
$extraFieldId = $row['id']; $extraFieldId = $row['id'];
/** @var \Chamilo\CoreBundle\Entity\ExtraField $extraField */ /** @var \Chamilo\CoreBundle\Entity\ExtraField $extraField */
$extraField = $extraFieldRepo->find($extraFieldId); $extraField = $extraFieldRepo->find($extraFieldId);
$row['display_text'] = $extraField->getDisplayText(); $row['display_text'] = ExtraField::translateDisplayName($row['variable'],$row['display_text']);
// All the tags of the field // All the tags of the field
$sql = "SELECT * FROM $this->table_field_tag $sql = "SELECT * FROM $this->table_field_tag
@ -794,6 +794,30 @@ class ExtraField extends Model
return $extra; return $extra;
} }
/**
* Translate the display text for a extra field.
*
* @param string $variable
* @param string $defaultDisplayText
*
* @return string
*/
public static function translateDisplayName($variable, $defaultDisplayText): string
{
// 1st priority variable.
$translatedVariable = get_lang(api_underscore_to_camel_case($variable));
if (api_underscore_to_camel_case($variable) !== $translatedVariable) {
return $translatedVariable;
}
// 2nd priority display text.
$translatedDisplayText = get_lang($defaultDisplayText);
if ($defaultDisplayText !== $translatedDisplayText) {
return $translatedDisplayText;
}
return $defaultDisplayText;
}
/** /**
* Return an array of all the extra fields available for this item. * Return an array of all the extra fields available for this item.
@ -924,7 +948,7 @@ class ExtraField extends Model
$extraFieldId = $extraField['id']; $extraFieldId = $extraField['id'];
/** @var \Chamilo\CoreBundle\Entity\ExtraField $field */ /** @var \Chamilo\CoreBundle\Entity\ExtraField $field */
$field = $extraFieldRepo->find($extraFieldId); $field = $extraFieldRepo->find($extraFieldId);
$extraField['display_text'] = $field->getDisplayText(); $extraField['display_text'] = self::translateDisplayName($extraField['variable'], $extraField['display_text']);
$extraField['options'] = $option->get_field_options_by_field( $extraField['options'] = $option->get_field_options_by_field(
$extraField['id'], $extraField['id'],
false, false,
@ -1114,7 +1138,7 @@ class ExtraField extends Model
//$translatedDisplayText = $field_details['display_text']; //$translatedDisplayText = $field_details['display_text'];
/** @var \Chamilo\CoreBundle\Entity\ExtraField $extraField */ /** @var \Chamilo\CoreBundle\Entity\ExtraField $extraField */
$extraField = $extraFieldRepo->find($field_details['id']); $extraField = $extraFieldRepo->find($field_details['id']);
$translatedDisplayText = get_lang($extraField->getDisplayText()); $translatedDisplayText = ExtraField::translateDisplayName($field_details['variable'],$field_details['display_text']);
$translatedDisplayHelpText = ''; $translatedDisplayHelpText = '';
if ($help) { if ($help) {
@ -2066,7 +2090,7 @@ class ExtraField extends Model
$extraFieldId = $row['id']; $extraFieldId = $row['id'];
/** @var \Chamilo\CoreBundle\Entity\ExtraField $extraField */ /** @var \Chamilo\CoreBundle\Entity\ExtraField $extraField */
$field = $extraFieldRepo->find($extraFieldId); $field = $extraFieldRepo->find($extraFieldId);
$row['display_text'] = $field->getDisplayText(); $row['display_text'] = $this->translateDisplayName($row['variable'], $row['display_text']);
// All the options of the field // All the options of the field
$sql = "SELECT * FROM $this->table_field_options $sql = "SELECT * FROM $this->table_field_options
@ -2495,7 +2519,7 @@ class ExtraField extends Model
$extraFieldRepo = Container::getExtraFieldRepository(); $extraFieldRepo = Container::getExtraFieldRepository();
/** @var \Chamilo\CoreBundle\Entity\ExtraField $extraField */ /** @var \Chamilo\CoreBundle\Entity\ExtraField $extraField */
$field = $extraFieldRepo->find($id); $field = $extraFieldRepo->find($id);
$info['display_text'] = $field->getDisplayText(); $info['display_text'] = ExtraField::translateDisplayName($info['variable'],$info['display_text']);
} }
return $info; return $info;

@ -549,7 +549,7 @@ class ExtraFieldOption extends Model
'id' => $row->getId(), 'id' => $row->getId(),
'field_id' => $row->getField()->getId(), 'field_id' => $row->getField()->getId(),
'option_value' => $row->getValue(), 'option_value' => $row->getValue(),
'display_text' => $row->getDisplayText(), 'display_text' => \ExtraField::translateDisplayName($row->getValue(), $row->getDisplayText()),
'priority' => $row->getPriority(), 'priority' => $row->getPriority(),
'priority_message' => $row->getPriorityMessage(), 'priority_message' => $row->getPriorityMessage(),
'option_order' => $row->getOptionOrder(), 'option_order' => $row->getOptionOrder(),
@ -591,7 +591,7 @@ class ExtraFieldOption extends Model
'id' => $subOption->getId(), 'id' => $subOption->getId(),
'field_id' => $subOption->getField()->getId(), 'field_id' => $subOption->getField()->getId(),
'option_value' => $subOption->getValue(), 'option_value' => $subOption->getValue(),
'display_text' => $subOption->getDisplayText(), 'display_text' => \ExtraField::translateDisplayName($subOption->getValue(), $subOption->getDisplayText()),
'priority' => $subOption->getPriority(), 'priority' => $subOption->getPriority(),
'priority_message' => $subOption->getPriorityMessage(), 'priority_message' => $subOption->getPriorityMessage(),
'option_order' => $subOption->getOptionOrder(), 'option_order' => $subOption->getOptionOrder(),
@ -862,7 +862,7 @@ class ExtraFieldOption extends Model
if ($info && $translateDisplayText) { if ($info && $translateDisplayText) {
$extraFieldOptionsRepo = Container::getExtraFieldOptionsRepository(); $extraFieldOptionsRepo = Container::getExtraFieldOptionsRepository();
$option = $extraFieldOptionsRepo->find($id); $option = $extraFieldOptionsRepo->find($id);
$info['display_text'] = $option->getDisplayText(); $info['display_text'] = \ExtraField::translateDisplayName($option->getValue(), $option->getDisplayText());
} }
return $info; return $info;
@ -875,7 +875,7 @@ class ExtraFieldOption extends Model
foreach ($result as &$row) { foreach ($result as &$row) {
$extraFieldOptionsRepo = Container::getExtraFieldOptionsRepository(); $extraFieldOptionsRepo = Container::getExtraFieldOptionsRepository();
$option = $extraFieldOptionsRepo->find($row['id']); $option = $extraFieldOptionsRepo->find($row['id']);
$row['display_text'] = $option->getDisplayText(); $row['display_text'] = \ExtraField::translateDisplayName($option->getValue(), $option->getDisplayText());
} }
return $result; return $result;

Loading…
Cancel
Save