Merge branch 'MDL-49240-master' of git://github.com/damyon/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2015-03-03 01:11:16 +01:00
commit fbc4839a0c
2 changed files with 19 additions and 11 deletions

View file

@ -108,11 +108,12 @@ class core_external extends external_api {
* @return string * @return string
* @since Moodle 2.4 * @since Moodle 2.4
*/ */
public static function get_string($stringid, $component = 'moodle', $stringparams = array()) { public static function get_string($stringid, $component = 'moodle', $lang = null, $stringparams = array()) {
$params = self::validate_parameters(self::get_string_parameters(), $params = self::validate_parameters(self::get_string_parameters(),
array('stringid'=>$stringid, 'component' => $component, 'stringparams' => $stringparams)); array('stringid'=>$stringid, 'component' => $component, 'lang' => $lang, 'stringparams' => $stringparams));
return get_string($params['stringid'], $params['component'], $stringmanager = get_string_manager();
return $stringmanager->get_string($params['stringid'], $params['component'],
core_external::format_string_parameters($params['stringparams']), $params['lang']); core_external::format_string_parameters($params['stringparams']), $params['lang']);
} }
@ -163,11 +164,12 @@ class core_external extends external_api {
public static function get_strings($strings) { public static function get_strings($strings) {
$params = self::validate_parameters(self::get_strings_parameters(), $params = self::validate_parameters(self::get_strings_parameters(),
array('strings'=>$strings)); array('strings'=>$strings));
$stringmanager = get_string_manager();
$translatedstrings = array(); $translatedstrings = array();
foreach($params['strings'] as $string) { foreach($params['strings'] as $string) {
if (empty($string['lang'])) { if (!empty($string['lang'])) {
$lang = $string['lang']; $lang = $string['lang'];
} else { } else {
$lang = current_language(); $lang = current_language();
@ -177,7 +179,7 @@ class core_external extends external_api {
'stringid' => $string['stringid'], 'stringid' => $string['stringid'],
'component' => $string['component'], 'component' => $string['component'],
'lang' => $lang, 'lang' => $lang,
'string' => get_string($string['stringid'], $string['component'], 'string' => $stringmanager->get_string($string['stringid'], $string['component'],
core_external::format_string_parameters($string['stringparams']), $lang)); core_external::format_string_parameters($string['stringparams']), $lang));
} }

View file

@ -41,7 +41,7 @@ class core_external_testcase extends externallib_advanced_testcase {
$service->id = 12; $service->id = 12;
// String with two parameters. // String with two parameters.
$returnedstring = core_external::get_string('addservice', 'webservice', $returnedstring = core_external::get_string('addservice', 'webservice', null,
array(array('name' => 'name', 'value' => $service->name), array(array('name' => 'name', 'value' => $service->name),
array('name' => 'id', 'value' => $service->id))); array('name' => 'id', 'value' => $service->id)));
@ -53,7 +53,7 @@ class core_external_testcase extends externallib_advanced_testcase {
// String with one parameter. // String with one parameter.
$acapname = 'A capability name'; $acapname = 'A capability name';
$returnedstring = core_external::get_string('missingrequiredcapability', 'webservice', $returnedstring = core_external::get_string('missingrequiredcapability', 'webservice', null,
array(array('value' => $acapname))); array(array('value' => $acapname)));
// We need to execute the return values cleaning process to simulate the web service server. // We need to execute the return values cleaning process to simulate the web service server.
@ -73,7 +73,7 @@ class core_external_testcase extends externallib_advanced_testcase {
// String with two parameter but one is invalid (not named). // String with two parameter but one is invalid (not named).
$this->setExpectedException('moodle_exception'); $this->setExpectedException('moodle_exception');
$returnedstring = core_external::get_string('addservice', 'webservice', $returnedstring = core_external::get_string('addservice', 'webservice', null,
array(array('value' => $service->name), array(array('value' => $service->name),
array('name' => 'id', 'value' => $service->id))); array('name' => 'id', 'value' => $service->id)));
} }
@ -84,6 +84,8 @@ class core_external_testcase extends externallib_advanced_testcase {
public function test_get_strings() { public function test_get_strings() {
$this->resetAfterTest(true); $this->resetAfterTest(true);
$stringmanager = get_string_manager();
$service = new stdClass(); $service = new stdClass();
$service->name = 'Dummy Service'; $service->name = 'Dummy Service';
$service->id = 12; $service->id = 12;
@ -94,16 +96,20 @@ class core_external_testcase extends externallib_advanced_testcase {
'stringid' => 'addservice', 'component' => 'webservice', 'stringid' => 'addservice', 'component' => 'webservice',
'stringparams' => array(array('name' => 'name', 'value' => $service->name), 'stringparams' => array(array('name' => 'name', 'value' => $service->name),
array('name' => 'id', 'value' => $service->id) array('name' => 'id', 'value' => $service->id)
) ),
'lang' => 'en'
), ),
array('stringid' => 'addaservice', 'component' => 'webservice') array('stringid' => 'addaservice', 'component' => 'webservice', 'lang' => 'en')
)); ));
// We need to execute the return values cleaning process to simulate the web service server. // We need to execute the return values cleaning process to simulate the web service server.
$returnedstrings = external_api::clean_returnvalue(core_external::get_strings_returns(), $returnedstrings); $returnedstrings = external_api::clean_returnvalue(core_external::get_strings_returns(), $returnedstrings);
foreach($returnedstrings as $returnedstring) { foreach($returnedstrings as $returnedstring) {
$corestring = get_string($returnedstring['stringid'], $returnedstring['component'], $service); $corestring = $stringmanager->get_string($returnedstring['stringid'],
$returnedstring['component'],
$service,
'en');
$this->assertSame($corestring, $returnedstring['string']); $this->assertSame($corestring, $returnedstring['string']);
} }
} }