Translating dynamic string? $subject = $user . " commented on your " . $type;

Events happening in the community are now at Drupal community events on www.drupal.org.
jaochoo's picture

I need to send email notifications like the following: $subject = $username . " commented on your " . $type; whereas $type is the content type, e. g. "blog", "gallery", etc.

Now I would like to send translated emails depending on the user's preferred language. So I also need to translate the $type, of course, otherwise I would get a mixed-language subject line. So my first idea was to use:

<?php
$subject
= t("!username commented on your !type", array(
 
'!username' => $user->name,
 
'!type' => t($type)
));
?>

But the API about the t() function states that "[b]ecause t() is designed for handling code-based strings, in almost all cases, the actual string and not a variable must be passed through t()."

So how can I achieve what I want and translate a string which is dynamic, i. e. I do not know it before?

Comments

markus_petrux's picture

Install the i18nstrings module (part of i18n), and then you can do something like this:

<?php
$subject
= t("@username commented on your @type", array(
 
'@username' => $user->name,
 
'@type' => i18nstrings("nodetype:type:$type:name", node_get_types('name', $type)),
));
?>

[EDIT] replacing ! by @ in first argument of t() to protect the output from XSS.

Thanks, Markus, that sounds

jaochoo's picture

Thanks, Markus, that sounds like a promising solution. However, how does the i18nstrings("nodetype:type:$type:name", node_get_types('name', $type)) know the language I am using/translating into?

markus_petrux's picture

Best source of information is the code itself and code comments all around the source (doxygen). Function i18nstrings() can be found in i18nstrings.module itself.

It also worths noting the comments at the top of that file:

/**
* @file
* Internationalization (i18n) package - translatable strings.
*
* Object oriented string translation using locale and textgroups. As opposed to core locale strings,
* all strings handled by this module will have a unique id (name), composed by several parts
*
* A string name or string id will have the form 'textgroup:type:objectid:property'. Examples:
*
* - 'profile:field:profile_name:title', will be the title for the profile field 'profile_name'
* - 'taxonomy:term:tid:name', will be the name for the taxonomy term tid
* - 'views:view_name:display_id:footer', footer text
*
* Notes:
* - The object id must be an integer. This is intended for quick indexing of some properties
*
* Some concepts
* - Textgroup. Group the string belongs to, defined by locale hook.
* - Location. Unique id of the string for this textgroup.
* - Name. Unique absolute id of the string: textgroup + location.
* - Context. Object with textgroup, type, objectid, property.
*
* Default language
* - Default language may be English or not. It will be the language set as default.
*   Source strings will be stored in default language.
* - In the traditional i18n use case you shouldn't change the default language once defined.
*
* Default language changes
* - You might result in the need to change the default language at a later point.
* - Enabling translation of default language will curcumvent previous limitations.
* - Check i18nstrings_translate_langcode() for more details.
*
* The API other modules to translate/update/remove user defined strings consists of
*
* @see i18nstrings($name, $string, $langcode)
* @see i18nstrings_update($name, $string, $format)
* @see i18nstrings_remove($name, $string)
*
* @author Jose A. Reyero, 2007
*/

Please note that these comments could change in the future as the code itseld evolves.

Internationalization

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: