I'm using Internationalization module and in my custom entity type, I have my custom field. Since this entity is going to be usable for all enabled languages, I have added translation to my custom field's label.

In node.tpl.php file in my custom template, I am calling this field values like this:

$field = field_info_instance('node', 'field_custom', 'custom_entity');  

And I render the field's label like this:

echo t($field['label']);

However, even though I've added translation for the label, it remains same string for all languages. I'm surely missing something here, how can I implement this?

Thanks in advance.

  • I had the same issue almost a week ago, latest i18n module release has alot of bugs with fields label translation, use the latest dev version and it works – Sohail Jun 26 '13 at 7:26
  • I thought it was something about my way, but if bugs are that obvious, I will give it a try and update the question if this is why. Thank you. – kubilay Jun 26 '13 at 8:10

You cannot use variables in t() function, it accepts only strings and placeholders with an array.

The i18n module has Field Translation module shipped with it, be sure you have enabled it as well.

There is also a good information on the following link about getting title fields translated as well.

Drupal 7 – Translate the title field label of content types

  • 1
    That's what I get confused about. If you use t('Home'), it translates. No array, just string: 'Home'. Isn't it relevant to string translations? I thought when you pass a string to t() function, it checks if there is a translated string and render it instead. P.S. I have enabled Field Translation already, otherwise I couldn't get to translate field properties that I mention in the question :-) – kubilay Jun 26 '13 at 8:09

OK, here's how I solved the problem.

Internatinalization module renders the content according to translated strings before node.tpl.php gets rendered. Thus, the $content variable in node.tpl.php contains the latest version of rendered page.

Since I was not using the <?php print render($content); ?> because I was creating a custom template, I have used the field properties inside this $content variable. And they contain the translated versions of labels.

Here's how you can get a translated label of a custom field:

echo $content['custom_field']['#title'];

P.S. If you want to get a field property from $content variable, you must set that field to visible in content type's manage display section.

  • This does work, unless you want to get the label of a field that doesn't have a value assigned. In that case the $content variable doesn't contain any info for the field (because it isn't actually part of that node's content). – doub1ejack Mar 19 '15 at 18:10

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.