Commit 3c7e6aa1 authored by Nigel McNie's avatar Nigel McNie Committed by Nigel McNie
Browse files
parents bcc14ffa 0f202abe
// Expects strings array
function get_string(s) {
// @todo Still need to sprintf these strings.
var flatargs = flattenArguments(arguments);
if (arguments.length > 1) {
argstr = '(' + flatargs.slice(1).join(',') + ')';
} else {
argstr = '';
}
var args = flattenArguments(arguments).slice(1);
if (typeof(strings) == 'undefined' || typeof(strings[s]) == 'undefined') {
return '[[[' + s + argstr + ']]]';
return '[[[' + s + ((args.length > 0) ? ('(' + args.join(',') + ')') : '') + ']]]';
}
var str = strings[s];
// @todo Need to sprintf these strings properly.
for (var i = 0; i < args.length; i++) {
str = str.replace('%s',args[i]);
}
return strings[s] + argstr;
return str;
}
// Appends a status message to the end of elemid
......@@ -39,7 +38,7 @@ function testRequired(e,formid) {
var labels = getElementsByTagAndClassName('label',null,formid);
for (var j = 0; j < labels.length; j++) {
if (getNodeAttribute(labels[j],'for') == e.name) {
displayMessage({'message':get_string('requiredfieldempty',scrapeText(labels[j])),
displayMessage({'message':get_string('namedfieldempty',scrapeText(labels[j])),
'type':'error'});
return false;
}
......
......@@ -55,6 +55,7 @@ $string['pagename'] = 'Page name';
$string['pagetext'] = 'Page text';
// mahara.js
$string['namedfieldempty'] = 'The required field "%s" is empty';
$string['processingform'] = 'Processing form';
$string['requiredfieldempty'] = 'A required field is empty';
$string['unknownerror'] = 'An unknown error occurred (0x20f91a0)';
......
......@@ -142,15 +142,6 @@ function ensure_internal_plugins_exist() {
function get_string($identifier, $section='mahara') {
$langconfigstrs = array('parentlanguage', 'strftimedate', 'strftimedateshort', 'strftimedatetime',
'strftimedaydate', 'strftimedaydatetime', 'strftimedayshort', 'strftimedaytime',
'strftimemonthyear', 'strftimerecent', 'strftimerecentfull', 'strftimetime',
'thislanguage');
if (in_array($identifier, $langconfigstrs)) {
$section = 'langconfig';
}
$variables = func_get_args();
if (count($variables) > 2) { // we have some stuff we need to sprintf
array_shift($variables);
......@@ -160,6 +151,40 @@ function get_string($identifier, $section='mahara') {
$variables = array();
}
return get_string_location($identifier, $section, $variables);
}
// get a string without sprintfing it.
function get_raw_string($identifier, $section='mahara') {
// For a raw string we don't want to format any arguments using
// sprintf, so the replace function passed to get_string_location
// should just return the first argument and ignore the second.
return get_string_location($identifier, $section, array(), create_function('$a,$b','return $a;'));
}
/**
* This function gets a language string identified by $identifier from
* an appropriate location, and formats the string and any arguments
* in $variables using the function $replacefunc.
*
* @param string $identifier
* @param string $section
* @param array $variables
* @param function $replacefunc
* @return string
*/
function get_string_location($identifier, $section, $variables, $replacefunc='format_langstring') {
$langconfigstrs = array('parentlanguage', 'strftimedate', 'strftimedateshort', 'strftimedatetime',
'strftimedaydate', 'strftimedaydatetime', 'strftimedayshort', 'strftimedaytime',
'strftimemonthyear', 'strftimerecent', 'strftimerecentfull', 'strftimetime',
'thislanguage');
if (in_array($identifier, $langconfigstrs)) {
$section = 'langconfig';
}
$lang = current_language();
// Define the locations of language strings for this section
......@@ -185,7 +210,7 @@ function get_string($identifier, $section='mahara') {
$langfile = $location . $lang . '/' . $section . '.php';
if (is_readable($langfile)) {
if ($result = get_string_from_file($identifier, $langfile)) {
return format_langstring($result, $variables);
return $replacefunc($result, $variables);
}
}
}
......@@ -197,7 +222,6 @@ function get_string($identifier, $section='mahara') {
}
// Is a parent language defined? If so, try to find this string in a parent language file
foreach ($locations as $location) {
$langfile = $location . $lang . '/langconfig.php';
if (is_readable($langfile)) {
......@@ -205,7 +229,7 @@ function get_string($identifier, $section='mahara') {
$langfile = $location . $parentlang . '/' . $section . '.php';
if (is_readable($langfile)) {
if ($result = get_string_from_file($identifier, $langfile)) {
return format_langstring($result, $variables);
return $replacefunc($result, $variables);
}
}
}
......@@ -218,7 +242,7 @@ function get_string($identifier, $section='mahara') {
$langfile = $location . 'en.utf8/' . $section . '.php';
if (is_readable($langfile)) {
if ($result = get_string_from_file($identifier, $langfile)) {
return format_langstring($result, $variables);
return $replacefunc($result, $variables);
}
}
}
......@@ -227,7 +251,6 @@ function get_string($identifier, $section='mahara') {
}
/**
* This function is only used from {@link get_string()}.
*
......
......@@ -87,7 +87,7 @@ function &smarty($javascript = array(), $headers = array(), $strings = array())
}
if (!empty($strings)) {
foreach ($strings as &$string) {
$string = '"' . $string . '":"' . addslashes(get_string($string)) . '"';
$string = '"' . $string . '":"' . addslashes(get_raw_string($string)) . '"';
}
$stringjs = '<script language="javascript" type="text/javascript">';
$stringjs .= 'var strings={' . implode(',', $strings) . '};';
......@@ -123,7 +123,8 @@ function &smarty($javascript = array(), $headers = array(), $strings = array())
}
function maharajsstrings() {
return array('processingform',
return array('namedfieldempty',
'processingform',
'requiredfieldempty',
'unknownerror');
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment