Actions

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

From Joomla! Documentation

m (Separated generic languages material from this template-specific page.)
m (Oops! Forgot to add to the templates category.)
Line 25: Line 25:
  
 
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.
 
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.
<ninclude>[[Category:Languages]]</noinclude>
+
<ninclude>[[Category:Templates]][[Category:Languages]]</noinclude>

Revision as of 05:53, 24 August 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 search for “WELCOME” and this time might encounter the string

WELCOME=Wilkommen

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. <ninclude>