[ 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      abstract public function print_exported_file( $project, $locale, $translation_set, $entries );
  14      abstract public 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(
  61                      sprintf(
  62                          /* translators: 1: Context. 2: Project ID. */
  63                          __( 'Missing context %1$s in project #%2$d', 'glotpress' ),
  64                          $entry->context,
  65                          $project->id
  66                      )
  67                  );
  68                  continue;
  69              }
  70  
  71              $new_translations->add_entry( $entry );
  72          }
  73  
  74          return $new_translations;
  75      }
  76  
  77      /**
  78       * Create a string that represents the value for the "Language:" header for an export file.
  79       *
  80       * @since 2.1.0
  81       *
  82       * @param GP_Locale $locale The locale object.
  83       *
  84       * @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.
  85       */
  86  	protected function get_language_code( $locale ) {
  87          $ret = '';
  88  
  89          if ( $locale->lang_code_iso_639_1 ) {
  90              $ret = $locale->lang_code_iso_639_1;
  91          } elseif ( $locale->lang_code_iso_639_2 ) {
  92              $ret = $locale->lang_code_iso_639_2;
  93          } elseif ( $locale->lang_code_iso_639_3 ) {
  94              $ret = $locale->lang_code_iso_639_3;
  95          }
  96  
  97          if ( '' === $ret ) {
  98              return false;
  99          }
 100  
 101          $ret = strtolower( $ret );
 102  
 103          if ( null !== $locale->country_code && 0 !== strcasecmp( $ret, $locale->country_code ) ) {
 104              $ret .= '_' . strtoupper( $locale->country_code );
 105          }
 106  
 107          return $ret;
 108      }
 109  
 110  }


Generated: Thu Nov 21 01:01:07 2024 Cross-referenced by PHPXref 0.7.1