diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index 4dad33a..061028e 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -1169,15 +1169,7 @@ function drupal_unpack($obj, $field = 'data') { * A string containing the English string to translate. * @param $args * An associative array of replacements to make after translation. - * Occurrences in $string of any key in $args are replaced with the - * corresponding value, after sanitization. The sanitization function depends - * on the first character of the key: - * - !variable: Inserted as is. Use this for text that has already been - * sanitized. - * - @variable: Escaped to HTML using check_plain(). Use this for anything - * displayed on a page on the site. - * - %variable: Escaped as a placeholder for user-submitted content using - * drupal_placeholder(), which shows up as emphasized text. + * See drupal_replace_string(). * @param $options * An associative array of additional options, with the following elements: * - 'langcode' (defaults to the current language): The language code to @@ -1223,26 +1215,47 @@ function t($string, array $args = array(), array $options = array()) { return $string; } else { - // Transform arguments before inserting them. - foreach ($args as $key => $value) { - switch ($key[0]) { - case '@': - // Escaped only. - $args[$key] = check_plain($value); - break; - - case '%': - default: - // Escaped and placeholder. - $args[$key] = drupal_placeholder($value); - break; + return drupal_replace_string($string, $args); + } +} - case '!': - // Pass-through. - } +/** + * Replace placeholders with sanitized values in a string. + * + * @param $string + * A string containing placeholders. + * @param $args + * An associative array of replacements to make. Occurrences in $string of + * any key in $args are replaced with the corresponding value, after + * sanitization. The sanitization function depends on the first character of + * the key: + * - !variable: Inserted as is. Use this for text that has already been + * sanitized. + * - @variable: Escaped to HTML using check_plain(). Use this for anything + * displayed on a page on the site. + * - %variable: Escaped as a placeholder for user-submitted content using + * drupal_placeholder(), which shows up as emphasized text. + */ +function drupal_replace_string($string, $args) { + // Transform arguments before inserting them. + foreach ($args as $key => $value) { + switch ($key[0]) { + case '@': + // Escaped only. + $args[$key] = check_plain($value); + break; + + case '%': + default: + // Escaped and placeholder. + $args[$key] = drupal_placeholder($value); + break; + + case '!': + // Pass-through. } - return strtr($string, $args); } + return strtr($string, $args); } /** diff --git a/misc/drupal.js b/misc/drupal.js index 3cebbd2..b17c4de 100644 --- a/misc/drupal.js +++ b/misc/drupal.js @@ -126,6 +126,42 @@ Drupal.checkPlain = function (str) { }; /** + * Replace placeholders with sanitized values in a string. + * + * @param str + * A string with placeholders. + * @param args + * An object of replacements pairs to make. Incidences of any key in this + * array are replaced with the corresponding value. Based on the first + * character of the key, the value is escaped and/or themed: + * - !variable: inserted as is + * - @variable: escape plain text to HTML (Drupal.checkPlain) + * - %variable: escape text and theme as a placeholder for user-submitted + * content (checkPlain + Drupal.theme('placeholder')) + */ +Drupal.replaceString = function(str, args) { + // Transform arguments before inserting them. + for (var key in args) { + switch (key.charAt(0)) { + // Escaped only. + case '@': + args[key] = Drupal.checkPlain(args[key]); + break; + // Pass-through. + case '!': + break; + // Escaped and placeholder. + case '%': + default: + args[key] = Drupal.theme('placeholder', args[key]); + break; + } + str = str.replace(key, args[key]); + } + return str; +} + +/** * Translate strings to the page language or a given language. * * See the documentation of the server-side t() function for further details. @@ -135,11 +171,7 @@ Drupal.checkPlain = function (str) { * @param args * An object of replacements pairs to make after translation. Incidences * of any key in this array are replaced with the corresponding value. - * Based on the first character of the key, the value is escaped and/or themed: - * - !variable: inserted as is - * - @variable: escape plain text to HTML (Drupal.checkPlain) - * - %variable: escape text and theme as a placeholder for user-submitted - * content (checkPlain + Drupal.theme('placeholder')) + * See Drupal.replaceString(). * @return * The translated string. */ @@ -150,24 +182,7 @@ Drupal.t = function (str, args) { } if (args) { - // Transform arguments before inserting them. - for (var key in args) { - switch (key.charAt(0)) { - // Escaped only. - case '@': - args[key] = Drupal.checkPlain(args[key]); - break; - // Pass-through. - case '!': - break; - // Escaped and placeholder. - case '%': - default: - args[key] = Drupal.theme('placeholder', args[key]); - break; - } - str = str.replace(key, args[key]); - } + str = Drupal.replaceString(str, args); } return str; }; @@ -193,11 +208,7 @@ Drupal.t = function (str, args) { * @param args * An object of replacements pairs to make after translation. Incidences * of any key in this array are replaced with the corresponding value. - * Based on the first character of the key, the value is escaped and/or themed: - * - !variable: inserted as is - * - @variable: escape plain text to HTML (Drupal.checkPlain) - * - %variable: escape text and theme as a placeholder for user-submitted - * content (checkPlain + Drupal.theme('placeholder')) + * See Drupal.replaceString(). * Note that you do not need to include @count in this array. * This replacement is done automatically for the plural case. * @return