This weekend I ran into a bit of a problem with the simplest of tasks. I was trying to display a message to the user with drupal_set_message() and I wanted this message to be translatable. This was my code:

function hook_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'view':
      drupal_set_message(t("This message should be added to translation."));
      break;
  }
}

A common pitfall when working with t() is not making sure the text had been displayed on my site, but this of course I had already tried.

After posting to forums, rearranging the message to be set in a variable instead, trying to implement hook_locale to get the message to show up in the 'Translate interface' I was finally adviced to to make sure that my text not only was being displayed in my default language but I should also try to make sure it was displayed in a node that belonged to another language that I had installed.

This solved the problem and thus it seems that t() (sometimes?) is not adding strings if they have not been viewed in both the default language and in any other installed language. This could very well be by design and if it is I think the docs should be updated to clearly state so. If not I think this might be a bug since non of my colleagues ever heard about this problem.

Here is the link to the forum where the community solved this problem for me:
http://drupal.org/node/799608

Summary

  • To reproduce, create a new module and add the code above.
  • Expected behaviour is the string is added to the database to be found when searching for it in 'Translate interface' after viewing a node which displays the string.
  • Problem: The string was not showing up in the translate interface until it had been viewed in at least two languages.

span

Comments

span’s picture

Title: t() function nog adding string to database when only showed on default language » t() function not adding string to database when only showed on default language

Fixed typo in title.

Damien Tournoud’s picture

Status: Active » Closed (works as designed)

This is pretty much by design. Strings are stored in the database the first time a translation is looked up, and strings in the default language (English) don't have to be translated.

Please note that the recommended way of extracting strings is by using the Potx module.