1

I made a content type programmatically with the entity_translation module. There are about 250 fields.

240 fields are not to translate because their values are identical. So I only want to use entity_translation for about 10 fields.

Now i have a lot of checkboxes and select fields. This are all associative arrays. Out of the box the values are shown in English for German and English urls.

E.g.: Select list: ("keyA" =>''valueA", "keyB"=>"valueB") shows "valueA" or "valueB" as result for the field in the node. I want the t-function included to use a *.po file for translation. Thus t('valueA') has to be implemented before rendering the field?

How can I do this?

THX in advance,

0

In short: use hook_field_widget_form_alter()

/*
* translate the form
*/
function mymodule_field_widget_form_alter(&$element, &$form_state, $context){

if(isset($element['#title'])){
  $element['#title'] = t($element['#title']);
}
if(isset($element['value']['#title'])){
  $element['value']['#title'] = t($element['value']['#title']);
}
if(isset($element['#description'])){
  $element['#description'] = t($element['#description']);
}
if(isset($element['value']['#description'])){
  $element['value']['#description'] = t($element['value']['#description']);
}
}

This translates titles and edscriptions of textstrings.

To show the fields title and description in the given language in node_view:

function mymodule_preprocess_field(&$vars) {
if ($node = menu_get_object()){
  if($node->type == 'immoclient'){
    // Check to be sure there's a label and it's visible.
    if(isset($vars['label']) && $vars['label_hidden'] === FALSE){
      $vars['label'] = t($vars['label']);
    }
    $field_name = $vars['element']['#field_name'];
    $trans_fields = mymodule_trans_fields();
      if(in_array($field_name, $trans_fields)){
        $items = $vars['items'];
        $result = count($items);
        for($i = 0;$i < $result; $i++){
          $vars['items'][$i]['#markup'] = t($vars['items'][$i]['#markup']) ;
        }
      }
     }
    }
   }

and mymodule_trans_fields() returns an array of fields to translate.

To show the items (and here the answer) in the appropriate language for selects and checkboxes in the form, I used hook_form_FORMID_alter:

function mymodule_form_[mynode_form]_alter(&$form,&$form_state,$form_id){
    foreach($form as $key => $value){
      $field = strpos($key, 'field_');
        //only for fields!
          if($field !== false){
            if(isset($form[$key]['und']['#description'])){
            //seems so that every field has a description, to use it for items too
            //build variables
              $res = $form[$key]['und'];
              $desc_t = t($form[$key]['und']['#description']);
              $form[$key]['und']['#description'] = $desc_t;
                }
               }
             }
           }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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