/** * Parse MO, PO or POT file, placing header object into argument */ public static function parse_po_with_headers($path, &$headers) { $export = self::parse_po($path); if (!isset($export[0])) { $ext = strtoupper(pathinfo($path, PATHINFO_EXTENSION)); throw new Exception(sprintf(Loco::__('Empty or invalid %s file'), $ext)); } if ($export[0]['source'] !== '') { $ext = strtoupper(pathinfo($path, PATHINFO_EXTENSION)); throw new Exception(sprintf(Loco::__('%s file has no header'), $ext)); } $headers = loco_parse_po_headers($export[0]['target']); $export[0] = array(); // <- avoid index errors as json return $export; }
$pot = basename($pot_path); break; } } } // Extract from sources by default if (!$package->has_source_dirs()) { throw new Exception(Loco::__('No source directories in this package, cannot sync from source code')); } if ($exp = LocoAdmin::xgettext($package, dirname($path))) { $pot = ''; break; } throw new Exception(Loco::__('No strings could be extracted from source files')); } // sync selected headers $headers = array(); if ('' === $exp[0]['source']) { $keep = array('Project-Id-Version', 'Language-Team', 'POT-Creation-Date', 'POT-Revision-Date'); $head = loco_parse_po_headers($exp[0]['target']); $headers = array_intersect_key($head->to_array(), array_flip($keep)); /*/ add prefixed header keys that can't be included above foreach( $head as $key => $value ){ if( 0 === strpos($key, 'X-Poedit-' ) ){ $headers[$key] = $value; } }*/ $exp[0] = array(); } // sync ok. return compact('pot', 'exp', 'headers');
function loco_msgfmt($po, $withhash = false) { if (!is_array($po)) { $po = loco_parse_po($po); } $head = null; if (isset($po[0]) && '' === $po[0]['source']) { $head = loco_parse_po_headers($po[0]['target']); $po[0] = null; } $export = array(); foreach ($po as $i => $r) { if (!$r) { continue; } $msg = array('key' => $r['source'], 'translation' => $r['target']); if (isset($r['parent'])) { unset($parent); $parent =& $export[$r['parent']]; isset($parent['plurals']) or $parent['plurals'] = array(); $parent['plurals'][] = $msg; } else { isset($r['context']) and $msg['context'] = $r['context']; $export[$i] = $msg; } } $mo = new LocoMo($export, $head); if ($withhash) { $mo->enableHash(); } return $mo->compile(); }