Adding the language files
At the moment, all the text used in our plugin is hardcoded, so if a user wanted to change any of the text, they would have to edit the source code which is something we want to avoid for our end users. A lot of people use Joomla! in their native language and not everyone is going to want the text in English. Even if your plugin is only ever going to be used by English speakers, you still want them to be able to easily override the text to suit their needs. So we are going to create a language file, and instead of hardcoding the text, we will use a language string.
Firstly, we are going to replace the text in the parameters we have just added with language strings. Edit the clicktocall.xml
file, and make the following changes:
<?xml version="1.0" encoding="UTF-8"?> <extension version="3.0" type="plugin" group="content" method="upgrade"> <name>Content - Click To Call</name> <author>Tim Plummer</author> <creationDate>April 2013</creationDate> <copyright>Copyright (C) 2013 Packt Publishing. All rights reserved.</copyright> <license> http://www.gnu.org/licenses/gpl-3.0.html</license> <authorEmail>example@packtpub.com</authorEmail> <authorUrl>http://packtpub.com</authorUrl> <version>1.2.0</version> <description>This plugin will replace phone numbers with click to call links. Requires Joomla! 3.0 or greater. Don't forget to publish this plugin! </description> <files> <filename plugin="clicktocall">clicktocall.php</filename> <filename plugin="clicktocall">index.html</filename> </files> <languages> <language tag="en-GB">language/en-GB/en-GB.plg_content_clicktocall.ini</language> </languages> <config> <fields name="params"> <fieldset name="basic"> <field name="phoneDigits1" type="text" default="4" label="PLG_CONTENT_CLICKTOCALL_FIELD_PHONEDIGITS1_LABEL" description="PLG_CONTENT_CLICKTOCALL_FIELD_PHONEDIGITS1_DESC" /> <field name="phoneDigits2" type="text" default="4" label="PLG_CONTENT_CLICKTOCALL_FIELD_PHONEDIGITS2_LABEL" description="PLG_CONTENT_CLICKTOCALL_FIELD_PHONEDIGITS2_DESC" /> </fieldset> </fields> </config> </extension>
We have incremented the version number since we are adding a new feature, then included the language tags that tells Joomla! to load our language file from the /language/en-GB
folder of our plugin install file and put them into the /administrator/language/en-GB
folder when the extension is installed.
<languages> <language tag="en-GB">language/en-GB/en-GB.plg_content_clicktocall.ini</language> </languages>
The format of the language string is PLG
(because it is a plugin) followed by CONTENT
(because this is a content plugin), followed by the plugin name CLICKTOCALL
, then a short description of what this language string is for. Language strings are always in uppercase and can only contain alphabetic characters and underscores, you can't use spaces.
Note
In Joomla! 1.5, you could use spaces and punctuation in language strings but Joomla! 1.6 changed to using the native PHP ini
parser for language files which is much more strict but loads faster.
Create the file at /administrator/language/en-GB/en-GB.plg_content_clicktocall.ini
which will contain the following language strings:
PLG_CONTENT_CLICKTOCALL_FIELD_PHONEDIGITS1_LABEL="Digits 1" PLG_CONTENT_CLICKTOCALL_FIELD_PHONEDIGITS1_DESC="How many digits in the first part of the phone number?" PLG_CONTENT_CLICKTOCALL_FIELD_PHONEDIGITS2_LABEL="Digits 2" PLG_CONTENT_CLICKTOCALL_FIELD_PHONEDIGITS2_DESC="How many digits in the second part of the phone number?"
Each line of the language file has the languages string followed by an equals sign, then the actual text enclosed in speech marks.
You can now try creating an Administrator language override. Make sure you set the filter to English (United Kingdom) and Administrator before creating the override so that it is for the backend. Override the language string PLG_CONTENT_CLICKTOCALL_FIELD_PHONEDIGITS1_LABEL
and change to Start num of digits
.
As you can see, the text Digits 1 has now been replaced with the override Start num of digits without having to edit any source code. Alternatively, you could try changing the text by editing the file /administrator/language/en-GB/en-GB.plg_content_clicktocall.ini
directly. Obviously you'd need to remove the language override, otherwise that will still take precedence.
Ideally all text displayed by your plugin should use a language string rather than being hardcoded. You will need to zip up your plugin again so the version you distribute will contain these new features. Before you zip it up, you will need to create the folder language
in your plugin folder that you will be zipping, and within that, create an en-GB
folder. Don't forget to include an index.html
file in each folder. Now copy in the language file /administrator/language/en-GB/en-GB.plg_content_clicktocall.ini
in the folder. The folder you are using to create the installable package should now have the following files:
/language/en-GB/en-GB.plg_content_clicktocall.ini
/language/en-GB/index.html
/language/index.html
clicktocall.php
clicktocall.xml
index.html
Zip it up and call this installable package as plg_content_clicktocall_v1.2.0.zip
, and try it out on another Joomla! website. Even though we said this plugin was for Version 3.0 or greater, it will actually install and work perfectly on a Joomla! 2.5 site as all the code we have used is available in both versions, and the Joomla! version number in the installation XML file is not strictly enforced.