I built a rule using rules module to create a node of a certain content type which inserts a text of full_html format inside a body field when follow flag is flagged. This is the rule

$values = array(
        'type' => 'notification',
        'uid' => UID_OF_A_USER,
        'status' => 0,
        'comment' => 0,
        'promote' => 0,
        'language'  => 'en'
$entity = entity_create ('node', $values);
$ewrapper = entity_metadata_wrapper('node', $entity);
$uname = "USERNAME_OF_A_USER";
$link = l("$uname","$url");
$body_content = t("!link started following you.",array('!link' => $link));
$ewrapper->field_body->set(array('value' => $my_body_content ,'format'=>'full_html') );

When a user uses Follow flag to follow another user, a message is sent to the followed user which says, !link started following you.

This rule works perfectly but the problem is with translation of the !link started following you.. My website supports three languages. This is the problem:

If a user (flagging user) flags another user using Follow flag and he is in a certain language, say french, The above message in french language will be sent to the followed user, no matter in what language the followed user is viewing his profile, he will see this message in french language and by switching language it will not be translated.

I need this message to be translated in the current language of flagged user when he is viewing its profile but the message is sent in the language of the flagging user and in the profile page of the followed user will not be translated even if he switches the language.

How can I make this message translatable?


Have a look at the options you can pass on to the t() function. You can explicitly say which language you want.

So you might do something like this to handle you message content :

$to_user = get_destination_user(); // whichever way you do that...
$body_content = t("!link started following you.",
                  array('!link' => $link),
                  array('langcode' => $to_user->language ));

In essence something like the above should do it.

Also make sure that the user language is set and valid. For this you might do something like :

$to_user = get_destination_user(); // whichever way you do that...

// get destination user language if set
$message_lang = !empty($to_user->language) ? $to_user->language : '';

// check if language is defined on our site
$languages = language_list();
if (!isset($languages[$message_lang])) {
    $message_lang = '';
// check if the language is enabled
if (!$languages[$message_lang]->enabled) {
    $message_lang = '';

$body_content = t("!link started following you.",
                  array('!link' => $link),
                  array('langcode' => $message_lang ));
| improve this answer | |
  • Thank you. This code will translate the text to the followed user language but by switching the language in profile page of the followed user, this message is not translated. – M a m a D Sep 30 '15 at 8:56
  • Ok, got it. You actually want these messages to follow language change for the followed user. In that case I suggest that you store the messages untranslated and t() them on the template that actually shows them. Would that solve your problem ? – rtome Sep 30 '15 at 9:04
  • Yes that will work, but it is preferred to use the core translation system. If I fail I will certainly do that – M a m a D Sep 30 '15 at 9:05
  • Or else just store the link in your field and then use hook_field_display_alter() to do the t("!link started following you.",array('!link' => $link)) part. – rtome Sep 30 '15 at 9:08
  • Will this hook get triggered in Panels? – M a m a D Sep 30 '15 at 9:12

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.