Actions

Difference between revisions of "Embedding translatable strings in the template"

From Joomla! Documentation

m (Fixed typos.)
 
Line 22: Line 22:
 
then the effect will be to output the string “Welcome!” to the browser. If the user switches to the German language then the German language definition file will be searched for “''WELCOME''” and this time might encounter the string
 
then the effect will be to output the string “Welcome!” to the browser. If the user switches to the German language then the German language definition file will be searched for “''WELCOME''” and this time might encounter the string
  
<source lang="ini">WELCOME=Wilkommen</source>
+
<source lang="ini">WELCOME=Willkommen</source>
  
and so “Wilkommen” will be sent to the browser. Importantly, if the user switches to German but there is no German language file present, or the appropriate string does not appear in the German language file, then Joomla will fall back to sending the untranslated string “''Welcome''” to the browser, also preserving its original case.
+
and so “Willkommen” will be sent to the browser. Importantly, if the user switches to German but there is no German language file present, or the appropriate string does not appear in the German language file, then Joomla will fall back to sending the untranslated string “''Welcome''” to the browser, also preserving its original case.
 
<noinclude>[[Category:Templates]][[Category:Languages]]</noinclude>
 
<noinclude>[[Category:Templates]][[Category:Languages]]</noinclude>

Latest revision as of 04:57, 9 November 2008

In the template itself translations are handled using the JText static class. It is referred to as “static” because it does not require instantiation as an object before its methods may be used.

Simple text strings

Most text strings can be translated using the “_” (underscore) method. For example, suppose your template contains the English text “Welcome” which needs to be made translatable.

<?php
    echo 'Welcome';
?>

Then you would replace the static string like this

<?php
    echo JText::_( 'Welcome' );
?>

This would cause the translation system to search the appropriate language file for “WELCOME” on the left-hand side of an equals sign. The search is case-insensitive. If this language definition string is encountered

WELCOME=Welcome!

then the effect will be to output the string “Welcome!” to the browser. If the user switches to the German language then the German language definition file will be searched for “WELCOME” and this time might encounter the string

WELCOME=Willkommen

and so “Willkommen” will be sent to the browser. Importantly, if the user switches to German but there is no German language file present, or the appropriate string does not appear in the German language file, then Joomla will fall back to sending the untranslated string “Welcome” to the browser, also preserving its original case.