0

I have created a content type (pdf_form) to which I added a field_gender_languagetest with two options, male and female. I translated both the options in German using the translation tab.

Now I am generating a report of the data filled by users using that content type.

Is there any way to programmatically get that translation?

I am trying this code, but it doesn't work.

t('male', array(), array('langcode' => 'de')); 

my working solution

  $lang ="de";

  if ($lang != "en") {
    $langs = language_list();
    $lang = $langs[$lang];
    foreach ($val['#base']['#node'] as $k => $v) {
      if (isset($v['#type']) && $v['#type'] == 'list_text') {
        foreach ($val['#base']['#node'][$k]['#value'] as $k_sub => $v_sub) {
          $trns_str = pdf_trans($v_sub, $lang, 'field');
          $val['#base']['#node'][$k]['#value'][$k_sub] = $trns_str['trans'];
        }
      }
    }
  }

Function

function pdf_trans( $string, $lang = NULL,$group = 'default') {
  $result = db_query("SELECT s.lid, s.source, s.context, s.location, t.translation, t.plid, t.plural FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = :language WHERE s.textgroup = :textgroup and s.source = :string_value ORDER BY t.plid, t.plural", array(':language' => $lang->language, ':textgroup' => $group, ':string_value'=>$string));
  $str= array();

  if($result){
    foreach ($result as $child) {
      $str= array(
        'trans' => isset($child->trans) ? $child->trans : '',
        'trans_availabe' => '1',
      );
    }
  }
  if(count($str)==0)
  {
    $str= array(
      'trans' => $string,
      'trans_availabe' => '0',
    );
  }
  return $str;
}

This shoudn't be the right way because drupal have must this functionality as core

3
  • 1
    If the report is programmatically generated, the question should show more code than it does. I doubt the code is actually using t('male', array(), array('langcode' => 'de')); to translate a field value. It's more probable the code is similar to t($field_value, array(), array('langcode' => 'de')); where $field_value is the value contained in the entity field.
    – apaderno
    Jul 19 at 11:30
  • actually I have only language code that is setup in my custom module for example de,jp ...if it is de i have to use translation from content type -> manage field -> (field) list -> translate -> de -> (here we have present)
    – Rog Boy
    Jul 19 at 15:00
  • please check question updated
    – Rog Boy
    Jul 19 at 15:10

Your Answer

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

Browse other questions tagged or ask your own question.