Easy translation for Field labels

Public

Sometimes Drupal just won't let you translate Fields labels on forms no matter what you do and how many modules you enable. This is a simple solution but it's not a general solution.

Get raw version
php
  1. /**
  2.  * Implements hook_form_alter()
  3.  * Translate fields
  4.  */
  5. function mymodule_form_alter(&$form, &$form_state, $form_id) {
  6.  
  7. // Do an if before all this to check for the $form_id
  8.  
  9. $fields = array();
  10. foreach($form as $key => $elements) {
  11. if(strstr($key, "field_")) {
  12. $fields[] = $key;
  13. }
  14. }
  15.  
  16. foreach($fields as $field) {
  17. if(isset($form[$field]["und"][0]["value"])) {
  18. $form[$field]["und"][0]["value"]["#title"] = t($form[$field]["und"][0]["value"]["#title"]);
  19. $form[$field]["und"][0]["value"]["#description"] = t($form[$field]["und"][0]["value"]["#description"]);
  20. }
  21. else if(isset($form[$field]["und"][0]["email"])) {
  22. $form[$field]["und"][0]["email"]["#title"] = t($form[$field]["und"][0]["email"]["#title"]);
  23. $form[$field]["und"][0]["email"]["#description"] = t($form[$field]["und"][0]["email"]["#description"]);
  24. }
  25. else {
  26. $form[$field]["und"][0]["#title"] = t($form[$field]["und"][0]["#title"]);
  27. $form[$field]["und"][0]["#description"] = t($form[$field]["und"][0]["#description"]);
  28. }
  29. }
  30. }

Comments

Anonymous's picture

How about translating drupal modules with a localization tool? This is what I am using: https://poeditor.com, a translation platform which has a plugin especially for Drupal.