Making templates translatable

From Joomla! Documentation

Revision as of 16:34, 22 August 2008 by Chris Davenport (Talk | contribs)

Introduction to template translation

<translate> Joomla! is a truly international application and supports the translation of all strings contained within it. Templates are no exception, and a little extra time spent ensuring that the strings used in your templates are translatable will pay dividends. </translate>

<translate> The language translation system has been designed to be as simple and error-proof as possible. For example, even if a language file is missing, or a particular string has not been translated, Joomla will transparently fall back to showing the untranslated string. There are also some useful tools built into Joomla itself to assist translators in creating a new translation. </translate>

<translate> In this document you will learn about constructing language definition files for your template and how to include translations in your template package file. You will also learn how to make sure that all strings used in your template are translatable and how to debug a new translation. </translate>

Location of template language definition files

  1. REDIRECT J1.5:Location of template language definition files

Creating a language definition file

<translate> series</translate>

<translate> Important note: Joomla language .ini files must be saved as UTF-8 </translate>

<translate> The format of language definition files is very basic. Blanks lines and lines beginning with “#” are ignored and the latter may be used to add comments to the file. Each line consists of a pair of strings separated by an equals sign like this : </translate>



<translate> where “KEY” is a string to be translated and “Value” is the translated string. For example : </translate>

ADDITIONAL_INFORMATION="Additional Information"

<translate> The “KEY” must be in all capitals or the string will not be found. The case of the source string does not matter as strings are folded to upper case before searching takes place. So “additional information”, “Additional Information” or even “AdDiTiOnAl InFoRmAtIoN” will be matched. </translate>

<translate> The “KEY” can include spaces and other punctuation characters but there should not be any spaces either side of the equals sign as spaces are significant. If more than one entry has the same left-hand side, the last one to be encountered is the one that will be used. </translate>

<translate> The "VALUE" cannot include double-quote characters ("). To get a double-quote character you must use the HTML special character sequence "&quot;" instead. Single-quote characters (') are valid. </translate>

<translate> More information about the discussed changes for the Joomla 2.5 and 3.x series can be found on the page Specification of language files </translate>

Amending the templateDetails.xml file

<translate> To ensure that your template is fully internationalised you must make sure that certain XML elements are translated and that the language definition files are listed in the templateDetails.xml file. </translate>


Translating templateDetails.xml

A couple of the elements in the templateDetails.xml file are used in the Template Manager and are themselves translatable. The description should always be translated. </translate>




Name of the template. For example, Beez</translate>




Description of the template</translate>

<translate> These fields are also shown to the user during template installation. </translate>


Adding language definition files to templateDetails.xml

</translate> <translate> All language files must be declared in the templateDetails.xml file. This is done by adding two <language> elements for each language to be included with the template; one for the front-end strings; the other for the administrator back-end strings. For example, the two British English language files and the two German language files for the Beez template are declared as follows: </translate>

<?xml version=”1.0” encoding=”utf-8” ?>
<install version=”1.5” type=”template”>
        <language tag=”en-GB”>en-GB.tpl_beez.ini</language>
        <language tag=”de-DE”>de-DE.tpl_beez.ini</language>
        <languages folder=”admin”>
            <language tag=”en-GB”>en-GB.tpl_beez.ini</language>
            <language tag=”de-DE”>de-DE.tpl_beez.ini</language>

<translate> Note that in the administration <languages> tag the folder attribute is used. This is because the language files for the front-end and back-end have the same file names and so cannot exist in the same directory within the template package file. In this example, the administration language files have been placed in a sub-directory called admin to separate them from the front-end language files. </translate>

Embedding translatable strings in the template

<translate> 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. </translate>


Simple text strings


<translate> 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. </translate>

    echo 'Welcome';

<translate> Then you would replace the static string like this </translate>

    echo JText::_( 'Welcome' );

<translate> 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 </translate>


<translate> 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 </translate>


<translate> 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. </translate>

Debugging a translation

<translate> Joomla supports some useful debugging mechanisms that can make it easier to locate untranslated strings and diagnose problems with language translations in installed extensions. </translate>


Debug Language

</translate> [[Image:global-config-language-debug-<translate> en</translate>.png|right]]<translate> You activate language debugging via the Administration Back-end by going into Global Configuration and clicking on the System tab. Find the Debug Language field, change the value to “Yes” and save your changes. </translate>

<translate> With this option active all translatable strings are shown surrounded with special characters that indicate their status </translate>





●Joomla CMS● <translate>

(text surrounded by bullets) indicates that a match has been found in the language definition file and the string has been translated.</translate>

 ??Joomla CMS?? <translate>

(text surrounded by pairs of question marks) indicates that the string is translatable but no match was found in the language definition file.</translate>

Joomla CMS <translate>

(text with no surrounding characters) indicates that the string is not translatable.</translate>


Debug System

</translate> <translate> Additional language debugging information can be obtained by activating system debugging. This is done by going into Global Configuration and clicking on the System tab. Find the Debug System field, change the value to “Yes” and save your changes. </translate>

<translate> With this option active all screens have additional debugging information at the end of each page. Currently this includes </translate>

  • <translate>

Profile information. This is the amount of time taken to execute code up to various mark points in the code.</translate>

  • <translate>

Memory usage. The amount of system RAM used.</translate>

  • <translate>

SQL queries executed. All of the SQL queries executed in the process of building the page.</translate>

  • <translate>

Language files loaded. A list of all the language files loaded in the process of building the page, including full path information. This can be useful to check that the expected files have been loaded. The number after each file path is the number of times that the file was referenced.</translate>

  • <translate>

Untranslated strings diagnostic. A list of all the untranslated strings found and the likely file location given where the JText call was made.</translate>

  • <translate>

Untranslated strings designer. A list of all the untranslated strings found but listed in a KEY=Value format so they can be copy-pasted directly into a language definition file (INI).</translate>


Debug Plugin

</translate> [[Image:debug-plugin-<translate> en</translate>.png|right]]<translate> This system plugin controls what is displayed when debugging is activated in Global Configuration. It is enabled by default. You can access the parameters for the plugin from Extensions → Plugin Manager. Locate the “System - Debug” plugin and click on it. There are three settings of interest to translators.</translate>

  • <translate>

Display loaded language files. If set to “Yes” then the debug information will include a list of the language files that were requested as the current page was being generated.</translate>

  • <translate>

Display undefined language strings. If set to “diagnostic mode” then a list of untranslated strings and the location of the file containing the call to JText is included in the debug information. If set to “designer mode” then a list of untranslated strings in a format that can be copy-pasted directly into a language definition file is included in the debug information. That is, it displays the list in KEY=String format. If set to “All modes” then both the diagnostic mode and designer mode lists are included in the debug information.</translate>

  • <translate>

Strip Key Prefix. Only used when Display undefined language strings is set to “Designer mode” or "All modes". This allows you to strip a prefix from the string to form the key. This is useful if the designer uses a common prefix for their extensions when using JText methods. See example below.</translate>

<translate> Note that the display of untranslated strings will only display the value passed to the appropriate JText method. For example, with the following code: </translate>

echo JText::_( 'Reports Import Configuration' );

<translate> If untranslated, Designer mode will display this as: </translate>

# /administrator/components/com_reports/views/reports/tmpl/default.php

<translate> If the Strip Key Prefix is set to "Reports", then the display would change slightly to: </translate>

# /administrator/components/com_reports/views/reports/tmpl/default.php

<translate> Note that the path shown is only a best guess based on a call to the PHP debug_backtrace function. Sometimes it is accurate, sometimes it is not and there are also cases where no file could be determined. In those cases you have to use your best judgement. </translate>