/**
  * Loads the message translation for the specified language and category or returns null if file doesn't exist.
  *
  * @param string $messageFile path to message file
  * @param string $category the message category
  * @return array|null array of messages or null if file not found
  */
 protected function loadMessagesFromFile($messageFile, $category)
 {
     if (is_file($messageFile)) {
         if ($this->useMoFile) {
             $gettextFile = new GettextMoFile(['useBigEndian' => $this->useBigEndian]);
         } else {
             $gettextFile = new GettextPoFile();
         }
         $messages = $gettextFile->load($messageFile, $category);
         if (!is_array($messages)) {
             $messages = [];
         }
         return $messages;
     } else {
         return null;
     }
 }
Ejemplo n.º 2
0
 /**
  * Writes messages into PO file
  *
  * @param array $messages
  * @param string $dirName name of the directory to write to
  * @param boolean $overwrite if existing file should be overwritten without backup
  * @param boolean $removeUnused if obsolete translations should be removed
  * @param boolean $sort if translations should be sorted
  * @param string $catalog message catalog
  * @param boolean $markUnused if obsolete translations should be marked
  */
 protected function saveMessagesToPO($messages, $dirName, $overwrite, $removeUnused, $sort, $catalog, $markUnused)
 {
     $file = str_replace("\\", '/', "{$dirName}/{$catalog}.po");
     FileHelper::createDirectory(dirname($file));
     $this->stdout("Saving messages to {$file}...\n");
     $poFile = new GettextPoFile();
     $merged = [];
     $todos = [];
     $hasSomethingToWrite = false;
     foreach ($messages as $category => $msgs) {
         $notTranslatedYet = [];
         $msgs = array_values(array_unique($msgs));
         if (is_file($file)) {
             $existingMessages = $poFile->load($file, $category);
             sort($msgs);
             ksort($existingMessages);
             if (array_keys($existingMessages) == $msgs) {
                 $this->stdout("Nothing new in \"{$category}\" category...\n");
                 sort($msgs);
                 foreach ($msgs as $message) {
                     $merged[$category . chr(4) . $message] = $existingMessages[$message];
                 }
                 ksort($merged);
                 continue;
             }
             // merge existing message translations with new message translations
             foreach ($msgs as $message) {
                 if (array_key_exists($message, $existingMessages) && $existingMessages[$message] !== '') {
                     $merged[$category . chr(4) . $message] = $existingMessages[$message];
                 } else {
                     $notTranslatedYet[] = $message;
                 }
             }
             ksort($merged);
             sort($notTranslatedYet);
             // collect not yet translated messages
             foreach ($notTranslatedYet as $message) {
                 $todos[$category . chr(4) . $message] = '';
             }
             // add obsolete unused messages
             foreach ($existingMessages as $message => $translation) {
                 if (!$removeUnused && !isset($merged[$category . chr(4) . $message]) && !isset($todos[$category . chr(4) . $message])) {
                     if (!empty($translation) && (!$markUnused || substr($translation, 0, 2) === '@@' && substr($translation, -2) === '@@')) {
                         $todos[$category . chr(4) . $message] = $translation;
                     } else {
                         $todos[$category . chr(4) . $message] = '@@' . $translation . '@@';
                     }
                 }
             }
             $merged = array_merge($todos, $merged);
             if ($sort) {
                 ksort($merged);
             }
             if ($overwrite === false) {
                 $file .= '.merged';
             }
         } else {
             sort($msgs);
             foreach ($msgs as $message) {
                 $merged[$category . chr(4) . $message] = '';
             }
             ksort($merged);
         }
         $this->stdout("Category \"{$category}\" merged.\n");
         $hasSomethingToWrite = true;
     }
     if ($hasSomethingToWrite) {
         $poFile->save($file, $merged);
         $this->stdout("Translation saved.\n", Console::FG_GREEN);
     } else {
         $this->stdout("Nothing to save.\n", Console::FG_GREEN);
     }
 }