[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * GlotPress Format Jed 1.x class
   4   *
   5   * @since 2.3.0
   6   *
   7   * @package GlotPress
   8   */
   9  
  10  /**
  11   * Format class used to support Jed 1.x compatible JSON file format.
  12   *
  13   * @since 2.3.0
  14   */
  15  class GP_Format_Jed1x extends GP_Format_JSON {
  16      /**
  17       * Name of file format, used in file format dropdowns.
  18       *
  19       * @since 2.3.0
  20       *
  21       * @var string
  22       */
  23      public $name = 'Jed 1.x (.json)';
  24  
  25      /**
  26       * File extension of the file format, used to autodetect formats and when creating the output file names.
  27       *
  28       * @since 2.3.0
  29       *
  30       * @var string
  31       */
  32      public $extension = 'jed.json';
  33  
  34      /**
  35       * Generates a string the contains the $entries to export in the Jed 1.x compatible JSON file format.
  36       *
  37       * @since 2.3.0
  38       *
  39       * @param GP_Project         $project         The project the strings are being exported for, not used
  40       *                                            in this format but part of the scaffold of the parent object.
  41       * @param GP_Locale          $locale          The locale object the strings are being exported for, not used
  42       *                                            in this format but part of the scaffold of the parent object.
  43       * @param GP_Translation_Set $translation_set The locale object the strings are being
  44       *                                            exported for. not used in this format but part
  45       *                                            of the scaffold of the parent object.
  46       * @param GP_Translation     $entries         The entries to export.
  47       * @return string The exported Jed 1.x compatible JSON string.
  48       */
  49  	public function print_exported_file( $project, $locale, $translation_set, $entries ) {
  50          $language_code = $this->get_language_code( $locale );
  51          if ( false === $language_code ) {
  52              $language_code = $locale->slug;
  53          }
  54  
  55          $result = array(
  56              'translation-revision-date' => GP::$translation->last_modified( $translation_set ) . '+0000',
  57              'generator'                 => 'GlotPress/' . GP_VERSION,
  58              'domain'                    => 'messages',
  59              'locale_data'               => array(
  60                  'messages' => array(
  61                      '__GP_EMPTY__' => array(
  62                          'domain'       => 'messages',
  63                          'plural-forms' => sprintf( 'nplurals=%1$s; plural=%2$s;', $locale->nplurals, $locale->plural_expression ),
  64                          'lang'         => $language_code,
  65                      ),
  66                  ),
  67              ),
  68          );
  69  
  70          /* @var Translation_Entry $entry */
  71          foreach ( $entries as $entry ) {
  72              $key = $entry->context ? $entry->context . chr( 4 ) . $entry->singular : $entry->singular;
  73  
  74              $result['locale_data']['messages'][ $key ] = array_filter( $entry->translations, function ( $translation ) {
  75                  return null !== $translation;
  76              } );
  77          }
  78  
  79          /** This filter is documented in gp-includes/formats/format-json.php */
  80          $pretty_print = apply_filters( 'gp_json_export_pretty_print', false );
  81  
  82          $result = wp_json_encode( $result, ( true === $pretty_print ) ? JSON_PRETTY_PRINT : 0 );
  83  
  84          /*
  85           * Replace '__GP_EMPTY__' with an actual empty string.
  86           *
  87           * Empty object property names are not supported in PHP, so they would get lost.
  88           *
  89           * Note: When decoding, PHP replaces empty strings with '_empty_'.
  90           *
  91           * @link https://bugs.php.net/bug.php?id=50867
  92           */
  93          return str_replace( '__GP_EMPTY__', '', $result );
  94      }
  95  
  96      /**
  97       * Reads a set of original strings from a JSON file.
  98       *
  99       * @since 2.3.0
 100       *
 101       * @param string $file_name The name of the uploaded JSON file.
 102       * @return Translations|bool The extracted originals on success, false on failure.
 103       */
 104  	public function read_originals_from_file( $file_name ) {
 105          $json = $this->decode_json_file( $file_name );
 106  
 107          if ( ! $json ) {
 108              return false;
 109          }
 110  
 111          $entries = new Translations();
 112  
 113          foreach ( $json['locale_data'][ $json['domain'] ] as $key => $value ) {
 114              if ( '' === $key ) {
 115                  continue;
 116              }
 117  
 118              $args = array(
 119                  'singular' => $key,
 120              );
 121  
 122              if ( false !== strpos( $key, chr( 4 ) ) ) {
 123                  $key              = explode( chr( 4 ), $key );
 124                  $args['context']  = $key[0];
 125                  $args['singular'] = $key[1];
 126              }
 127  
 128              $value = (array) $value;
 129  
 130              if ( isset( $value[0] ) ) {
 131                  $args['translations'] = $value[0];
 132              }
 133  
 134              if ( isset( $value[1] ) ) {
 135                  $args['plural'] = $value[1];
 136              }
 137  
 138              $entries->add_entry( new Translation_Entry( $args ) );
 139          }
 140  
 141          return $entries;
 142      }
 143  
 144      /**
 145       * Decodes a JSON string and checks for needed array keys.
 146       *
 147       * @since 2.3.0
 148       *
 149       * @param string $file_name The name of the JSON file to parse.
 150       * @return array|false The encoded value or false on failure.
 151       */
 152  	protected function decode_json_file( $file_name ) {
 153          $json = parent::decode_json_file( $file_name );
 154  
 155          if ( ! $json ) {
 156              return false;
 157          }
 158  
 159          if ( ! isset( $json['domain'] ) ||
 160               ! isset( $json['locale_data'] ) ||
 161               ! isset( $json['locale_data'][ $json['domain'] ] )
 162          ) {
 163              return false;
 164          }
 165  
 166          return $json;
 167      }
 168  }
 169  
 170  GP::$formats['jed1x'] = new GP_Format_Jed1x();


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