[ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * GlotPress Format JSON class 4 * 5 * @since 2.3.0 6 * 7 * @package GlotPress 8 */ 9 10 /** 11 * Format class used to support JSON file format. 12 * 13 * @since 2.3.0 14 */ 15 class GP_Format_JSON extends GP_Format { 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 = 'JSON (.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 = 'json'; 33 34 /** 35 * Generates a string the contains the $entries to export in the 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 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 ? $entry->context . chr( 4 ) . $entry->singular : $entry->singular; 55 56 $result[ $key ] = array_filter( 57 $entry->translations, 58 function ( $translation ) { 59 return null !== $translation; 60 } 61 ); 62 } 63 64 /** 65 * Filter whether the exported JSON should be pretty printed. 66 * 67 * @since 2.3.0 68 * 69 * @param bool $pretty_print Whether pretty print should be enabled or not. Default false. 70 */ 71 $pretty_print = apply_filters( 'gp_json_export_pretty_print', false ); 72 73 return wp_json_encode( $result, ( true === $pretty_print ) ? JSON_PRETTY_PRINT : 0 ); 74 } 75 76 /** 77 * Reads a set of original strings from a JSON file. 78 * 79 * @since 2.3.0 80 * 81 * @param string $file_name The name of the uploaded JSON file. 82 * @return Translations|bool The extracted originals on success, false on failure. 83 */ 84 public function read_originals_from_file( $file_name ) { 85 $json = $this->decode_json_file( $file_name ); 86 87 if ( ! $json ) { 88 return false; 89 } 90 91 $entries = new Translations(); 92 93 foreach ( $json as $key => $value ) { 94 if ( '' === $key ) { 95 continue; 96 } 97 98 $args = array( 99 'singular' => $key, 100 ); 101 102 if ( false !== strpos( $key, chr( 4 ) ) ) { 103 $key = explode( chr( 4 ), $key ); 104 $args['context'] = $key[0]; 105 $args['singular'] = $key[1]; 106 } 107 108 $args['translations'] = (array) $value; 109 110 $entries->add_entry( new Translation_Entry( $args ) ); 111 } 112 113 return $entries; 114 } 115 116 /** 117 * Reads a set of translations from a JSON file. 118 * 119 * @since 2.3.0 120 * 121 * @param string $file_name The name of the uploaded properties file. 122 * @param GP_Project $project Unused. The project object to read the translations into. 123 * @return Translations|bool The extracted translations on success, false on failure. 124 */ 125 public function read_translations_from_file( $file_name, $project = null ) { 126 return $this->read_originals_from_file( $file_name ); 127 } 128 129 /** 130 * Loads a given JSON file and decodes its content. 131 * 132 * @since 2.3.0 133 * 134 * @param string $file_name The name of the JSON file to parse. 135 * @return array|false The encoded value or false on failure. 136 */ 137 protected function decode_json_file( $file_name ) { 138 if ( ! file_exists( $file_name ) ) { 139 return false; 140 } 141 142 $file = file_get_contents( $file_name ); 143 144 if ( ! $file ) { 145 return false; 146 } 147 148 $json = json_decode( $file, true ); 149 150 if ( null === $json ) { 151 return false; 152 } 153 154 return $json; 155 } 156 } 157 158 GP::$formats['json'] = new GP_Format_JSON();
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:01:07 2024 | Cross-referenced by PHPXref 0.7.1 |