Index: trunk/extensions/UsabilityInitiative/UsabilityInitiative.hooks.php |
— | — | @@ -12,6 +12,7 @@ |
13 | 13 | |
14 | 14 | private static $doOutput = false; |
15 | 15 | private static $messages = array(); |
| 16 | + private static $variables = array(); |
16 | 17 | private static $styles = array(); |
17 | 18 | private static $styleFiles = array( |
18 | 19 | 'base_sets' => array( |
— | — | @@ -143,17 +144,32 @@ |
144 | 145 | self::$messages[$i] = |
145 | 146 | "'{$escapedMessageKey}':'{$escapedMessageValue}'"; |
146 | 147 | } |
147 | | - // Converts array of object members to a comma delimited list |
148 | | - $messagesList = implode( ',', self::$messages ); |
149 | 148 | // Add javascript to document |
150 | | - $out->addScript( |
151 | | - Xml::tags( |
152 | | - 'script', |
153 | | - array( 'type' => $wgJsMimeType ), |
154 | | - "loadGM({{$messagesList}});" |
155 | | - ) |
156 | | - ); |
157 | | - |
| 149 | + if ( count( self::$messages ) > 0 ) { |
| 150 | + $out->addScript( |
| 151 | + Xml::tags( |
| 152 | + 'script', |
| 153 | + array( 'type' => $wgJsMimeType ), |
| 154 | + 'loadGM({' . implode( ',', self::$messages ) . '});' |
| 155 | + ) |
| 156 | + ); |
| 157 | + } |
| 158 | + // Transforms variables into javascript global variables |
| 159 | + foreach ( self::$variables as $key => $value ) { |
| 160 | + $escapedVariableValue = Xml::escapeJsString( $value ); |
| 161 | + $escapedVariableKey = Xml::escapeJsString( $key ); |
| 162 | + self::$variables[$key] = |
| 163 | + "var {$escapedVariableKey} = '{$escapedVariableValue}';"; |
| 164 | + } |
| 165 | + if ( count( self::$variables ) > 0 ) { |
| 166 | + $out->addScript( |
| 167 | + Xml::tags( |
| 168 | + 'script', |
| 169 | + array( 'type' => $wgJsMimeType ), |
| 170 | + implode( self::$variables ) |
| 171 | + ) |
| 172 | + ); |
| 173 | + } |
158 | 174 | // Loops over each style |
159 | 175 | foreach ( self::$styles as $style ) { |
160 | 176 | // Add css for various styles |
— | — | @@ -196,4 +212,13 @@ |
197 | 213 | public static function addMessages( $messages ) { |
198 | 214 | self::$messages = array_merge( self::$messages, $messages ); |
199 | 215 | } |
| 216 | + |
| 217 | + /** |
| 218 | + * Adds internationalized message definitions to the document for access |
| 219 | + * via javascript using the gM() function |
| 220 | + * @param array $messages Key names of messages to load |
| 221 | + */ |
| 222 | + public static function addVariables( $variables ) { |
| 223 | + self::$variables = array_merge( self::$variables, $variables ); |
| 224 | + } |
200 | 225 | } |