[ 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(
  75                  $entry->translations,
  76                  function ( $translation ) {
  77                      return null !== $translation;
  78                  }
  79              );
  80          }
  81  
  82          /** This filter is documented in gp-includes/formats/format-json.php */
  83          $pretty_print = apply_filters( 'gp_json_export_pretty_print', false );
  84  
  85          $result = wp_json_encode( $result, ( true === $pretty_print ) ? JSON_PRETTY_PRINT : 0 );
  86  
  87          /*
  88           * Replace '__GP_EMPTY__' with an actual empty string.
  89           *
  90           * Empty object property names are not supported in PHP, so they would get lost.
  91           *
  92           * Note: When decoding, PHP replaces empty strings with '_empty_'.
  93           *
  94           * @link https://bugs.php.net/bug.php?id=50867
  95           */
  96          return str_replace( '__GP_EMPTY__', '', $result );
  97      }
  98  
  99      /**
 100       * Reads a set of original strings from a JSON file.
 101       *
 102       * @since 2.3.0
 103       *
 104       * @param string $file_name The name of the uploaded JSON file.
 105       * @return Translations|bool The extracted originals on success, false on failure.
 106       */
 107  	public function read_originals_from_file( $file_name ) {
 108          $json = $this->decode_json_file( $file_name );
 109  
 110          if ( ! $json ) {
 111              return false;
 112          }
 113  
 114          $entries = new Translations();
 115  
 116          foreach ( $json['locale_data'][ $json['domain'] ] as $key => $value ) {
 117              if ( '' === $key ) {
 118                  continue;
 119              }
 120  
 121              $args = array(
 122                  'singular' => $key,
 123              );
 124  
 125              if ( false !== strpos( $key, chr( 4 ) ) ) {
 126                  $key              = explode( chr( 4 ), $key );
 127                  $args['context']  = $key[0];
 128                  $args['singular'] = $key[1];
 129              }
 130  
 131              $value = (array) $value;
 132  
 133              $args['translations'] = (array) $value;
 134  
 135              $entries->add_entry( new Translation_Entry( $args ) );
 136          }
 137  
 138          return $entries;
 139      }
 140  
 141      /**
 142       * Decodes a JSON string and checks for needed array keys.
 143       *
 144       * @since 2.3.0
 145       *
 146       * @param string $file_name The name of the JSON file to parse.
 147       * @return array|false The encoded value or false on failure.
 148       */
 149  	protected function decode_json_file( $file_name ) {
 150          $json = parent::decode_json_file( $file_name );
 151  
 152          if ( ! $json ) {
 153              return false;
 154          }
 155  
 156          if ( ! isset( $json['domain'] ) ||
 157               ! isset( $json['locale_data'] ) ||
 158               ! isset( $json['locale_data'][ $json['domain'] ] )
 159          ) {
 160              return false;
 161          }
 162  
 163          return $json;
 164      }
 165  }
 166  
 167  GP::$formats['jed1x'] = new GP_Format_Jed1x();


Generated: Tue Mar 19 01:01:21 2024 Cross-referenced by PHPXref 0.7.1