06.05.2013 Views

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

Drupal 6 JavaScript and jQuery - Ebook-Cours.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

[ 135 ]<br />

Chapter 5<br />

The string looks like this: The URL for @siteName is !url. There are two<br />

placeholders in this string, @siteName <strong>and</strong> !url. When the <strong>Drupal</strong>.t() function is<br />

executed, the placeholders will be replaced by values from the params object.<br />

In this case, @siteName will be replaced by Example.Com, <strong>and</strong> !url will be replaced<br />

by http://example.<strong>com</strong>/. So the English rendering of the string would be The URL<br />

for Example.Com is http://example.<strong>com</strong>/.<br />

But wait! There are a couple of details to fill in. First of all, why are we using<br />

placeholders in the first place? And second, what are the @ <strong>and</strong> ! signs for?<br />

In answer to the first question, placeholders should be used for any values that<br />

should not be translated. The example uses a proper name for @siteName <strong>and</strong> a URL<br />

for !url. In cases like this, translation would be unnecessary. Presumably, the site<br />

name <strong>and</strong> URL are the same in all languages.<br />

This is a simple case where placeholders might be used. However, it's not all that<br />

<strong>com</strong>mon in practical cases.<br />

A more realistic use of placeholders is to substitute it in values that are not known<br />

at translation time. To elaborate the example, consider the case where the site name<br />

<strong>and</strong> site URL are retrieved from some other object. Let's say we have an object called<br />

SiteInfo that contains this information (This is a fictional example. There is no<br />

such object.)<br />

Our params object might look like this instead:<br />

var params = {<br />

"@siteName": SiteInfo.name,<br />

"!url": SiteInfo.url<br />

};<br />

Here, the values of these variables may not be known until runtime, long after the<br />

translation has been generated. So using placeholders clearly makes sense.<br />

Translations are created by humans, <strong>and</strong> the process of translation is<br />

mostly h<strong>and</strong>led manually. We will see this process in a few minutes.<br />

But nothing magical happens at runtime. Translated strings are simply<br />

substituted for the default (usually English-language) text.<br />

Placeholders are then used in cases where values need to be inserted into a translated<br />

string, but where the values themselves should not be translated as part of that string.<br />

This material is copyright <strong>and</strong> is licensed for the sole use by Richard Ostheimer on 18th June 2009<br />

2205 hilda ave., , missoula, , 59801

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!