User

Difference between revisions of "Rvsjoen/tutorial/Developing a Module/Part 04"

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing a Module
Line 1: Line 1:
 
= Adding translation =
 
= Adding translation =
 +
 +
So, we would like our module 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 set of language files.
  
 
With your favorite editor, create the following file
 
With your favorite editor, create the following file
  
<span id="language/en-GB.mod_helloworld.sys.ini">
+
<span id="language/en-GB.mod_helloworld.ini">
'''<tt>language/en-GB.mod_helloworld.sys.ini</tt>'''
+
'''<tt>language/en-GB.mod_helloworld.ini</tt>'''
 
<source lang="ini">
 
<source lang="ini">
 
MOD_HELLOWORLD="Hello World!"
 
MOD_HELLOWORLD="Hello World!"
 
MOD_HELLOWORLD_XML_DESCRIPTION="Hello World! module description"
 
MOD_HELLOWORLD_XML_DESCRIPTION="Hello World! module description"
 +
MOD_HELLOWORLD_GREETING_DEFAULT="I am a module"
 +
MOD_HELLOWORLD_GREETING_LABEL="Greeting"
 +
MOD_HELLOWORLD_GREETING_DESC="The greeting to display"
 
</source>
 
</source>
 
</span>
 
</span>
 +
 +
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
 
With your favorite editor, create the following file
  
<span id="language/en-GB.mod_helloworld.ini">
+
<span id="language/en-GB.mod_helloworld.sys.ini">
'''<tt>language/en-GB.mod_helloworld.ini</tt>'''
+
'''<tt>language/en-GB.mod_helloworld.sys.ini</tt>'''
 
<source lang="ini">
 
<source lang="ini">
 
MOD_HELLOWORLD="Hello World!"
 
MOD_HELLOWORLD="Hello World!"
 
MOD_HELLOWORLD_XML_DESCRIPTION="Hello World! module description"
 
MOD_HELLOWORLD_XML_DESCRIPTION="Hello World! module description"
MOD_HELLOWORLD_GREETING_DEFAULT="I am a module"
 
MOD_HELLOWORLD_GREETING_LABEL="Greeting"
 
MOD_HELLOWORLD_GREETING_DESC="The greeting to display"
 
 
</source>
 
</source>
 
</span>
 
</span>

Revision as of 12:03, 13 March 2012

Adding translation[edit]

So, we would like our module 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 set of language files.

With your favorite editor, create the following file

language/en-GB.mod_helloworld.ini

MOD_HELLOWORLD="Hello World!"
MOD_HELLOWORLD_XML_DESCRIPTION="Hello World! module description"
MOD_HELLOWORLD_GREETING_DEFAULT="I am a module"
MOD_HELLOWORLD_GREETING_LABEL="Greeting"
MOD_HELLOWORLD_GREETING_DESC="The greeting to display"

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

language/en-GB.mod_helloworld.sys.ini

MOD_HELLOWORLD="Hello World!"
MOD_HELLOWORLD_XML_DESCRIPTION="Hello World! module description"

With your favorite editor, edit the following file

tmpl/default.php

<?php 

/**
 * @package	Joomla.Tutorials
 * @subpackage	Module
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license	License GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die;
?>

<p><?php echo $params->get('greeting', JText::_('MOD_HELLOWORLD_GREETING_DEFAULT')); ?></p>

With your favorite editor, edit the following file

mod_helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="2.5.0" method="upgrade">

	<name>MOD_HELLOWORLD</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>Once upon a time</creationDate>
	<author>John Doe</author>
	<authorEmail>john.doe@example.org</authorEmail>
	<authorUrl>http://www.example.org</authorUrl>
	<copyright>Copyright Info</copyright>
	<license>License Info</license>
	<!--  The version string is stored in the extension table -->
	<version>0.0.4</version>
	<!-- The description is optional and defaults to the name -->
	<description>MOD_HELLOWORLD_XML_DESCRIPTION</description>

	<!-- Note the folder attribute: This attribute describes what to copy
		into the module folder -->
	<files>
		<filename module="mod_helloworld">mod_helloworld.php</filename>
		<filename>mod_helloworld.xml</filename>
		<folder>tmpl</folder>
		<folder>language</folder>
	</files>

	<config>
		<fields name="params">
			<fieldset name="basic">
				<field
					name="greeting"
					type="text"
					default="MOD_HELLOWORLD_GREETING_DEFAULT"
					label="MOD_HELLOWORLD_GREETING_LABEL"
					description="MOD_HELLOWORLD_GREETING_DESC"
				/>
			</fieldset>
		</fields>
	</config>

</extension>

In the XML manifest we have replaced all the strings with language keys which you already added to the language files. This makes it possible to provide different strings for different languages. In addition in the highlighted line we have to remember to tell the installer to copy the language files into the module directory during installation.

File listing[edit]

Testing your component[edit]

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

Download this part[edit]

To be continued...

Articles in this series[edit]

This tutorial is supported by the following versions of Joomla!

Joomla 2.5