Skip to content

Commit

Permalink
MDL-22015 AMOS - making room for new string manager
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Apr 1, 2010
1 parent c80e7a1 commit ef330d9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 72 deletions.
4 changes: 2 additions & 2 deletions admin/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ function lang_standard_locations() {
function lang_extra_locations() {
global $CFG;
$files = array();
$places = string_manager::instance()->get_registered_plugin_types();
$places = get_string_manager()->get_registered_plugin_types();
foreach ($places as $prefix => $directories) {
foreach ($directories as $directory) {
foreach (get_list_of_plugins($directory) as $plugin) {
Expand Down Expand Up @@ -1295,7 +1295,7 @@ function lang_help_standard_locations() {
function lang_help_extra_locations() {
global $CFG;
$files = array();
$places = string_manager::instance()->get_registered_plugin_types();
$places = get_string_manager()->get_registered_plugin_types();
foreach ($places as $prefix => $directories) {
foreach ($directories as $directory) {
foreach (get_list_of_plugins($directory) as $plugin) {
Expand Down
4 changes: 2 additions & 2 deletions help.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

if (!empty($file)) {
// The help to display is from a help file.
list($filepath, $foundlang) = string_manager::instance()->find_help_file($file, $module, $forcelang, $skiplocal);
list($filepath, $foundlang) = get_string_manager()->find_help_file($file, $module, $forcelang, $skiplocal);

if ($filepath) {
$helpfound = true;
Expand Down Expand Up @@ -171,7 +171,7 @@ function include_help_for_each_module($file, $forcelang, $skiplocal) {
ksort($modulebyname, SORT_LOCALE_STRING);

foreach ($modulebyname as $mod) {
list($filepath, $foundlang) = string_manager::instance()->find_help_file($file, $mod->name, $forcelang, $skiplocal);
list($filepath, $foundlang) = get_string_manager()->find_help_file($file, $mod->name, $forcelang, $skiplocal);
if ($filepath) {
echo '<hr />';
include($filepath);
Expand Down
133 changes: 69 additions & 64 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5686,14 +5686,67 @@ function get_parent_language($lang=null) {
* @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
* ===================
Expand All @@ -5703,12 +5756,12 @@ function print_string($identifier, $module='', $a=NULL) {
*
* 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();
Expand All @@ -5730,25 +5783,6 @@ class string_manager {
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.
/**
Expand All @@ -5761,7 +5795,7 @@ public static function instance() {
* @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/' => '',
Expand Down Expand Up @@ -5869,34 +5903,6 @@ protected function parse_module_name($module) {
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
*
Expand Down Expand Up @@ -6050,10 +6056,9 @@ private function log_get_string_call($identifier, $module, $a) {
* @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);
}
Expand All @@ -6078,10 +6083,6 @@ public function get_string($identifier, $module='', $a=NULL, $extralocations=NUL
$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) {
Expand Down Expand Up @@ -6226,7 +6227,11 @@ public function get_list_of_countries() {
* @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);
}

/**
Expand All @@ -6239,7 +6244,7 @@ function get_string($identifier, $module='', $a=NULL, $extralocations=NULL) {
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;
}
Expand Down Expand Up @@ -6401,7 +6406,7 @@ function get_list_of_charsets() {
* @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();
}

/**
Expand Down
5 changes: 1 addition & 4 deletions lib/simpletest/teststringmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
/**
* Test subclass that makes all the protected methods we want to test pubic.
*/
class testable_string_manager extends string_manager {
public function __construct($dirroot, $dataroot, $runninginstaller) {
parent::__construct($dirroot, $dataroot, $runninginstaller);
}
class testable_string_manager extends legacy_string_manager {
public function locations_to_search($module) {
return parent::locations_to_search($module);
}
Expand Down

0 comments on commit ef330d9

Please sign in to comment.