Template language file

Sometimes when creating or editing a template you may have wondered how to include text strings without hard coding them. This is very simple when using the language system in Joomla. The first thing you need to do is create a file called xx-XX.tpl_{template-name}.ini, where xx-XX is the language code (for example en-GB for english or es-ES for spanish) and place it in root/language/xx-XX/. Inside it you can define the strings like this:

# comment
TPL_NAME_STRING = string

# example
TPL_HELIX_CHARACTERS_LEFT = Characters left

The TPL_NAME is a convention to know where the language strings are coming from. When you have all your strings defined you can use them in your template overrides. To do so you need to load them first by placing the following code at the top of the override's default.php file:

// Import template language file 
$app =& JFactory::getApplication();
$template = $app->getTemplate();
$lang =& JFactory::getLanguage();
$lang->load('tpl_'.$template, JPATH_SITE);

Once you have that you can use your string in this file like this:

echo JText::_( 'TPL_NAME_STRING' );

The strings will also be available on sub templates like default_items.php for example.

Adding the language in the install

By default Joomla will place language files from extensions in the language folder. To tell Joomla that you have used language files in your template you need to modify templateDetails.xml by adding the following:

<install ... >
.
.
.
<languages> 
<language tag="xx-XX">language/xx-XX/xx-XX.tpl_name.ini</language>
</languages>
.
.
.
</install>

Where name is the name of the template. If you have made many language files simply list them inside the languages tags. You must include the .ini file in that directory structure in the template install file, of course.

Rate this item
(0 votes)
Tagged under

5 comments

  • Comment Link Olympic Wednesday, 19 October 2011 09:58 posted by Olympic

    Great site!!keep up the good work

  • Comment Link Demente Tuesday, 04 October 2011 10:48 posted by Demente

    We're glad to know it's helpful :)

  • Comment Link Web development Banglades Monday, 03 October 2011 06:44 posted by Web development Banglades

    great blog site...like it...thank you...

  • Comment Link WEb design Bangladesh Monday, 03 October 2011 06:43 posted by WEb design Bangladesh

    this is a great blog site...it is full of important and useful information...i have been working on template and these information and materials has help me a lot...thank you for sharing these with us...

  • Comment Link Tom Lottery guides Monday, 03 October 2011 06:41 posted by Tom Lottery guides

    this is a great blog site...it is full of important and useful information...i have been working on template and these information and materials has help me a lot...thank you for sharing these with us...

Leave a comment