In the baseFieldDefinitions
of Comment
Entity, the subject
field is set to be translatable, but I can not find such a translation in User interface translation
(admin/config/regional/translate) that can make it take effect when comment on a content.
$fields['subject'] = BaseFieldDefinition::create('string')
->setLabel(t('Subject'))
->setTranslatable(TRUE)
->setSetting('max_length', 64)
->setDisplayOptions('form', [
'type' => 'string_textfield',
// Default comment body field has weight 20.
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE);
I now use the code to translate it in my module:
function mymodule_preprocess_form_element(&$variables) {
if (\Drupal::routeMatch()->getRouteName() == 'entity.node.canonical') {
if($variables['label']['#title'] == 'Subject') $variables['label']['#title'] = t('Subject');
}
}
I don't think it's a good way since the translation is already defined.(And I've defined Hook_Comment_presave, this two function seems will effect each other,which I don’t want it happen either.)
Is there a better way to do that?