示例#1
0
 /**
  * Render poedit screen
  * @param string optional package root directory
  * @param string PO or PO file path
  * @param array data to load into editor
  */
 private static function render_poeditor(LocoPackage $package, $path, array $data, LocoHeaders $head = null)
 {
     $pot = $po = $locale = null;
     $warnings = array();
     // remove header and check if empty
     $minlength = 1;
     if (isset($data[0]['source']) && $data[0]['source'] === '') {
         $data[0] = array();
         $minlength = 2;
     }
     // path may not exist if we're creating a new one
     if (file_exists($path)) {
         $modified = self::format_datetime(filemtime($path));
     } else {
         $modified = 0;
     }
     if ($is_pot = self::is_pot($path)) {
         $pot = $data;
         $type = 'POT';
     } else {
         $po = $data;
         $type = 'PO';
         $locale = self::resolve_file_locale($path);
         $domain = self::resolve_file_domain($path);
         $haspot = $package->get_pot($domain);
     }
     // warn if new file can't be written
     $writable = self::is_writable($path);
     if (!$writable && !$modified) {
         //$message = $modified ? Loco::__('File cannot be saved to disk automatically'): Loco::__('File cannot be created automatically');
         //$warnings[] = $message.'. '.sprintf(Loco::__('Fix the file permissions on %s'),$path);
         $warnings[] = Loco::__('File cannot be created automatically. Fix the file permissions or use Download instead of Save');
     }
     // Warnings if file is empty
     if (count($data) < $minlength) {
         $lines = array();
         if ($is_pot) {
             if ($modified) {
                 // existing POT, may need sync
                 $lines[] = sprintf(Loco::__('%s file is empty'), 'POT');
                 $lines[] = Loco::__('Run Sync to update from source code');
             } else {
                 // new POT, would have tried to extract from source. Fine you can add by hand
                 $lines[] = Loco::__('No strings could be extracted from source code');
             }
         } else {
             if ($modified) {
                 $lines[] = sprintf(Loco::__('%s file is empty'), 'PO');
                 if ($haspot) {
                     // existing PO that might be updatable from POT
                     $lines[] = sprintf(Loco::__('Run Sync to update from %s'), basename($haspot));
                 } else {
                     // existing PO that might be updatable from sources
                     $lines[] = Loco::__('Run Sync to update from source code');
                 }
             } else {
                 // this shouldn't happen if we throw an error during msginit
                 throw new Exception(Loco::__('No translatable strings found'));
             }
         }
         $warnings[] = implode('. ', $lines);
     } else {
         if ($modified) {
             if ($is_pot) {
                 $sources = $package->get_source_files();
                 if ($sources && filemtime($path) < self::newest_mtime_recursive($sources)) {
                     $warnings[] = Loco::__('Source code has been modified, run Sync to update POT');
                 }
             } else {
                 if ($haspot && filemtime($haspot) > filemtime($path)) {
                     $warnings[] = Loco::__('POT has been modified since PO file was saved, run Sync to update');
                 }
             }
         }
     }
     // extract some PO headers
     if ($head instanceof LocoHeaders) {
         $proj = $head->trimmed('Project-Id-Version');
         if ($proj && 'PACKAGE VERSION' !== $proj) {
             $name = $proj;
         }
     } else {
         $head = new LocoHeaders();
     }
     // set Last-Translator if PO file
     if (!$is_pot) {
         /* @var WP_User $user */
         $user = wp_get_current_user() and $head->add('Last-Translator', $user->get('display_name') . ' <' . $user->get('user_email') . '>');
     }
     // overwrite source location headers
     // create a relative path to target source directory from location of PO
     if (!$head->has('X-Poedit-Basepath')) {
         $head->add('X-Poedit-Basepath', '.');
         foreach ($package->get_source_dirs($path) as $i => $dir) {
             $dir or $dir = '.';
             $head->add('X-Poedit-SearchPath-' . $i, $dir);
         }
     }
     // compiled keywords for running source extraction in POEdit
     // note that these aren't just wordpress keywords, but they're the same as we're using in self::xgettext
     $ext = new LocoPHPExtractor();
     $head->add('X-Poedit-KeywordsList', implode(';', $ext->get_xgettext_keywords()));
     // ensure nice name for project
     if (!isset($name)) {
         $meta = $package->meta();
         $name = $meta['name'];
     }
     $head->add('Project-Id-Version', $name);
     $headers = $head->export();
     // no longer need the full local paths
     $path = self::trim_path($path);
     // If parsing MO file, from now on treat as PO
     if (!$is_pot && self::is_mo($path)) {
         $path = str_replace('.mo', '.po', $path);
     }
     Loco::enqueue_scripts('build/admin-common', 'build/admin-poedit');
     Loco::render('admin-poedit', compact('package', 'path', 'po', 'pot', 'locale', 'headers', 'name', 'type', 'modified', 'writable', 'warnings'));
     return true;
 }
function loco_parse_po_headers($str)
{
    $headers = new LocoHeaders();
    foreach (explode("\n", $str) as $line) {
        $i = strpos($line, ':') and $key = trim(substr($line, 0, $i)) and $headers->add($key, trim(substr($line, ++$i)));
    }
    return $headers;
}