Hi There

Our dedication to our clients has always been top priority. We have a personal connection to and grow with each client, rather than extracting maximum money out of each project.  Everyone is technical and a builder, there is no wasted budget going toward non-technical, project manager middle men.

Free Energy Media the most effective and efficient WordPress, Drupal HTML5 and mobile development agency in NYC, has partnered with  Pixilshift, check our new website out there.

Content translation is a desired feature for sites targeting audience of multiple countries or of more than one linguistic groups. For Drupal websites, it is quite easy once you know how to do it. We will discuss some standard practices while using translate function that we must follow to be able to swiftly and smoothly translate our website to another language.

The Translate Function:

When we are dealing with content in templates for pages/nodes/content-types, etc. some strings are hardcoded in these tpl.php files.

Drupal provides us a smart way to let its template engine know which strings to consider for translation and which strings to treat as constant literals – that don’t need translation.

There is a translate function, also known as t function or t(). Anything passed inside this function is processed and considered for translation before getting passed to html content. Example: t(“text for translation”).

String literals that are not enclosed in this function are considered as constant strings and are as it is passed into the html content. Example: Anything that doesn’t require translation – like Names of places, persons, etc. (in case we are translating in same script like English to French)

The prototype of translate function is:

t($string, array $args = array(), array $options = array());

Here, $string is the variable containing the English string to translate.
$args is an associative array of replacements to make. Occurrences in $string of any key in $args are replaced with the corresponding value, after optional sanitization and formatting. The type of sanitization and formatting depends on the first character of the key:

  • @variable: Value is inserted as plain text.
  • %variable: This makes it display as <em>emphasized</em> text.
  • !variable: Inserted as is, with no sanitization or formatting. Only use this for text that has already been prepared for HTML display (for example, user-supplied text that has already been run through check_plain() previously, or is expected to contain some limited HTML tags and has already been run through filter_xss() previously).

This is an example to demonstrate above three keys:

t("This is my <b>text</b>", array( '@This' => 'yeh', '%is my' => 'hi', '!<b>text</b>' =>  '<i>text</i>'));

It will translate from “This is my text” to “yeh hi text”.
The simplest call to t() looks like this:
t(“This is my text”);

Where to call t() function ?

  • Any string that is a name, url, address passed in t() should specify the translated string also either from the admin panel or in the translate function call itself by passing through $args array.
  • In case you are making a Form with form API, it is a good practice to pass the title, placeholder and labels in t() function. Example:$form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#required' => TRUE,
    );
  • In case your are using a php field in views, it is advisable to output any text in t(), except for dates or dynamic data.

Where not to call t() function ?

It is NOT advisable to use t() in following scenarios:

  • t($text);
    unless the text that the variable holds has been passed through t() elsewhere (e.g., $text is one of several translated literal strings in an array).
  • It is especially important never to call
    t($user_text);
    where $user_text is some text that a user entered – doing that can lead to cross-site scripting and other security problems.

However, you can use variable substitution in your string, to put variable text such as user names or link URLs into translated text. Variable substitution looks like this:

$text = t("@name's blog", array('@name' => format_username($account)));

Basically, you can put variables like @name into your string, and t() will substitute their sanitized values at translation time. Translators can then rearrange the string as necessary for the language (e.g., in Spanish, it might be “blog de @name”).

This widget allows the Drupal Five Star module to accept inputs from multiple votes for various criteria and average it into one, It then displays the average of the multiple votes.

To create views sortable by the new widget (the average of five votes), hook_votingapi_results_alter() was used in a small custom module.

Below is a tutorial on how to implement the code.

Best practices using multiple votes criteria In node using FiveStar module in Drupal 6.

1. Enable Fivestar module, Voting API.
2. Go to edit content type and Enable Fivestar rating.
3. Add CCK field Type Fivestar Rating with “Fivestar rating” Select list widget.
4. Configure this CCK field: add Voting Axis: which is the multiple criteria separated by a comma. For example: first, second, third, fourth, fifth . Save field settings.
5. Create the template for node type. In this example it is node type is “teacher”. This will be node-teacher.tpl.php .

In the template paste this snippet:

$output = '';
$tags = array(
'first' = t('Communication'),
'second' = t(‘Availability'),
'third' = t(‘Skills'),
'fourth' = t(‘Efficiency'),
'fifth' = t(‘Personality'),
);
$i = 0;
foreach ($tags as $tag = $title) {
$votes = fivestar_get_votes('node', $node->nid, $tag);
if(!empty($votes['average']['value'])){
$i++;
}
$values = array(
'user' = isset($votes['user']['value']) ? $votes['user']['value'] : NULL,
'average' = isset($votes['average']['value']) ? $votes['average']['value'] : NULL,
'count' = isset($votes['count']['value']) ? $votes['count']['value'] : NULL,
);

if (user_access('rate content')) { /*check user access for voting in fivestar*/
$settings = array(
'stars' = 5,
'allow_clear' = TRUE,
'style' = 'average',
'text' = 'dual',
'content_type' = 'node',
'content_id' = $node->nid,
'tag' = $tag,
'autosubmit' = TRUE,
'title' = $title,
'feedback_enable' = TRUE,
'labels_enable' = TRUE,
'labels' = array(t('Poor'), t('Okay'), t('Good'), t('Great'), t('Awesome')),
);
$output .= drupal_get_form('fivestar_custom_widget', $values, $settings);
}
else {
$output .= theme_fivestar_static($values['average'], 5, $tag);
}
}

$reliability_rating = votingapi_select_results(array('content_id' = $node->nid, 'tag' =array('first', 'second', 'third', 'fourth', 'fifth' ), 'function' = 'average'));
foreach ($reliability_rating as $v) {
$a = $a + $v['value]; /*sum values*/
}
?>
<?php
print theme('fivestar_static', $a/$i, '5') ; /* print fivestar widget with general average result*/
?>
<?php
print $output; /*print 5 fivestar fields for votes with average result each*/
?>

Check out the module used in this social networking site for yoga teachers, (ignore debug output at top)

Sort By Votes

To create views sortable by votes here is a small custom module that uses hook_votingapi_results_alter().  This is a complete custom module so save this code in a .module file with it’s own .info file.
[php]
function mymodule_votingapi_results_alter(&$results, $content_type, $content_id) {
$vote_avg_sum = 0;
$vote_avg_count = 0;
$vote_tags = 0;

foreach($results as $tag = $data) {
if($tag != ‘vote’) {
$vote_avg_sum += $data[‘percent’][‘average’];
$vote_avg_count += $data[‘percent’][‘count’];
$vote_tags++;
}
}
if($vote_tags > 0) {
$results[‘vote’][‘percent’][‘average’] = $vote_avg_sum/$vote_tags;
$results[‘vote’][‘percent’][‘count’] = $vote_avg_count/$vote_tags;
}
}

1.In views settings add relationships
“Node: Vote results ”

2. configure it
“Value type: No filtering” “Vote tag: Normal vote” “Aggregation function:No filtering”

3. Go to Sort criteria and add
“Vote results: Value”
choose Descending or Ascending.

4. Add the filters that you need.

5. Then you can add to your fields
“Vote results: Value”