Problem/Motivation
If field label contain any html entities or field description contain any html tags then i18n_field is unable to translate both label and description.
This problem exists because when core creates $element array in field_default_form or field_multiple_value_form
$element = array(
'#entity' => $entity,
'#entity_type' => $instance['entity_type'],
'#bundle' => $instance['bundle'],
'#field_name' => $field_name,
'#language' => $langcode,
'#field_parents' => $parents,
'#columns' => array_keys($field['columns']),
'#title' => check_plain($instance['label']),
'#description' => field_filter_xss($instance['description']),
// Only the first widget should be required.
'#required' => $delta == 0 && $instance['required'],
'#delta' => $delta,
);
And in i18n_field_field_widget_form_alter while replacing label and description translation
if (!empty($instance_current['label']) && $instance_current['label'] != $instance['label']) {
if (!empty($element['#title']) && $element['#title'] == $instance['label']) {
$element['#title'] = $instance_current['label'];
}
if (isset($element['value']) && !empty($element['value']['#title']) && $element['value']['#title'] == check_plain($instance['label'])) {
$element['value']['#title'] = $instance_current['label'];
}
}
.
.
.
if (!empty($instance_current['description']) && $instance_current['description'] != $instance['description']) {
if (!empty($element['#description']) && $element['#description'] == $instance['description']) {
$element['#description'] = $instance_current['description'];
}
if (isset($element['value']) && !empty($element['value']['#description']) && $element['value']['#description'] == field_filter_xss($instance['description'])) {
$element['value']['#description'] = $instance_current['description'];
}
}
Proposed resolution
As seen in core code while creating $element['#title']$instance['label'] is passed through check_plain
$element['#title'] = check_plain($instance['label']);
And for $element['#description']$instance['description'] is passed through field_filter_xss.
$element['#description'] = field_filter_xss($instance['description']);
i18n_field_field_widget_form_alter should do the same.
Remaining tasks
Create a patch.
- Verify changes.
- Review the patch
- Get the patch committed
User interface changes
none
API changes
none
#1157512: Labels are not translated with i18n_field for Drupal core.
Comments
Comment #1
bojanz commentedNeeds a rerolll since #1904368: [7.x-1.8 regression] Field translation fails in numerous situations landed.
Comment #2
jibranNew patch after #1904368: [7.x-1.8 regression] Field translation fails in numerous situations.
Comment #3
bojanz commentedLooks good. This approach was already tested in the other issue.
Comment #4
jose reyero commentedOk, committed. Thanks.
Comment #6
Carlos Miranda Levy commentedThis situation remains as of i18n 7.x-1.11.
At least for Entity Reference fields referencing User Profiles.
Seems related to https://www.drupal.org/node/1904368
Comment #7
idebr commentedThis page is ranked highly in Google for untranslatable field labels/descriptions with i18n.
Future visitors: this is the issue you are looking for: #2474403: Translation of field description overwritten