Score:0

get translation programmatically of field value in specific language

ma flag

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

apaderno avatar
us flag
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.
Rog Boy avatar
ma flag
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 avatar
ma flag
please check question updated
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.