|
|
| 1 |
<?php |
|
|
| 2 |
|
| 3 |
/** |
| 4 |
* @file tools/localeCheck.php |
| 5 |
* |
| 6 |
* Copyright (c) 2005-2010 Alec Smecher and John Willinsky |
| 7 |
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING. |
| 8 |
* |
| 9 |
* @class localeCheck |
| 10 |
* @ingroup tools |
| 11 |
* |
| 12 |
* @brief CLI tool to check the various locales for consistency. |
| 13 |
* |
| 14 |
*/ |
| 15 |
|
| 16 |
// $Id$ |
| 17 |
|
| 18 |
|
| 19 |
require(dirname(__FILE__) . '/bootstrap.inc.php'); |
| 20 |
|
| 21 |
define('MASTER_LOCALE', 'en_US'); |
| 22 |
|
| 23 |
define('LOCALE_ERROR_MISSING_KEY', 0x0000001); |
| 24 |
define('LOCALE_ERROR_EXTRA_KEY', 0x0000002); |
| 25 |
define('LOCALE_ERROR_SUSPICIOUS_LENGTH', 0x0000003); |
| 26 |
define('LOCALE_ERROR_DIFFERING_PARAMS', 0x0000004); |
| 27 |
|
| 28 |
define('EMAIL_ERROR_MISSING_EMAIL', 0x0000005); |
| 29 |
define('EMAIL_ERROR_EXTRA_EMAIL', 0x0000006); |
| 30 |
define('EMAIL_ERROR_DIFFERING_PARAMS', 0x0000007); |
| 31 |
|
| 32 |
class localeCheck extends CommandLineTool { |
| 33 |
/** @var $locales List of locales to check */ |
| 34 |
var $locales; |
| 35 |
|
| 36 |
function localeCheck($args) { |
| 37 |
parent::CommandLineTool($args); |
| 38 |
array_shift($args); // Knock the tool name off the list |
| 39 |
$this->locales = $args; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Print command usage information. |
| 44 |
*/ |
| 45 |
function usage() { |
| 46 |
echo "Script to test locales for consistency\n" |
| 47 |
. "Usage: {$this->scriptName} [localeName (optional)] ...\n"; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Test locales. |
| 52 |
*/ |
| 53 |
function execute() { |
| 54 |
// Flush the file cache just to be certain we're using |
| 55 |
// the most recent stuff |
| 56 |
import('lib.pkp.classes.cache.CacheManager'); |
| 57 |
$cacheManager =& CacheManager::getManager(); |
| 58 |
$cacheManager->flush('locale'); |
| 59 |
|
| 60 |
// Load plugins so that their locale data is included too |
| 61 |
$plugins = array(); |
| 62 |
foreach (PluginRegistry::getCategories() as $category) { |
| 63 |
echo "Loading plugin category \"$category\"...\n"; |
| 64 |
$morePlugins = PluginRegistry::loadCategory($category); |
| 65 |
if (is_array($morePlugins)) $plugins += $morePlugins; |
| 66 |
} |
| 67 |
|
| 68 |
foreach (Locale::getAllLocales() as $locale => $name) { |
| 69 |
if (!empty($this->locales) && !in_array($locale, $this->locales)) continue; |
| 70 |
if ($locale != MASTER_LOCALE) { |
| 71 |
echo "Testing locale \"$name\" ($locale) against reference locale " . MASTER_LOCALE . ".\n"; |
| 72 |
$this->testLocale($locale, MASTER_LOCALE, $plugins); |
| 73 |
$this->testEmails($locale, MASTER_LOCALE); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
function testEmails($locale, $referenceLocale) { |
| 79 |
import('lib.pkp.classes.install.Installer'); // Bring in data dir |
| 80 |
|
| 81 |
$errors = array( |
| 82 |
); |
| 83 |
|
| 84 |
$xmlParser = new XMLParser(); |
| 85 |
$referenceEmails =& $xmlParser->parse( |
| 86 |
INSTALLER_DATA_DIR . "/data/locale/$referenceLocale/email_templates_data.xml" |
| 87 |
); |
| 88 |
$emails =& $xmlParser->parse( |
| 89 |
INSTALLER_DATA_DIR . "/data/locale/$locale/email_templates_data.xml" |
| 90 |
); |
| 91 |
$emailsTable =& $emails->getChildByName('table'); |
| 92 |
$referenceEmailsTable =& $referenceEmails->getChildByName('table'); |
| 93 |
$matchedReferenceEmails = array(); |
| 94 |
|
| 95 |
// Pass 1: For all translated emails, check that they match |
| 96 |
// against reference translations. |
| 97 |
for ($emailIndex = 0; ($email =& $emailsTable->getChildByName('row', $emailIndex)) !== null; $emailIndex++) { |
| 98 |
// Extract the fields from the email to be tested. |
| 99 |
$fields = $this->extractFields($email); |
| 100 |
|
| 101 |
// Locate the reference email and extract its fields. |
| 102 |
for ($referenceEmailIndex = 0; ($referenceEmail =& $referenceEmailsTable->getChildByName('row', $referenceEmailIndex)) !== null; $referenceEmailIndex++) { |
| 103 |
$referenceFields = $this->extractFields($referenceEmail); |
| 104 |
if ($referenceFields['email_key'] == $fields['email_key']) break; |
| 105 |
} |
| 106 |
|
| 107 |
// Check if a matching reference email was found. |
| 108 |
if (!isset($referenceEmail) || $referenceEmail === null) { |
| 109 |
$errors[EMAIL_ERROR_EXTRA_EMAIL][] = array( |
| 110 |
'key' => $fields['email_key'] |
| 111 |
); |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
// We've successfully found a matching reference email. |
| 116 |
// Compare it against the translation. |
| 117 |
$bodyParams = $this->getParameterNames($fields['body']); |
| 118 |
$referenceBodyParams = $this->getParameterNames($referenceFields['body']); |
| 119 |
if ($bodyParams !== $referenceBodyParams) { |
| 120 |
$errors[EMAIL_ERROR_DIFFERING_PARAMS][] = array( |
| 121 |
'key' => $fields['email_key'], |
| 122 |
'mismatch' => array_diff($bodyParams, $referenceBodyParams) |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
$subjectParams = $this->getParameterNames($fields['subject']); |
| 127 |
$referenceSubjectParams = $this->getParameterNames($referenceFields['subject']); |
| 128 |
|
| 129 |
if ($subjectParams !== $referenceSubjectParams) { |
| 130 |
$errors[EMAIL_ERROR_DIFFERING_PARAMS][] = array( |
| 131 |
'key' => $fields['email_key'], |
| 132 |
'mismatch' => array_diff($subjectParams, $referenceSubjectParams) |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
$matchedReferenceEmails[] = $fields['email_key']; |
| 137 |
|
| 138 |
unset($email); |
| 139 |
unset($referenceEmail); |
| 140 |
} |
| 141 |
|
| 142 |
// Pass 2: Make sure that there are no missing translations. |
| 143 |
for ($referenceEmailIndex = 0; ($referenceEmail =& $referenceEmailsTable->getChildByName('row', $referenceEmailIndex)) !== null; $referenceEmailIndex++) { |
| 144 |
// Extract the fields from the email to be tested. |
| 145 |
$referenceFields = $this->extractFields($referenceEmail); |
| 146 |
if (!in_array($referenceFields['email_key'], $matchedReferenceEmails)) { |
| 147 |
$errors[EMAIL_ERROR_MISSING_EMAIL][] = array( |
| 148 |
'key' => $referenceFields['email_key'] |
| 149 |
); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
$this->displayEmailErrors($locale, $errors); |
| 154 |
} |
| 155 |
|
| 156 |
function extractFields(&$node) { |
| 157 |
$returner = array(); |
| 158 |
foreach ($node->getChildren() as $field) if ($field->getName() === 'field') { |
| 159 |
$returner[$field->getAttribute('name')] = $field->getValue(); |
| 160 |
} |
| 161 |
return $returner; |
| 162 |
} |
| 163 |
|
| 164 |
function displayEmailErrors($locale, $errors) { |
| 165 |
ksort($errors); |
| 166 |
echo "\nERROR REPORT FOR EMAILS IN \"$locale\":\n"; |
| 167 |
echo "-----------------------------------\n"; |
| 168 |
foreach ($errors as $type => $errorList) { |
| 169 |
if (!empty($errorList)) switch ($type) { |
| 170 |
case EMAIL_ERROR_MISSING_EMAIL: |
| 171 |
echo "The following messages are missing from the emails file and need translation.\n"; |
| 172 |
foreach ($errorList as $error) echo " - " . $error['key'] . "\n"; |
| 173 |
break; |
| 174 |
case EMAIL_ERROR_EXTRA_EMAIL: |
| 175 |
echo "\nThe following emails are not in the master translation and may be deleted:\n"; |
| 176 |
foreach ($errorList as $error) echo " - " . $error['key'] . "\n"; |
| 177 |
break; |
| 178 |
case EMAIL_ERROR_DIFFERING_PARAMS: |
| 179 |
echo "\nThe following emails are missing parameters or have extra parameters and need\n"; |
| 180 |
echo "correcting against the master translation:\n"; |
| 181 |
foreach ($errorList as $error) { |
| 182 |
echo " - " . $error['key'] . "\n"; |
| 183 |
echo " Mismatching parameter(s): " . implode(', ', $error['mismatch']) . "\n"; |
| 184 |
} |
| 185 |
break; |
| 186 |
default: die("Unknown error type \"$type\"!\n"); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
function testLocale($locale, $referenceLocale, $plugins) { |
| 192 |
$errors = array( |
| 193 |
LOCALE_ERROR_MISSING_KEY => array(), |
| 194 |
LOCALE_ERROR_EXTRA_KEY => array(), |
| 195 |
LOCALE_ERROR_SUSPICIOUS_LENGTH => array(), |
| 196 |
LOCALE_ERROR_DIFFERING_PARAMS => array() |
| 197 |
); |
| 198 |
|
| 199 |
$localeCache =& Locale::_getCache($locale); |
| 200 |
$referenceLocaleCache =& Locale::_getCache($referenceLocale); |
| 201 |
|
| 202 |
$localeContents =& $localeCache->getContents(); |
| 203 |
$referenceContents =& $referenceLocaleCache->getContents(); |
| 204 |
|
| 205 |
// Add locale data for plugins |
| 206 |
foreach ($plugins as $plugin) { |
| 207 |
$pluginCache = $plugin->_getCache($locale); |
| 208 |
$referencePluginCache = $plugin->_getCache($referenceLocale); |
| 209 |
$localeContents += $pluginCache->getContents(); |
| 210 |
$referenceContents += $referencePluginCache->getContents(); |
| 211 |
} |
| 212 |
|
| 213 |
foreach ($referenceContents as $key => $referenceValue) { |
| 214 |
if (!isset($localeContents[$key])) { |
| 215 |
$errors[LOCALE_ERROR_MISSING_KEY][] = array( |
| 216 |
'key' => $key, |
| 217 |
'locale' => $locale |
| 218 |
); |
| 219 |
continue; |
| 220 |
} |
| 221 |
$value = $localeContents[$key]; |
| 222 |
|
| 223 |
// Watch for suspicious lengths. |
| 224 |
if (!$this->checkLengths($referenceValue, $value)) { |
| 225 |
$errors[LOCALE_ERROR_SUSPICIOUS_LENGTH][] = array( |
| 226 |
'key' => $key, |
| 227 |
'locale' => $locale, |
| 228 |
'reference' => $referenceValue, |
| 229 |
'value' => $value |
| 230 |
); |
| 231 |
} |
| 232 |
|
| 233 |
$referenceParams = $this->getParameterNames($referenceValue); |
| 234 |
$params = $this->getParameterNames($value); |
| 235 |
if ($referenceParams !== $params) { |
| 236 |
$errors[LOCALE_ERROR_DIFFERING_PARAMS][] = array( |
| 237 |
'key' => $key, |
| 238 |
'locale' => $locale, |
| 239 |
'mismatch' => array_diff($referenceParams, $params) |
| 240 |
); |
| 241 |
} |
| 242 |
// After processing a key, remove it from the list; |
| 243 |
// this way, the remainder at the end of the loop |
| 244 |
// will be extra unnecessary keys. |
| 245 |
unset($localeContents[$key]); |
| 246 |
} |
| 247 |
|
| 248 |
// Leftover keys are extraneous. |
| 249 |
foreach ($localeContents as $key => $value) { |
| 250 |
$errors[LOCALE_ERROR_EXTRA_KEY][] = array( |
| 251 |
'key' => $key, |
| 252 |
'locale' => $locale |
| 253 |
); |
| 254 |
} |
| 255 |
|
| 256 |
$this->displayLocaleErrors($locale, $errors); |
| 257 |
} |
| 258 |
|
| 259 |
function displayLocaleErrors($locale, $errors) { |
| 260 |
ksort($errors); |
| 261 |
echo "\nERROR REPORT FOR LOCALE STRINGS IN \"$locale\":\n"; |
| 262 |
echo "---------------------------------------\n"; |
| 263 |
foreach ($errors as $type => $errorList) { |
| 264 |
if (!empty($errorList)) switch ($type) { |
| 265 |
case LOCALE_ERROR_MISSING_KEY: |
| 266 |
echo "The following keys are missing from a locale file and need to be translated.\n"; |
| 267 |
foreach ($errorList as $error) echo " - " . $error['key'] . "\n"; |
| 268 |
break; |
| 269 |
case LOCALE_ERROR_EXTRA_KEY: |
| 270 |
echo "\nThe following keys are not in the master translation and may be deleted:\n"; |
| 271 |
foreach ($errorList as $error) echo " - " . $error['key'] . "\n"; |
| 272 |
break; |
| 273 |
case LOCALE_ERROR_SUSPICIOUS_LENGTH: |
| 274 |
echo "\nThe following keys have suspicious lengths compared with the master translation\n"; |
| 275 |
echo "and may need checking:\n"; |
| 276 |
foreach ($errorList as $error) { |
| 277 |
$reference = $this->truncate($error['reference'], 65); |
| 278 |
$value = $this->truncate($error['value'], 65); |
| 279 |
echo " - " . $error['key'] . "\n"; |
| 280 |
echo " \"" . $reference . "\" vs.\n"; |
| 281 |
echo " \"" . $value . "\" ($locale)\n"; |
| 282 |
} |
| 283 |
break; |
| 284 |
case LOCALE_ERROR_DIFFERING_PARAMS: |
| 285 |
echo "\nThe following keys are missing parameters or have extra parameters and need\n"; |
| 286 |
echo "correcting against the master translation:\n"; |
| 287 |
foreach ($errorList as $error) { |
| 288 |
echo " - " . $error['key'] . "\n"; |
| 289 |
echo " Mismatching parameter(s): " . implode(', ', $error['mismatch']) . "\n"; |
| 290 |
} |
| 291 |
break; |
| 292 |
default: die("Unknown error type \"$type\"!\n"); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
function truncate($value, $length = 80, $ellipsis = '...') { |
| 298 |
if (String::strlen($value) > $length) { |
| 299 |
$value = String::substr($value, 0, $length - String::strlen($ellipsis)); |
| 300 |
return $value . $ellipsis; |
| 301 |
} |
| 302 |
return $value; |
| 303 |
} |
| 304 |
|
| 305 |
function checkLengths($reference, $value) { |
| 306 |
$referenceLength = String::strlen($reference); |
| 307 |
$length = String::strlen($value); |
| 308 |
$lengthDifference = abs($referenceLength - $length); |
| 309 |
if ($referenceLength == 0) return false; |
| 310 |
if ($lengthDifference / $referenceLength > 1 && $lengthDifference > 10) return false; |
| 311 |
return true; |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Given a locale string, get the list of parameter references of the |
| 316 |
* form {$myParameterName}. |
| 317 |
* @param $source string |
| 318 |
* @return array |
| 319 |
*/ |
| 320 |
function getParameterNames($source) { |
| 321 |
$matches = null; |
| 322 |
String::regexp_match_get('/({\$[^}]+})/' /* '/{\$[^}]+})/' */, $source, $matches); |
| 323 |
array_shift($matches); // Knock the top element off the array |
| 324 |
return $matches; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
$tool = new localeCheck(isset($argv) ? $argv : array()); |
| 329 |
$tool->execute(); |
| 330 |
|
| 331 |
?> |