[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * GlotPress Format base class. It is supposed to be inherited.
   5   */
   6  abstract class GP_Format {
   7  
   8      public $name = '';
   9      public $extension = '';
  10      public $alt_extensions = array();
  11      public $filename_pattern = '%s-%s';
  12  
  13      public abstract function print_exported_file( $project, $locale, $translation_set, $entries );
  14      public abstract function read_originals_from_file( $file_name );
  15  
  16      /**
  17       * Gets the list of supported file extensions.
  18       *
  19       * @since 2.0.0
  20       *
  21       * @return array Supported file extensions.
  22       */
  23  	public function get_file_extensions() {
  24          return array_merge( array( $this->extension ), $this->alt_extensions );
  25      }
  26  
  27  	public function read_translations_from_file( $file_name, $project = null ) {
  28          if ( is_null( $project ) ) {
  29              return false;
  30          }
  31  
  32          $translations = $this->read_originals_from_file( $file_name );
  33  
  34          if ( ! $translations ) {
  35              return false;
  36          }
  37  
  38          $originals        = GP::$original->by_project_id( $project->id );
  39          $new_translations = new Translations;
  40  
  41          foreach( $translations->entries as $key => $entry ) {
  42              // we have been using read_originals_from_file to parse the file
  43              // so we need to swap singular and translation
  44              if ( $entry->context == $entry->singular ) {
  45                  $entry->translations = array();
  46              } else {
  47                  $entry->translations = array( $entry->singular );
  48              }
  49  
  50              $entry->singular = null;
  51  
  52              foreach( $originals as $original ) {
  53                  if ( $original->context == $entry->context ) {
  54                      $entry->singular = $original->singular;
  55                      break;
  56                  }
  57              }
  58  
  59              if ( ! $entry->singular ) {
  60                  error_log( sprintf( __( 'Missing context %s in project #%d', 'glotpress' ), $entry->context, $project->id ) );
  61                  continue;
  62              }
  63  
  64              $new_translations->add_entry( $entry );
  65          }
  66  
  67          return $new_translations;
  68      }
  69  
  70      /**
  71       * Create a string that represents the value for the "Language:" header for an export file.
  72       *
  73       * @since 2.1.0
  74       *
  75       * @param GP_Locale $locale The locale object.
  76       *
  77       * @return string|false Returns false if the locale object does not have any iso_639 language code, otherwise returns the shortest possible language code string.
  78       */
  79  	protected function get_language_code( $locale ) {
  80          $ret = '';
  81  
  82          if ( $locale->lang_code_iso_639_1 ) {
  83              $ret = $locale->lang_code_iso_639_1;
  84          } elseif ( $locale->lang_code_iso_639_2 ) {
  85              $ret = $locale->lang_code_iso_639_2;
  86          } elseif ( $locale->lang_code_iso_639_3 ) {
  87              $ret = $locale->lang_code_iso_639_3;
  88          }
  89  
  90          if ( '' === $ret ) {
  91              return false;
  92          }
  93  
  94          $ret = strtolower( $ret );
  95  
  96          if ( null !== $locale->country_code && 0 !== strcasecmp( $ret, $locale->country_code ) ) {
  97              $ret .= '_' . strtoupper( $locale->country_code );
  98          }
  99  
 100          return $ret;
 101      }
 102  
 103  }


Generated: Sat Aug 17 01:01:56 2019 Cross-referenced by PHPXref 0.7.1