Actions

User

Rvsjoen/tutorial/Developing an MVC Component/Part 08

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing an MVC Component

Contents

Adding language translation

So, we would like our component to support translation, which essentially means that instead of hard-coding strings, we would like the strings to be read from translation files. This makes it very easy to provide multiple translations since all that has to be done to change the language is to read a different language files.

Frontend translation

The first thing we will do is to add the language files, for now the frontend language files will be empty since we are not displaying anything in the frontend except for the hello message itself, but in the order of completeness, we will still add a file for translating the frontend.

With your favorite editor, create the following empty file

site/language/en-GB/en-GB.com_helloworld.ini

 

Now, this can get a little tricky, since the view is part of the frontend but the menu item type (site/views/helloworld/tmpl/default.xml) is used in the backend, the translation strings for this xml file will be added to the backend language files and not the frontend, so for now we will only replace the language strings.

With your favorite editor, modify the following file to look like this

site/views/helloworld/tmpl/default.xml

<?xml version="1.0" encoding="utf-8"?>
<metadata>
        <layout title="COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE">
                <message>COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC</message>
        </layout>
        <fields name="request"
                addfieldpath="/administrator/components/com_helloworld/models/fields">
                <fieldset name="request">
                        <field
                                name="id"
                                type="helloworld"
                                label="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL"
                                description="COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC"
                        />
                </fieldset>
        </fields>
</metadata>

Backend translation

With your favorite editor, create the following file

admin/language/en-GB/en-GB.com_helloworld.ini

COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_DESC="This message will be displayed"
COM_HELLOWORLD_HELLOWORLD_FIELD_GREETING_LABEL="Message"
COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING="Greeting"
COM_HELLOWORLD_HELLOWORLD_HEADING_ID="ID"

As you can see, language strings are sort of namespaced. This is a very good practice for avoiding that strings override each other. As an example these strings start with COM_HELLOWORLD which is the component name, and then the next part is HELLOWORLD which is the view name. FIELD clearly states that we are dealing with language strings for form fields etc. This method also makes it very easy to sort the languages files in order to have all the strings of a certain type in the same place, making it both easier to read and to maintain.

In order to provide translation during the installation process, in the extension manager and in the menus, another language file is required in the backend. The difference between these two files can be daunting at first, but usually the sys.ini contains a lot less translation strings and this file is loaded in scenarios where the loaded component is not com_helloworld itself, but minimal translation is still needed.

With your favorite editor, create the following file

admin/language/en-GB/en-GB.com_helloworld.sys.ini

COM_HELLOWORLD="Hello World"
COM_HELLOWORLD_DESCRIPTION="This is the Hello World description"
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_TITLE="Hello World"
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC="This view displays a selected message"
COM_HELLOWORLD_MENU="Hello World"

With your favorite editor, modify the following file to look like this

admin/views/helloworld/tmpl/default_head.php

<?php
// No direct access to this file
defined('_JEXEC') or die;
?>
<tr>
        <th width="5">
                <?php echo JText::_('COM_HELLOWORLD_HELLOWORLD_HEADING_ID'); ?>
        </th>
        <th width="20">
                <input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count($this->items); ?>);" />
        </th>                   
        <th>
                <?php echo JText::_('COM_HELLOWORLD_HELLOWORLD_HEADING_GREETING'); ?>
        </th>
</tr>

Here we simply replace the previously hardcoded table headings with their language string counterparts.

Installation manifest

In our manifest, in addition to updating the version number and adding the language folders for both the frontend and backend, we also replace the component name, description and the menu title with language strings which are present in our language files (line 4, 15 and 41).

helloworld.xml

  1. <?xml version="1.0" encoding="utf-8"?>
    
  2. <extension type="component" version="2.5.0" method="upgrade">
    
  3.  
    
  4.         <name>COM_HELLOWORLD</name>
    
  5.         <!-- The following elements are optional and free of formatting constraints -->
    
  6.         <creationDate>June 2011</creationDate>
    
  7.         <author>John Doe</author>
    
  8.         <authorEmail>john.doe@example.org</authorEmail>
    
  9.         <authorUrl>http://www.example.org</authorUrl>
    
  10.         <copyright>Copyright Info</copyright>
    
  11.         <license>License Info</license>
    
  12.         <!--  The version string is stored in the components table -->
    
  13.         <version>0.0.8</version>
    
  14.         <!-- The description is optional and defaults to the name -->
    
  15.         <description>COM_HELLOWORLD_DESCRIPTION</description>
    
  16.  
    
  17.         <install>
    
  18.                 <sql>
    
  19.                         <file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
    
  20.                 </sql>
    
  21.         </install>
    
  22.         <uninstall>
    
  23.                 <sql>
    
  24.                         <file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
    
  25.                 </sql>
    
  26.         </uninstall>
    
  27.  
    
  28.         <!-- Note the folder attribute: This attribute describes the folder
    
  29.                 to copy FROM in the package to install therefore files copied
    
  30.                 in this section are copied from "site/" in the package -->
    
  31.         <files folder="site">
    
  32.                 <filename>index.html</filename>
    
  33.                 <filename>helloworld.php</filename>
    
  34.                 <filename>controller.php</filename>
    
  35.                 <folder>views</folder>
    
  36.                 <folder>models</folder>
    
  37.                 <folder>language</folder>
    
  38.         </files>
    
  39.  
    
  40.         <administration>
    
  41.                 <menu>COM_HELLOWORLD_MENU</menu>
    
  42.                 <!-- Note the folder attribute: This attribute describes the folder
    
  43.                         to copy FROM in the package to install therefore files copied
    
  44.                         in this section are copied from "admin/" in the package -->
    
  45.                 <files folder="admin">
    
  46.                         <filename>index.html</filename>
    
  47.                         <filename>helloworld.php</filename>
    
  48.                         <filename>controller.php</filename>
    
  49.                         <folder>sql</folder>
    
  50.                         <folder>tables</folder>
    
  51.                         <folder>models</folder>
    
  52.                         <folder>views</folder>
    
  53.                         <folder>language</folder>
    
  54.                 </files>
    
  55.         </administration>
    
  56.  
    
  57. </extension>
    

Testing your component

For details on how to install the component into your Joomla! site, refer to the information provided in Part 01.

In order to see if this component works, make sure all the language strings are properly translated both in the frontend and backend.

File listing

Download this part

Download example package

Articles in this series

This tutorial is supported by the following versions of Joomla!

Joomla 2.5