* @param mixed $a An object, string or number that can be used within translation strings
*/
function print_string($identifier, $module='', $a=NULL) {
- echo string_manager::instance()->get_string($identifier, $module, $a);
+ echo get_string_manager()->get_string($identifier, $module, $a);
}
/**
- * Singleton class for managing the search for language strings.
- *
- * Most code should not use this class directly. Instead you should use the
- * {@link get_string()} function.
+ * Returns current string_manager instance.
+ * @return string_manager
+ */
+function get_string_manager() {
+ global $CFG;
+
+ static $singleton = null;
+
+ // TODO: here will be some switching code for other new string managers
+
+ if ($singleton === null) {
+ $singleton = new legacy_string_manager($CFG->dirroot, $CFG->dataroot, !empty($CFG->running_installer), !empty($CFG->debugstringids));
+ // Uncomment the followign line to log every call to get_string
+ // to a file in $CFG->dataroot/temp/getstringlog/...
+ // $singleton->start_logging();
+ }
+
+ return $singleton;
+}
+
+/**
+ * Interface describing class which is responsible for getting
+ * of localised strings.
+ */
+interface string_manager {
+ /**
+ * Mega Function - Get String returns a requested string
+ *
+ * @param string $identifier The identifier of the string to search for
+ * @param string $component The module the string is associated with
+ * @param string $a An object, string or number that can be used
+ * within translation strings
+ * @return string The String !
+ */
+ public function get_string($identifier, $component='', $a=NULL);
+
+ /**
+ * Returns a list of country names in the current language
+ * @return array two-letter country code => translated name.
+ */
+ public function get_list_of_countries();
+
+ /**
+ * TODO: this will be probably removed soon
+ */
+ public function get_registered_plugin_types();
+
+ /**
+ * TODO: will be removed really soon
+ */
+ public function find_help_file($file, $module, $forcelang, $skiplocal);
+}
+
+/**
+ * No code should use this class directly. Instead you should use the
+ * {@link get_string()} function. Core code has to use {@link get_string_manage()}
+ * to get the instance.
*
* Notes for develpers
* ===================
*
* In some cases (for example bootstrap_renderer::early_error) get_string gets
* called very early on during Moodle's self-initialisation. Think very carefully
- * before relying on the normal Moodle libraries here.
+ * before relying on the normal Moodle libraries.
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package moodlecore
*/
-class string_manager {
+class legacy_string_manager implements string_manager {
private $parentlangs = array('en_utf8' => NULL);
private $searchpathsformodule = array();
private $strings = array();
private $logtofile = false;
private $showstringsource = false;
private $allcountrycodes = NULL;
- private static $singletoninstance = NULL;
-
- /**
- * Creates a new instance of string_manager
- *
- * @global object
- * @return string_manager Returns a new instance of string_manager
- */
- public static function instance() {
- if (is_null(self::$singletoninstance)) {
- global $CFG;
- self::$singletoninstance = new self($CFG->dirroot, $CFG->dataroot,
- isset($CFG->running_installer), !empty($CFG->debugstringids));
- // Uncomment the followign line to log every call to get_string
- // to a file in $CFG->dataroot/temp/getstringlog/...
- // self::$singletoninstance->start_logging();
- }
- return self::$singletoninstance;
- }
// Some of our arrays need $CFG.
/**
* @param boolean $showstringsource add debug info to each string before it is
* returned, to say where it came from.
*/
- protected function __construct($dirroot, $dataroot, $runninginstaller, $showstringsource = false) {
+ public function __construct($dirroot, $dataroot, $runninginstaller, $showstringsource = false) {
$this->dirroot = $dirroot;
$this->corelocations = array(
$dirroot . '/lang/' => '',
return array($type, $plugin);
}
- /**
- * An deprecated function to allow plugins to load thier language strings
- * from the plugin dir
- *
- * @deprecated Deprecated function if no longer used remove
- * @todo Remove deprecated function when no longer used
- *
- * @param array $locations Array of existing locations
- * @param array $extralocations Array or new locations to add
- * @return array Array of combined locations
- */
- protected function add_extra_locations($locations, $extralocations) {
- // This is an old, deprecated mechanism that predates the
- // current mechanism that lets plugins include their lang strings in the
- // plugin folder. So tell people who use it to change.
- debugging('The fourth, $extralocations parameter to get_string is deprecated. ' .
- 'See http://docs.moodle.org/en/Development:Places_to_search_for_lang_strings ' .
- 'for a better way to package language strings with your plugin.', DEBUG_DEVELOPER);
- if (is_array($extralocations)) {
- $locations = array_merge($locations, $extralocations);
- } else if (is_string($extralocations)) {
- $locations[] = $extralocations;
- } else {
- debugging('Bad lang path provided');
- }
- return $locations;
- }
-
/**
* Get the path to the parent lanugage file of a given language
*
* @param string $module The module the string is associated with
* @param string $a An object, string or number that can be used
* within translation strings
- * @param array extralocations Add extra locations --- Deprecated ---
* @return string The String !
*/
- public function get_string($identifier, $module='', $a=NULL, $extralocations=NULL) {
+ public function get_string($identifier, $module='', $a=NULL) {
if ($this->logtofile) {
$this->log_get_string_call($identifier, $module, $a);
}
$locations = array_merge(array($this->dirroot . '/install/lang/' => ''), $locations);
}
- if ($extralocations) {
- $locations = $this->add_extra_locations($locations, $extralocations);
- }
-
/// Now do the search.
for ($lang = current_language(); $lang; $lang = $this->get_parent_language($lang)) {
foreach ($locations as $location => $ignored) {
* @return string The localized string.
*/
function get_string($identifier, $module='', $a=NULL, $extralocations=NULL) {
- return string_manager::instance()->get_string($identifier, $module, $a, $extralocations);
+ if ($extralocations !== NULL) {
+ debugging('extralocations parameter in get_string() is not supported any more, please use standard lang locations only.');
+ }
+
+ return get_string_manager()->get_string($identifier, $module, $a);
}
/**
function get_strings($array, $module='') {
$string = new stdClass;
foreach ($array as $item) {
- $string->$item = string_manager::instance()->get_string($item, $module);
+ $string->$item = get_string_manager()->get_string($item, $module);
}
return $string;
}
* @return array two-letter country code => translated name.
*/
function get_list_of_countries() {
- return string_manager::instance()->get_list_of_countries();
+ return get_string_manager()->get_list_of_countries();
}
/**