0

In order to get the label of a field I just do :

$fii = field_info_instance('node', 'field_app_area', 'products');
$label = $fii['label'];

But it returns the label in the original language of the field, not the current user language.

I have seen some code here to get the translated label from the $content variable of a node template, but I don't have the template variables available.

So my question is either : how do I get the label translation or how do I get the $content (or similar) variable available without being on a template.

  • Hello, can you indicate the drupal version that you're using? Also, you can consider passing it through t(). Quick and dirty solution though. – Japo Domingo Mar 16 '17 at 20:41
  • Forgot that, it's drupal 7, added the tag. Also whoever down-voted the question I would be grateful to know why, so I can improve it. – drcelus Mar 17 '17 at 10:44
  • @drcelus it's good question vote up . – Yusef Mohamadi Mar 17 '17 at 11:09
1

Turns out that using t(), doesn't work for translated field labels.

To get the translated field label text (in the current language), just use the i18n_field_translate_property function

  $field_instance = field_info_instance('node', 'field_application_area', 'products');
  $label = i18n_field_translate_property($field_instance, 'label');

Or, for a specific language :

  $label = i18n_field_translate_property($field_instance, 'label', 'fr');

Found on this related answer.

0

Try this

$fii = field_info_instance('node', 'field_app_area', 'products');
$label = t($fii['label']);

and according to this if you want to get on specified language try

$fii = field_info_instance('node', 'field_app_area', 'products');
$en_label = t($fii['label']);
$fr_label = t('language', array(), array('langcode' => 'fr'));
  • Thank you, but this doesn't work for translated field labels. – drcelus Mar 17 '17 at 11:38
  • @drcelus I didn't test it before, vote up for your answer ,it's correct answer. – Yusef Mohamadi Mar 17 '17 at 11:45

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.