Standard plugin features

Download the plugin from GitHib. Now, let's take a look at the structure of the duallang plugin together. There are three files worth mentioning:

  • version.php: This contains not only the version information of the plugin itself but also the minimum version of Moodle required to run it. It also, optionally, contains a list of dependencies. The following is a copy of the relevant code from our new plugin:
      $plugin->component = 'local_duallang'; 
$plugin->release = 'alpha1';
$plugin->version = 2016112300;
$plugin->requires = 2015030900;
$plugin->maturity = MATURITY_ALPHA;
$plugin->dependencies = array();
  • lang/en/local_duallang.php: This contains the plugin's language strings. There is only one language string required for this basic plugin and that is the name of the plugin itself:
      $string['pluginname'] = 'Dual language string manager'; 
  • classes/duallang_string_manager.php: This script contains the overridden implementation of the core_string_manager class, specifically, the get_string() function, which has been modified to construct a language string from both the UK English and the simplified Chinese Mandarin language packs:
      class duallang_string_manager extends 
\core_string_manager_standard {

/**
* Implementation of the get_string() method to display both
simplified
* Chinese and UK English simultaneously.
*
* @param string $identifier the identifier of the string to
search for
* @param string $component the component the string is
provided by
* @paramstring|object|array $a optional data placeholder
* @param string $langmoodle translation language, null means
use
* current
* @return string
*/
public function get_string($identifier, $component = '', $a =
null,
$lang = null) {

$string = parent::get_string($identifier, $component, $a,
'en');

$zh_cn = parent::get_string($identifier, $component, $a,
'zh_cn');

if(strlen($zh_cn) > 0) {
$string .= ' | ' . $zh_cn;
}

return $string;
}
}

Note that in order to specifically load UK English and simplified Chinese, we simply call the method in the parent class (refer to the An objected-oriented philosophy section for some elaboration on the parent/child class relationship).

At the top of each PHP script, you will find a general comment header, important copyright, and optional version control information. You will also see the following line of code:

defined('MOODLE_INTERNAL') || die(); 

This prevents someone from outside of Moodle from accessing the PHP script. To install the plugin, we can either copy the files to Moodle's local folder or use Moodle's built-in plugin installer (for details, visit https://docs.moodle.org/31/en/Installing_plugins#Installing_via_uploaded_ZIP_file):

Once installed, we will also need to add a line to Moodle's config.php file (located in Moodle's root directory):

Return to Moodle's home page and you will now see the UK English and simplified Chinese Mandarin language strings displayed side by side: