Index: trunk/extensions/Translate/MessageChecks.php |
— | — | @@ -30,6 +30,11 @@ |
31 | 31 | 'checkPrintfExtraVars', |
32 | 32 | 'checkBalance', |
33 | 33 | ), |
| 34 | + 'voctrain' => array( |
| 35 | + 'checkI18nSprintMissingVars', |
| 36 | + 'checkI18nSprintExtraVars', |
| 37 | + 'checkBalance', |
| 38 | + ), |
34 | 39 | ); |
35 | 40 | |
36 | 41 | private function __construct() { |
— | — | @@ -397,6 +402,56 @@ |
398 | 403 | } |
399 | 404 | } |
400 | 405 | |
| 406 | + /** |
| 407 | + * Checks for translations not containing vars %xx that are in the |
| 408 | + * definition. |
| 409 | + * |
| 410 | + * @param $message Instance of TMessage. |
| 411 | + * @return True if namespace has been tampered with. |
| 412 | + */ |
| 413 | + protected function checkI18nSprintMissingVars( TMessage $message, $code, &$desc = null ) { |
| 414 | + if ( !preg_match_all( '/%[^% ]+/U', $message->definition, $defVars ) ) { |
| 415 | + return false; |
| 416 | + } |
| 417 | + preg_match_all( '/%[^% ]+/U', $message->translation, $transVars ); |
| 418 | + |
| 419 | + $missing = self::compareArrays( $defVars[0], $transVars[0] ); |
| 420 | + |
| 421 | + if ( $count = count($missing) ) { |
| 422 | + global $wgLang; |
| 423 | + $desc = array( 'translate-checks-parameters', |
| 424 | + implode( ', ', $missing ), |
| 425 | + $wgLang->formatNum($count) ); |
| 426 | + return true; |
| 427 | + } else { |
| 428 | + return false; |
| 429 | + } |
| 430 | + } |
| 431 | + |
| 432 | + /** |
| 433 | + * Checks for translations containing vars %xx that are not in the |
| 434 | + * definition. |
| 435 | + * |
| 436 | + * @param $message Instance of TMessage. |
| 437 | + * @return True if namespace has been tampered with. |
| 438 | + */ |
| 439 | + protected function checkI18nSprintExtraVars( TMessage $message, $code, &$desc = null ) { |
| 440 | + preg_match_all( '/%[^% ]+/U', $message->definition, $defVars ); |
| 441 | + preg_match_all( '/%[^% ]+/U', $message->translation, $transVars ); |
| 442 | + |
| 443 | + $missing = self::compareArrays( $transVars[0], $defVars[0] ); |
| 444 | + |
| 445 | + if ( $count = count($missing) ) { |
| 446 | + global $wgLang; |
| 447 | + $desc = array( 'translate-checks-parameters-unknown', |
| 448 | + implode( ', ', $missing ), |
| 449 | + $wgLang->formatNum($count) ); |
| 450 | + return true; |
| 451 | + } else { |
| 452 | + return false; |
| 453 | + } |
| 454 | + } |
| 455 | + |
401 | 456 | protected static function compareArrays( $defs, $trans ) { |
402 | 457 | $missing = array(); |
403 | 458 | foreach ( $defs as $defVar ) { |