/**
  * 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 = AppLocale::getParameterNames($email['body']);
         $referenceBodyParams = AppLocale::getParameterNames($referenceEmails[$emailKey]['body']);
         $diff = array_diff($bodyParams, $referenceBodyParams);
         if (!empty($diff)) {
             $errors[EMAIL_ERROR_DIFFERING_PARAMS][] = array('key' => $emailKey, 'mismatch' => $diff);
         }
         $subjectParams = AppLocale::getParameterNames($email['subject']);
         $referenceSubjectParams = AppLocale::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;
 }
Example #2
0
 /**
  * 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 = AppLocale::getParameterNames($referenceValue);
         $params = AppLocale::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;
 }
 /**
  * Get a list of email templates for the supplied locale. Returns data in the
  * following data structure:
  * array(
  *   'emailKey' => array(
  *     'subject' => 'Localized Subject',
  *     'body' => 'Localized Body',
  *     'description' => 'Localized Description',
  *     'templateFile' => '/path/to/template-file.xml',
  *     'templateDataFile' => '/path/to/localized/data-file.xml'
  *   ), ...
  * )
  * @param $locale string Locale code
  * @return array Email template data
  */
 static function _getEmailTemplates($locale)
 {
     $files = TranslatorAction::getEmailFileMap($locale);
     $returner = array();
     foreach ($files as $templateFile => $templateDataFile) {
         if (!file_exists($templateFile)) {
             continue;
         }
         $xmlParser = new XMLParser();
         $data = $xmlParser->parse($templateFile);
         foreach ($data->getChildren() as $emailNode) {
             $returner[$emailNode->getAttribute('key')] = array('subject' => null, 'body' => null, 'description' => null, 'templateFile' => $templateFile, 'statusCode' => 'doesNotExist');
         }
         if (!file_exists($templateDataFile)) {
             continue;
         }
         $localeData = $xmlParser->parse($templateDataFile);
         if ($localeData) {
             foreach ($localeData->getChildren() as $emailNode) {
                 $key = $emailNode->getAttribute('key');
                 if (isset($returner[$key])) {
                     $returner[$key] = array_merge($returner[$key], array('subject' => $emailNode->getChildValue('subject'), 'body' => $emailNode->getChildValue('body'), 'description' => $emailNode->getChildValue('description'), 'templateDataFile' => $templateDataFile, 'statusCode' => 'exists'));
                 }
             }
         }
     }
     // Fill in status
     if ($locale != MASTER_LOCALE) {
         $masterLocale = self::_getEmailTemplates(MASTER_LOCALE);
     }
     foreach ($returner as $key => &$emailData) {
         switch ($emailData['statusCode']) {
             case 'doesNotExist':
                 $emailData['status'] = __('plugins.generic.translator.email.doesNotExist');
                 break;
             case 'exists':
                 $emailData['status'] = __('plugins.generic.translator.email.complete');
                 if ($locale != MASTER_LOCALE) {
                     $referenceSubject = $masterLocale[$key]['subject'];
                     $referenceBody = $masterLocale[$key]['body'];
                     if (0 != count(array_diff(AppLocale::getParameterNames($emailData['subject']), AppLocale::getParameterNames($referenceSubject)))) {
                         $emailData['status'] = __('plugins.generic.translator.errors.EMAIL_ERROR_DIFFERING_PARAMS.title');
                     }
                     if (0 != count(array_diff(AppLocale::getParameterNames($emailData['body']), AppLocale::getParameterNames($referenceBody)))) {
                         $emailData['status'] = __('plugins.generic.translator.errors.LOCALE_ERROR_DIFFERING_PARAMS.title');
                     }
                 }
                 break;
         }
     }
     return $returner;
 }