/** * Test a locale file against the given reference locale file and * return an array of errorType => array(errors). * @param $referenceLocaleFile object * @return array */ function testLocale(&$referenceLocaleFile) { $errors = array(LOCALE_ERROR_MISSING_KEY => array(), LOCALE_ERROR_EXTRA_KEY => array(), LOCALE_ERROR_SUSPICIOUS_LENGTH => array(), LOCALE_ERROR_DIFFERING_PARAMS => array(), LOCALE_ERROR_MISSING_FILE => array()); if (!$this->isValid()) { $errors[LOCALE_ERROR_MISSING_FILE][] = array('locale' => $this->locale, 'filename' => $this->filename); return $errors; } $localeContents = LocaleFile::load($this->filename); $referenceContents = LocaleFile::load($referenceLocaleFile->filename); foreach ($referenceContents as $key => $referenceValue) { if (!isset($localeContents[$key])) { $errors[LOCALE_ERROR_MISSING_KEY][] = array('key' => $key, 'locale' => $this->locale, 'filename' => $this->filename, 'reference' => $referenceValue); continue; } $value = $localeContents[$key]; // Watch for suspicious lengths. if (!Locale::checkLengths($referenceValue, $value)) { $errors[LOCALE_ERROR_SUSPICIOUS_LENGTH][] = array('key' => $key, 'locale' => $this->locale, 'referenceLocale' => $referenceLocaleFile->locale, 'reference' => $referenceValue, 'value' => $value, 'filename' => $this->filename); } $referenceParams = Locale::getParameterNames($referenceValue); $params = Locale::getParameterNames($value); if (count(array_diff($referenceParams, $params)) > 0) { $errors[LOCALE_ERROR_DIFFERING_PARAMS][] = array('key' => $key, 'locale' => $this->locale, 'mismatch' => array_diff($referenceParams, $params), 'filename' => $this->filename, 'reference' => $referenceValue, 'value' => $value); } // After processing a key, remove it from the list; // this way, the remainder at the end of the loop // will be extra unnecessary keys. unset($localeContents[$key]); } // Leftover keys are extraneous. foreach ($localeContents as $key => $value) { $errors[LOCALE_ERROR_EXTRA_KEY][] = array('key' => $key, 'locale' => $this->locale, 'filename' => $this->filename); } return $errors; }
/** * Test the emails in the supplied locale against those in the supplied * reference locale. * @param $locale string * @param $referenceLocale string * @return array List of errors */ function testEmails($locale, $referenceLocale) { $errors = array(); $emails = TranslatorAction::getEmailTemplates($locale); $referenceEmails = TranslatorAction::getEmailTemplates($referenceLocale); // Pass 1: For all translated emails, check that they match // against reference translations. foreach ($emails as $emailKey => $email) { // Check if a matching reference email was found. if (!isset($referenceEmails[$emailKey])) { $errors[EMAIL_ERROR_EXTRA_EMAIL][] = array('key' => $emailKey); continue; } // We've successfully found a matching reference email. // Compare it against the translation. $bodyParams = Locale::getParameterNames($email['body']); $referenceBodyParams = Locale::getParameterNames($referenceEmails[$emailKey]['body']); $diff = array_diff($bodyParams, $referenceBodyParams); if (!empty($diff)) { $errors[EMAIL_ERROR_DIFFERING_PARAMS][] = array('key' => $emailKey, 'mismatch' => $diff); } $subjectParams = Locale::getParameterNames($email['subject']); $referenceSubjectParams = Locale::getParameterNames($referenceEmails[$emailKey]['subject']); $diff = array_diff($subjectParams, $referenceSubjectParams); if (!empty($diff)) { $errors[EMAIL_ERROR_DIFFERING_PARAMS][] = array('key' => $emailKey, 'mismatch' => $diff); } $matchedReferenceEmails[] = $emailKey; unset($email); unset($referenceEmail); } // Pass 2: Make sure that there are no missing translations. foreach ($referenceEmails as $emailKey => $email) { // Extract the fields from the email to be tested. if (!isset($emails[$emailKey])) { $errors[EMAIL_ERROR_MISSING_EMAIL][] = array('key' => $emailKey); } } return $errors; }
/** * Test the emails in the supplied locale against those in the supplied * reference locale. * @param $locale string * @param $referenceLocale string * @return array List of errors */ function testEmails($locale, $referenceLocale) { $errors = array(); $xmlParser =& new XMLParser(); $referenceEmails =& $xmlParser->parse(Locale::getEmailTemplateFilename($referenceLocale)); $emails =& $xmlParser->parse(Locale::getEmailTemplateFilename($locale)); $emailsTable =& $emails->getChildByName('table'); $referenceEmailsTable =& $referenceEmails->getChildByName('table'); $matchedReferenceEmails = array(); // Pass 1: For all translated emails, check that they match // against reference translations. for ($emailIndex = 0; ($email =& $emailsTable->getChildByName('row', $emailIndex)) !== null; $emailIndex++) { // Extract the fields from the email to be tested. $fields = Locale::extractFields($email); // Locate the reference email and extract its fields. for ($referenceEmailIndex = 0; ($referenceEmail =& $referenceEmailsTable->getChildByName('row', $referenceEmailIndex)) !== null; $referenceEmailIndex++) { $referenceFields = Locale::extractFields($referenceEmail); if ($referenceFields['email_key'] == $fields['email_key']) { break; } } // Check if a matching reference email was found. if (!isset($referenceEmail) || $referenceEmail === null) { $errors[EMAIL_ERROR_EXTRA_EMAIL][] = array('key' => $fields['email_key']); continue; } // We've successfully found a matching reference email. // Compare it against the translation. $bodyParams = Locale::getParameterNames($fields['body']); $referenceBodyParams = Locale::getParameterNames($referenceFields['body']); $diff = array_diff($bodyParams, $referenceBodyParams); if (!empty($diff)) { $errors[EMAIL_ERROR_DIFFERING_PARAMS][] = array('key' => $fields['email_key'], 'mismatch' => $diff); } $subjectParams = Locale::getParameterNames($fields['subject']); $referenceSubjectParams = Locale::getParameterNames($referenceFields['subject']); $diff = array_diff($subjectParams, $referenceSubjectParams); if (!empty($diff)) { $errors[EMAIL_ERROR_DIFFERING_PARAMS][] = array('key' => $fields['email_key'], 'mismatch' => $diff); } $matchedReferenceEmails[] = $fields['email_key']; unset($email); unset($referenceEmail); } // Pass 2: Make sure that there are no missing translations. for ($referenceEmailIndex = 0; ($referenceEmail =& $referenceEmailsTable->getChildByName('row', $referenceEmailIndex)) !== null; $referenceEmailIndex++) { // Extract the fields from the email to be tested. $referenceFields = Locale::extractFields($referenceEmail); if (!in_array($referenceFields['email_key'], $matchedReferenceEmails)) { $errors[EMAIL_ERROR_MISSING_EMAIL][] = array('key' => $referenceFields['email_key']); } } return $errors; }
/** * Test a locale file against the given reference locale file and * return an array of errorType => array(errors). * @param $referenceLocaleFile object * @return array */ function testLocale(&$referenceLocaleFile) { $errors = array(LOCALE_ERROR_MISSING_KEY => array(), LOCALE_ERROR_EXTRA_KEY => array(), LOCALE_ERROR_DIFFERING_PARAMS => array(), LOCALE_ERROR_MISSING_FILE => array()); if ($referenceLocaleFile->isValid()) { if (!$this->isValid()) { $errors[LOCALE_ERROR_MISSING_FILE][] = array('locale' => $this->locale, 'filename' => $this->filename); return $errors; } } else { // If the reference file itself does not exist or is invalid then // there's nothing to be translated here. return $errors; } $localeContents = LocaleFile::load($this->filename); $referenceContents = LocaleFile::load($referenceLocaleFile->filename); foreach ($referenceContents as $key => $referenceValue) { if (!isset($localeContents[$key])) { $errors[LOCALE_ERROR_MISSING_KEY][] = array('key' => $key, 'locale' => $this->locale, 'filename' => $this->filename, 'reference' => $referenceValue); continue; } $value = $localeContents[$key]; $referenceParams = Locale::getParameterNames($referenceValue); $params = Locale::getParameterNames($value); if (count(array_diff($referenceParams, $params)) > 0) { $errors[LOCALE_ERROR_DIFFERING_PARAMS][] = array('key' => $key, 'locale' => $this->locale, 'mismatch' => array_diff($referenceParams, $params), 'filename' => $this->filename, 'reference' => $referenceValue, 'value' => $value); } // After processing a key, remove it from the list; // this way, the remainder at the end of the loop // will be extra unnecessary keys. unset($localeContents[$key]); } // Leftover keys are extraneous. foreach ($localeContents as $key => $value) { $errors[LOCALE_ERROR_EXTRA_KEY][] = array('key' => $key, 'locale' => $this->locale, 'filename' => $this->filename); } return $errors; }