4885: Message.printAbstract() should indicate when additional message parameters... issue_4885
authorTJ <acontent@iam.tj>
Sun, 9 Oct 2011 15:33:57 +0000 (16:33 +0100)
committerTJ <acontent@iam.tj>
Wed, 19 Oct 2011 19:07:41 +0000 (20:07 +0100)
include/classes/Message/Message.class.php

index ad4f5ac..1f36ebf 100644 (file)
@@ -97,16 +97,30 @@ class Message {
                        
                                /* this is an array with terms to replace */
                                $first = array_shift($item);
-                               $result = _AT($first); // lets translate the code
-                               
-                               if ($result == '') { // if the code is not in the db lets just print out the code for easier trackdown
-                                       $result = '[' . $first . ']';
+                               $format_specifier = _AT($first); // lets translate the code
+
+                               if ($format_specifier == '') { // if the code is not in the db lets just print out the code for easier trackdown
+                                       $format_specifier = '[' . $first . ']';
+                               }
+
+                               /* replace the format-specifier tokens in DB language_text.text field with the terms */
+                               $result = vsprintf($format_specifier, $item);
+                               /* Improve code quality. Detect specifiers that are not used.
+                                  This will highlight language_text.term entries that are missing format-specifiers */
+                               $match_fs = array();
+                               $count_fs = preg_match_all('/[^\d]%[^%]|\s]/', $format_specifier, $match_fs);
+                               $count_item = count($item);
+                               if (strcmp($result, $format_specifier) == 0 || $count_item != $count_fs) {
+                                       // $item array has elements that appear to have been ignored so give a warning
+                                       trigger_error('ERROR: language_text term '.$first.' is missing one or more format specifiers. Expected '
+                                        .$count_item.' but got '.$count_fs);
+                                       // now add the missed elements to the message
+                                       $idx = 0;
+                                       foreach($item as $element) {
+                                           if ($idx++ >= $count_fs)
+                                                       $result .= ' ' . $element;
+                                       }
                                }
-                                                                               
-                               $terms = $item;
-                       
-                               /* replace the tokens with the terms */
-                               $result = vsprintf($result, $terms);
                                
                        } else {
                                $result = _AT($item);
@@ -469,4 +483,4 @@ class Message {
 
 
 
-?>
\ No newline at end of file
+?>