[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * GlotPress Format NGX Translate class
   4   *
   5   * @since 3.0.0
   6   *
   7   * @package GlotPress
   8   */
   9  
  10  /**
  11   * Format class used to support NGX Translate JSON file format.
  12   *
  13   * @since 3.0.0
  14   */
  15  class GP_Format_NGX extends GP_Format {
  16      /**
  17       * Name of file format, used in file format dropdowns.
  18       *
  19       * @since 3.0.0
  20       *
  21       * @var string
  22       */
  23      public $name = 'NGX-Translate (.json)';
  24  
  25      /**
  26       * File extension of the file format, used to autodetect formats and when creating the output file names.
  27       *
  28       * @since 3.0.0
  29       *
  30       * @var string
  31       */
  32      public $extension = 'ngx.json';
  33  
  34      /**
  35       * Generates a string the contains the $entries to export in the JSON file format.
  36       *
  37       * @since 3.0.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 JSON string.
  48       */
  49  	public function print_exported_file( $project, $locale, $translation_set, $entries ) {
  50          $result = array();
  51  
  52          /* @var Translation_Entry $entry */
  53          foreach ( $entries as $entry ) {
  54              $key           = $entry->context;
  55              $arraykeyindex = strpos( $key, '[' );
  56              if ( false !== $arraykeyindex ) {
  57                  $entrykey  = substr( $key, 0, $arraykeyindex );
  58                  $keypair   = substr( $key, $arraykeyindex + 1, strlen( $key ) - $arraykeyindex - 2 );
  59                  $valuepair = array(
  60                      'key'         => $keypair,
  61                      'translation' => $entry->translations[0],
  62                  );
  63                  if ( is_array( $result[ $entrykey ] ) ) {
  64                      array_push( $result[ $entrykey ], $valuepair );
  65                  } else {
  66                      $result[ $entrykey ] = array();
  67                      array_push( $result[ $entrykey ], $valuepair );
  68                  }
  69              } else {
  70                  if ( null === $key ) {
  71                      $key = $entry->singular;
  72                  }
  73  
  74                  $result[ $key ] = $entry->translations[0];
  75              }
  76          }
  77  
  78          /**
  79           * Filter whether the exported JSON should be pretty printed.
  80           *
  81           * @since 3.0.0
  82           *
  83           * @param bool $pretty_print Whether pretty print should be enabled or not. Default false.
  84           */
  85          $pretty_print = apply_filters( 'gp_json_export_pretty_print', false );
  86  
  87          return wp_json_encode( $result, ( true === $pretty_print ) ? JSON_PRETTY_PRINT : 0 );
  88      }
  89  
  90      /**
  91       * Reads a set of original strings from a JSON file.
  92       *
  93       * @since 3.0.0
  94       *
  95       * @param string $file_name The name of the uploaded JSON file.
  96       * @return Translations|bool The extracted originals on success, false on failure.
  97       */
  98  	public function read_originals_from_file( $file_name ) {
  99          $json = $this->decode_json_file( $file_name );
 100          if ( ! $json ) {
 101              return false;
 102          }
 103          $entries = new Translations();
 104          foreach ( $json as $key => $value ) {
 105              if ( '' === $key ) {
 106                  continue;
 107              }
 108              if ( is_array( $value ) ) {
 109                  foreach ( $value as $keyelem => $valueelem ) {
 110                      if ( isset( $valueelem['key'] )
 111                        && isset( $valueelem['translation'] ) ) {
 112                          $args = array(
 113                              'singular' => $valueelem['translation'],
 114                              'context'  => $key . '[' . $valueelem['key'] . ']',
 115                          );
 116                          $entries->add_entry( new Translation_Entry( $args ) );
 117                      }
 118                  };
 119              } else {
 120                  $args = array(
 121                      'singular' => $value,
 122                      'context'  => $key,
 123                  );
 124                  $entries->add_entry( new Translation_Entry( $args ) );
 125              }
 126          }
 127  
 128          return $entries;
 129      }
 130  
 131      /**
 132       * Decode a JSON file.
 133       *
 134       * @since 3.0.0
 135       *
 136       * @param string $file_name The name of the JSON file to decode.
 137       * @return decode JSON file as an array.
 138       */
 139  	protected function decode_json_file( $file_name ) {
 140          if ( ! file_exists( $file_name ) ) {
 141              return false;
 142          }
 143          $file = file_get_contents( $file_name );
 144  
 145          if ( ! $file ) {
 146              return false;
 147          }
 148          $json = json_decode( $file, true );
 149  
 150          if ( null === $json ) {
 151              return false;
 152          }
 153          return $json;
 154      }
 155  }
 156  
 157  GP::$formats['ngx'] = new GP_Format_NGX();


Generated: Thu Dec 26 01:01:15 2024 Cross-referenced by PHPXref 0.7.1