From 4b3749440c560da36b7deadcee65133062fd10a1 Mon Sep 17 00:00:00 2001 From: brettp Date: Wed, 3 Nov 2010 18:40:58 +0000 Subject: Refs #1320: elgg_echo() has built-in sprintf support. git-svn-id: http://code.elgg.org/elgg/trunk@7226 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/languages.php | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/engine/lib/languages.php b/engine/lib/languages.php index 3472d0d29..d34a89707 100644 --- a/engine/lib/languages.php +++ b/engine/lib/languages.php @@ -86,15 +86,29 @@ function get_language() { * Given a message shortcode, returns an appropriately translated full-text string * * @param string $message_key The short message code + * @param array $args An array of arguments to pass through vsprintf(). * @param string $language Optionally, the standard language code * (defaults to site/user default, then English) * - * @return string Either the translated string or the original English string + * @return string Either the translated string, the English string, + * or the original language string. */ -function elgg_echo($message_key, $language = "") { +function elgg_echo($message_key, $args = array(), $language = "") { global $CONFIG; static $CURRENT_LANGUAGE; + + // old param order is deprecated + if (!is_array($args)) { + elgg_deprecated_notice( + 'As of Elgg 1.8, the 2nd arg to elgg_echo() is an array of string replacements and the 3rd arg is the language.', + 1.8 + ); + + $language = $args; + $args = array(); + } + if (!$CURRENT_LANGUAGE) { $CURRENT_LANGUAGE = get_language(); } @@ -103,12 +117,20 @@ function elgg_echo($message_key, $language = "") { } if (isset($CONFIG->translations[$language][$message_key])) { - return $CONFIG->translations[$language][$message_key]; + $string = $CONFIG->translations[$language][$message_key]; } else if (isset($CONFIG->translations["en"][$message_key])) { - return $CONFIG->translations["en"][$message_key]; + $string = $CONFIG->translations["en"][$message_key]; + } else { + $string = $message_key; + } + + // only pass through if we have arguments to allow backward compatibility + // with manual sprintf() calls. + if ($args) { + $string = vsprintf($string, $args); } - return $message_key; + return $string; } /** -- cgit v1.2.3