[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/formats/ -> format-pomo.php (source)

   1  <?php
   2  
   3  class GP_Format_PO extends GP_Format {
   4  
   5      public $name = 'Portable Object Message Catalog (.po/.pot)';
   6      public $extension = 'po';
   7      public $alt_extensions = array( 'pot' );
   8  
   9      public $class = 'PO';
  10  
  11  	public function print_exported_file( $project, $locale, $translation_set, $entries ) {
  12          $po = new $this->class;
  13  
  14          // See https://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html for header details.
  15          // TODO: add more meta data in the project: language team, report URL.
  16          $this->set_header( $po, 'PO-Revision-Date', GP::$translation->last_modified( $translation_set ) . '+0000' );
  17          $this->set_header( $po, 'MIME-Version', '1.0' );
  18          $this->set_header( $po, 'Content-Type', 'text/plain; charset=UTF-8' );
  19          $this->set_header( $po, 'Content-Transfer-Encoding', '8bit' );
  20          $this->set_header( $po, 'Plural-Forms', "nplurals=$locale->nplurals; plural=$locale->plural_expression;" );
  21          $this->set_header( $po, 'X-Generator', 'GlotPress/' . GP_VERSION );
  22  
  23          $language_code = $this->get_language_code( $locale );
  24          if ( false !== $language_code ) {
  25              $this->set_header( $po, 'Language', $language_code );
  26          }
  27  
  28          // Force export only current translations.
  29          $filters = array();
  30          $filters['status'] = 'current';
  31  
  32          foreach( $entries as $entry ) {
  33              $po->add_entry( $entry );
  34          }
  35  
  36          $current = $project;
  37          $project_tree = array();
  38          $project_tree[] = $current->name;
  39  
  40          while ( $current->parent_project_id > 0 ) {
  41              $current = GP::$project->get( $current->parent_project_id );
  42              $project_tree[] = $current->name;
  43          }
  44  
  45          $project_tree = array_reverse( $project_tree );
  46  
  47          $project_id_version = implode( ' - ', $project_tree );
  48  
  49          /**
  50           * Filter the project name and version header before export.
  51           *
  52           * @since 2.1.0
  53           *
  54           * @param string $project_id_version The default project name/version to use in the header and
  55           *                                   comments ( "Parent - Child - GrandChild - etc." by default).
  56           * @param array  $project_tree       An array of the parent/child project tree, ordered from Parent
  57           *                                   to child to grandchild to etc...
  58           */
  59          $project_id_version = apply_filters( 'gp_pomo_export_project_id_version', $project_id_version, $project_tree );
  60  
  61          $this->set_header( $po, 'Project-Id-Version', $project_id_version );
  62  
  63          $this->add_comments_before_headers( $po, "Translation of {$project_id_version} in {$locale->english_name}\n" );
  64          $this->add_comments_before_headers( $po, "This file is distributed under the same license as the {$project_id_version} package.\n" );
  65  
  66          return $po->export();
  67      }
  68  
  69  	public function read_translations_from_file( $file_name, $project = null ) {
  70          $po     = new $this->class;
  71          $result = $po->import_from_file( $file_name );
  72  
  73          return $result ? $po : $result;
  74      }
  75  
  76  	public function read_originals_from_file( $file_name ) {
  77          return $this->read_translations_from_file( $file_name );
  78      }
  79  
  80      /**
  81       * Add a header to the selected format, overrideable by child classes.
  82       *
  83       * @since 2.1.0
  84       *
  85       * @param GP_Format $format The format object to set the header for.
  86       * @param string    $header The header name to set.
  87       * @param string    $text   The text to set the header to.
  88       */
  89  	protected function set_header( $format, $header, $text ) {
  90          $format->set_header( $header, $text );
  91      }
  92  
  93      /**
  94       * Add a comment before the headers for the selected format, overrideable by child classes.
  95       *
  96       * @since 2.1.0
  97       *
  98       * @param GP_Format $format The format object to set the header for.
  99       * @param string    $text   The text to add to the comment.
 100       */
 101  	protected function add_comments_before_headers( $format, $text ) {
 102          $format->comments_before_headers .= $text;
 103      }
 104  }
 105  
 106  class GP_Format_MO extends GP_Format_PO {
 107      public $name = 'Machine Object Message Catalog (.mo)';
 108      public $extension = 'mo';
 109      public $alt_extensions = array();
 110  
 111      public $class = 'MO';
 112  
 113      /**
 114       * Override the comments function as PO files do not use it.
 115       *
 116       * @since 2.1.0
 117       *
 118       * @param GP_Format $format The format object to set the header for.
 119       * @param string    $text   The text to add to the comment.
 120       */
 121  	protected function add_comments_before_headers( $format, $text ) {
 122          return;
 123      }
 124  }
 125  
 126  GP::$formats['po'] = new GP_Format_PO;
 127  GP::$formats['mo'] = new GP_Format_MO;


Generated: Mon Nov 18 01:01:56 2019 Cross-referenced by PHPXref 0.7.1