Exemplo n.º 1
0
 public function execute()
 {
     // I18nProfile class: http://stackoverflow.com/questions/8433686/is-there-a-php-library-for-parsing-gettext-po-pot-files
     App::import('Lib', 'I18nJs.PoParser');
     $dir = new Folder(APP . 'Locale');
     $locales = $dir->read();
     foreach ($locales[0] as $localeDir) {
         $msgids = array();
         $language = $localeDir;
         $localeDir = new Folder(APP . 'Locale' . DS . $localeDir);
         $files = $localeDir->findRecursive('i18n_js.*\\.po');
         // Loop over PO i18n_js PO files
         foreach ($files as $file) {
             $file = new File($file);
             // Get language
             if (preg_match('%Locale/(.*?)/LC_MESSAGES%', $file->path, $regs)) {
                 $language = $regs[1];
             } else {
                 // todo return
                 $this->out(__d('i18n_js', '<error>Unable to determine language of PO file:</error>') . $file->path);
                 return;
             }
             // Get context, domain
             $context = '';
             if (strpos($file->name(), '.')) {
                 $context = explode('.', $file->name());
                 $context = $context[1];
             }
             // Parse PO file
             $poparser = new \Sepia\PoParser();
             $translations = $poparser->parse($file->path);
             foreach ($translations as $key => $translation) {
                 if (empty($key)) {
                     continue;
                 }
                 if (is_array($translation['msgid'])) {
                     $translation['msgid'] = implode('', $translation['msgid']);
                 }
                 if (is_array($translation['msgstr'])) {
                     $translation['msgstr'] = implode('', $translation['msgstr']);
                 }
                 $msgids[$context][$translation['msgid']] = $translation['msgstr'];
             }
         }
         if (empty($msgids)) {
             continue;
         }
         // Write JS file
         $outputFile = new File(WWW_ROOT . 'js' . DS . 'Locale' . DS . 'i18n_js.' . $language . '.js', true);
         $data = "I18nJs.locale = { ";
         $data .= "'pluralFormula': function (\$n) { return Number((\$n != 1)); }, ";
         $data .= "'strings': " . json_encode($msgids, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT) . " };";
         if ($outputFile->write($data)) {
             $this->out(__d('i18n_js', '<info>%s created</info>', $outputFile->path));
         } else {
             $this->out(__d('i18n_js', '<error>Unable to write: %s</error>', $outputFile->path));
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Read .po file and return array of translated (not fuzzy)
  * strings [original]=>translation
  *
  * @param string $path Path to the file to read
  *
  * @return array Array of strings
  */
 public static function loadPoFile($path)
 {
     $po_parser = new \Sepia\PoParser();
     $po_strings = $po_parser->read($path);
     $po_data = [];
     if (count($po_strings) > 0) {
         foreach ($po_strings as $entry) {
             if (!isset($entry['fuzzy']) && implode($entry['msgstr']) != '') {
                 if (implode($entry['msgid']) == implode($entry['msgstr'])) {
                     // Add {ok} if the translation is identical to the English string
                     $string_status = ' {ok}';
                 } else {
                     $string_status = '';
                 }
                 $po_data[implode($entry['msgid'])] = trim(implode($entry['msgstr']) . $string_status);
             }
         }
     }
     return $po_data;
 }
Exemplo n.º 3
0
 /**
  * Parses a translationfile.
  *
  * This method is intend to parse a translationfile and return the result as array.
  *
  * @param string $filename The name of the file to parse
  *
  * @author Benjamin Carl <*****@*****.**>
  *
  * @return array The content of the given file
  *
  * @throws Doozr_I18n_Service_Exception
  */
 protected function parseTranslationfile($filename)
 {
     // Check for existence ...
     if (!file_exists($filename) || !is_readable($filename)) {
         throw new Doozr_I18n_Service_Exception(sprintf('Translationfile: "%s" does not exist or isn\'t readable.', $filename));
     }
     // Assume empty resulting array
     $result = [];
     // Init PO Parser and parse file ...
     $fileHandler = new Sepia\FileHandler($filename);
     $poParser = new Sepia\PoParser($fileHandler);
     $entries = $poParser->parse();
     // Iterate entries ...
     foreach ($entries as $entry => $translation) {
         $result[md5($entry)] = $translation['msgstr'][0];
     }
     // return parsed array
     return $result;
 }
Exemplo n.º 4
0
 public function importPOTranslation($filename, \Translation $translation)
 {
     $parser = new \Sepia\PoParser();
     $data = $parser->parseFile($filename);
     foreach ($translation->getMessages() as $message) {
         $msgId = $message->getSingular();
         if (isset($data[$msgId])) {
             $entry = $data[$msgId];
             $trans = implode('|', $entry['msgstr']);
             $message->setTranslations($entry['msgstr']);
             $this->dm->persist($message);
         }
     }
     $this->dm->flush();
 }
 private function _collectStringsFromPoFile($file)
 {
     $fileHandler = new Sepia\FileHandler($file);
     $poParser = new Sepia\PoParser($fileHandler);
     $entries = $poParser->parse();
     $toReturn = array();
     foreach ($entries as $entry) {
         if (!isset($entry['msgid'])) {
             throw new RuntimeException("{$file} has an entry with no msgid");
         }
         $toReturn[] = $entry['msgid'][0];
     }
     $toReturn = array_unique($toReturn);
     return $toReturn;
 }