By mikedrupalfan on
Hi,
About t() functions, i have afew questions about it..
1-why to use t() function and not just ' print $variable ' ?
2-why can't i use a variable in it ?
3-are function like "format_string($string, array $args = array())" & check_plain($text) & drupal_placeholder($text) are auto-include in it ?
http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/forma...
Comments
1-why to use t() function and
1-why to use t() function and not just ' print $variable ' ?
To make strings used in Drupal translatable. Every string in a t() function can be translated through the Drupal UI. That way people who use a Drupal site in a different language can adapt it to their needs.
2-why can't i use a variable in it ?
You can, but you have to do it through a placeholder like one of these:
See http://api.drupal.org/api/drupal/includes!bootstrap.inc/function/t/7
3-are function like "format_string($string, array $args = array())" & check_plain($text) & drupal_placeholder($text) are auto-include in it ?
It depends on what you use as a placeholder:
See http://api.drupal.org/api/drupal/includes!bootstrap.inc/function/format_string/7
t() is mainly used to
t() is mainly used to 'Translates a string to the current language or to a given language.'
To answer your first two questions, see http://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/t/7
Also, simply using 'print $variable ' may cause security problem, see http://drupal.org/node/28984
These functions are auto inclued, and they could be used in code, depending these functions have been loaded. When drupal is still in bootstrap phase, some functions may not be available. For example, many drupal functions are not available inside hook_boot() http://api.drupal.org/api/drupal/modules%21system%21system.api.php/funct...
Thanks everybody. about the
Thanks everybody.
about the use of
.
Does drupal can automatically translate a string to outher language like the google translate ?
I don't think so, at least
I don't think so, at least Drupal core can't. The t() function is just there to make Drupal aware that a string is translatable. There might be a module, but I guess that automated translations are way too complex.
From what I understand, t()
From what I understand, t() uses a replace system to replace English string into local string. Where does it find item to replace? The answer is searching from table 'locales_source', and replacing it with item from table 'locales_target'.
For instance, regarding t('You'), string 'You' should be translated into German. And we know German string package has been imported, or you have edited it in 'admin/config/regional/translate/translate'(D7). So, we understand there's a string called 'You'(English string) residing in table 'locales_source' and a string called 'Sie'(German string) residing in table 'locales_target' to match string 'You'. Then when this site's default language is German, t('You') will search table 'locales_target' to find a record whose 'language' is 'de' and 'lid' matcing string 'You', and replace 'You' with the 'translation'(i.e., 'Sie') into HTML output.
Drupal can't work as how Google translate does. Because Google has a very very large 'locale_source' table and a 'locales_target' table. It also uses algorithm to do its translating, while Drupal simply replaces the exact string with another exact string.
Excellent!!
Very useful