Modules Tutorial Part 2

In part 1 you learned how to create a basic module for Redaxscript. Part 2 explains Redaxscript's language system and uses it to provide a multi-lingual interface for your module.

This part uses the "example2" module from the redaxscript-third-party repository on GitHub. The folder and file organisation is the same as example1, but with the addition of a language folder containing a php file for each language supported. Since all the language strings for the module are contained in a single file (per language) it is quite easy for someone to provide a translation for your module.

  • modules
    • example2
      • index.php
      • install.php
      • languages
        • de.php
        • en.php
        • fr.php


The install.php file is the same as for example 1 apart from the name of the module and the description. Normally when developing a module the install.php file doesn't have to change. Note that the version field is the version of Redaxscript that the module is designed for, not the version of the module!

/** * example2 install * * @since 2.0.0 * @deprecated 2.0.0 * * @package Redaxscript Third Party * @category Modules * @author Gary Aylward */ function example2_install() { $query = 'INSERT INTO ' . PREFIX . 'modules (name, alias, author, description, version, status, access) VALUES ('Second example module', 'example2', 'GaryA', 'Mult-lingual hello world', '2.0.0', 1, 0)'; mysql_query($query); } /** * example2 uninstall * * @since 2.0.0 * @deprecated 2.0.0 * * @package Redaxscript Third Party * @category Modules * @author Gary Aylward */ function example2_uninstall() { $query = 'DELETE FROM ' . PREFIX . 'modules WHERE alias = 'example2' LIMIT 1'; mysql_query($query); }

Language Files

Language files simply define a number of elements of the Redaxscript language associative array (simply called "l"). The array keys for the module must all be prefixed with the module name so that they do not interfere with the core language files or any other module.

As part of the page load function, Redaxscript builds the language array for the user's current language. The language strings are then available to all php functions. Redaxscript also creates a javascript object containing the language strings so that javascript functions can take advantage of the translations.

For this example, we only have two strings to translate so the language files are very short. You must ensure that all language files include translations for every string.


/* example2 */ $l['example2_hello'] = 'Hello'; $l['example2_world'] = 'world';


/* example2 */ $l['example2_hello'] = 'Hallo'; $l['example2_world'] = 'zusammen';


/* example2 */ $l['example2_hello'] = 'Bonjour'; $l['example2_world'] = 'tout le monde';


The simple hello world function doesn't change much from example1. The hard-coded strings are replaced by the appropriate language array elements. We also add an excalamation mark from the core language files.

/** * example2 * * @since 2.0.0 * @deprecated 2.0.0 * * @package Redaxscript Third Party * @category Modules * @author Gary Aylward * * @param string $name * @return string */ function example2($name = 'world') { if ($name === 'world') { $name = l('example2_world'); } $output = '<span>' . l('example2_hello') . ' ' . $name . l('exclamation_mark') . '</span>'; return $output; }

That's all there is to adding translation to a module. To test the translation, you will have to set the language that Redaxscript uses to something other than "Detect". Otherwise you will only be able to see your own language.

You can change the page language temporarily by adding a language code to the end of the URL. You can try this out with the Examples Modules page on this site:
English: redaxscript-example-modules.en
Be aware that changing the language seems to persist (probably for the session - I haven't looked into it), and it applies to all the admin buttons and messages!

Alternatively, log in to the admin section and select System -> Settings -> General then select a language from the drop-down list.

In part three we will add some custom styling to our module...


12.10.2013 at 18:53
You can also switch the language by adding .en, .de or .fr to the current [url] - same with .template[name] :-)


12.10.2013 at 19:06
Thanks for the reminder Henry, I've added it to the tutorial.