1

I am working drupal 8 site. In this site I create a contact form for contact page. I am able to translate field label but I want to translate my placeholder.I can't find any option for translate contact forms placehoder in drupal 8.

5

1 Answer 1

2

Normally you should be able to translate the placeholder in configuration translation as described in https://drupal.stackexchange.com/a/191927/47547. @Berdir pointed out that there is an open issue that prevents this: https://www.drupal.org/node/2546212

A workaround is to make the placeholder translatable in a form alter hook:

function mytheme_form_contact_message_feedback_form_alter(&$form, $form_state) {
  $form['subject']['widget']['0']['value']['#placeholder'] = t('Please enter a subject');
}

After visiting the translated form you can translate this string in the user interface translation.

If you want to translate all placeholders in the form and use the untranslated strings from the widget settings:

function mytheme_form_contact_message_feedback_form_alter(&$form, $form_state) {
  array_walk_recursive($form, function (&$value, $key) {
    if ($key === '#placeholder' && is_string($value) && $value !== '') {
      $value = t($value);
    }
  });
}
4
  • Thanks It's Working butthis function showing error . I just pass placehole in t() function they showing in User interface translation. function mytheme_form_contact_message_feedback_form_alter(&$form, $form_state) { array_walk_recursive($form, function (&$value, $key) { if ($key === '#placeholder' && $value !=='') { $value = t($value); } }); } Commented Feb 4, 2017 at 11:14
  • What was the error? What you've changed to get it working? The code in your comment seems to be the same as in the answer.
    – 4uk4
    Commented Feb 4, 2017 at 11:25
  • I am Just use this function function mytheme_form_contact_message_feedback_form_alter(&$form, $form_state) { $form['subject']['widget']['0']['value']['#placeholder'] = t('Please enter a subject'); } Commented Feb 4, 2017 at 11:33
  • I think the error was from a placeholder already containing a translated markup. I've added an extra check.
    – 4uk4
    Commented Feb 4, 2017 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.