[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  
   3  class GP_Format_ResX extends GP_Format {
   4  
   5      public $name           = '.NET Resource (.resx)';
   6      public $extension      = 'resx';
   7      public $alt_extensions = array( 'resx.xml' );
   8  
   9      public $exported = '';
  10  
  11  	public function print_exported_file( $project, $locale, $translation_set, $entries ) {
  12          $this->exported = '';
  13          $this->line( '<?xml version="1.0" encoding="utf-8"?>' );
  14          $this->line( '<root>' );
  15  
  16          $this->add_schema_info();
  17          $this->add_schema_declaration();
  18  
  19          $this->res_header( 'resmimetype', 'text/microsoft-resx' );
  20          $this->res_header( 'version', '2.0' );
  21          $this->res_header( 'reader', 'System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' );
  22          $this->res_header( 'writer', 'System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' );
  23          $this->res_header( 'translation_revision_date', GP::$translation->last_modified( $translation_set ) . '+0000' );
  24          $this->res_header( 'plural_forms', "nplurals={$locale->nplurals}; plural={$locale->plural_expression};" );
  25          $this->res_header( 'generator', 'GlotPress/' . GP_VERSION );
  26  
  27          $language_code = $this->get_language_code( $locale );
  28          if ( false !== $language_code ) {
  29              $this->res_header( 'language', $language_code );
  30          }
  31  
  32          foreach ( $entries as $entry ) {
  33              if ( empty( $entry->translations ) || ! array_filter( $entry->translations ) ) {
  34                  continue;
  35              }
  36  
  37              if ( empty( $entry->context ) ) {
  38                  $entry->context = $entry->singular;
  39              }
  40  
  41              $this->line( '<data name="' . esc_attr( $entry->context ) . '" xml:space="preserve">', 1 );
  42              $this->line( '<value>' . $this->escape( $entry->translations[0] ) . '</value>', 2 );
  43              if ( isset( $entry->extracted_comments ) && $entry->extracted_comments ) {
  44                  $this->line( '<comment>' . $this->escape( $entry->extracted_comments ) . '</comment>', 2 );
  45              }
  46              $this->line( '</data>', 1 );
  47          }
  48          $this->line( '</root>' );
  49          return $this->exported;
  50      }
  51  
  52  	public function read_originals_from_file( $file_name ) {
  53          $errors = libxml_use_internal_errors( true );
  54          $data   = simplexml_load_string( file_get_contents( $file_name ) );
  55          libxml_use_internal_errors( $errors );
  56  
  57          if ( ! is_object( $data ) ) {
  58              return false;
  59          }
  60  
  61          $entries = new Translations();
  62          foreach ( $data->data as $string ) {
  63              $entry = new Translation_Entry();
  64  
  65              if ( isset( $string['type'] ) && gp_in( 'System.Resources.ResXFileRef', (string) $string['type'] ) ) {
  66                  continue;
  67              }
  68  
  69              $entry->context  = (string) $string['name'];
  70              $entry->singular = $this->unescape( (string) $string->value );
  71  
  72              if ( isset( $string->comment ) && $string->comment ) {
  73                  $entry->extracted_comments = (string) $string->comment;
  74              }
  75  
  76              $entry->translations = array();
  77              $entries->add_entry( $entry );
  78          }
  79  
  80          return $entries;
  81      }
  82  
  83  
  84  	private function line( $string, $prepend_tabs = 0 ) {
  85          $this->exported .= str_repeat( "\t", $prepend_tabs ) . "$string\n";
  86      }
  87  
  88  	private function res_header( $name, $value ) {
  89          $this->line( '<resheader name="' . $name . '">', 1 );
  90          $this->line( '<value>' . $value . '</value>', 2 );
  91          $this->line( '</resheader>', 1 );
  92      }
  93  
  94  	private function unescape( $string ) {
  95          return $string;
  96      }
  97  
  98  	private function escape( $string ) {
  99          $string = str_replace( array( '&', '<' ), array( '&amp;', '&lt;' ), $string );
 100          return $string;
 101      }
 102  
 103  
 104  	private function add_schema_info() {
 105          $this->line( '<!--', 1 );
 106          $this->line( 'Microsoft ResX Schema', 2 );
 107          $this->line( '', 0 );
 108          $this->line( 'Version 2.0', 2 );
 109          $this->line( '', 0 );
 110          $this->line( 'The primary goals of this format is to allow a simple XML format', 2 );
 111          $this->line( 'that is mostly human readable. The generation and parsing of the', 2 );
 112          $this->line( 'various data types are done through the TypeConverter classes', 2 );
 113          $this->line( 'associated with the data types.', 2 );
 114          $this->line( '', 0 );
 115          $this->line( 'Example:', 2 );
 116          $this->line( '', 0 );
 117          $this->line( '... ado.net/XML headers & schema ...', 2 );
 118          $this->line( '<resheader name="resmimetype">text/microsoft-resx</resheader>', 2 );
 119          $this->line( '<resheader name="version">2.0</resheader>', 2 );
 120          $this->line( '<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>', 2 );
 121          $this->line( '<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>', 2 );
 122          $this->line( '<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>', 2 );
 123          $this->line( '<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>', 2 );
 124          $this->line( '<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">', 2 );
 125          $this->line( '<value>[base64 mime encoded serialized .NET Framework object]</value>', 3 );
 126          $this->line( '</data>', 2 );
 127          $this->line( '<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">', 2 );
 128          $this->line( '<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>', 3 );
 129          $this->line( '<comment>This is a comment</comment>', 3 );
 130          $this->line( '</data>', 2 );
 131          $this->line( '', 0 );
 132          $this->line( 'There are any number of "resheader" rows that contain simple', 2 );
 133          $this->line( 'name/value pairs.', 2 );
 134          $this->line( '', 0 );
 135          $this->line( 'Each data row contains a name, and value. The row also contains a', 2 );
 136          $this->line( 'type or mimetype. Type corresponds to a .NET class that support', 2 );
 137          $this->line( 'text/value conversion through the TypeConverter architecture.', 2 );
 138          $this->line( 'Classes that don\'t support this are serialized and stored with the', 2 );
 139          $this->line( 'mimetype set.', 2 );
 140          $this->line( '', 0 );
 141          $this->line( 'The mimetype is used for serialized objects, and tells the', 2 );
 142          $this->line( 'ResXResourceReader how to depersist the object. This is currently not', 2 );
 143          $this->line( 'extensible. For a given mimetype the value must be set accordingly:', 2 );
 144          $this->line( '', 0 );
 145          $this->line( 'Note - application/x-microsoft.net.object.binary.base64 is the format', 2 );
 146          $this->line( 'that the ResXResourceWriter will generate, however the reader can', 2 );
 147          $this->line( 'read any of the formats listed below.', 2 );
 148          $this->line( '', 0 );
 149          $this->line( 'mimetype: application/x-microsoft.net.object.binary.base64', 2 );
 150          $this->line( 'value   : The object must be serialized with', 2 );
 151          $this->line( ': System.Runtime.Serialization.Formatters.Binary.BinaryFormatter', 4 );
 152          $this->line( ': and then encoded with base64 encoding.', 4 );
 153          $this->line( '', 0 );
 154          $this->line( 'mimetype: application/x-microsoft.net.object.soap.base64', 2 );
 155          $this->line( 'value   : The object must be serialized with', 2 );
 156          $this->line( ': System.Runtime.Serialization.Formatters.Soap.SoapFormatter', 4 );
 157          $this->line( ': and then encoded with base64 encoding.', 4 );
 158          $this->line( '', 0 );
 159          $this->line( 'mimetype: application/x-microsoft.net.object.bytearray.base64', 2 );
 160          $this->line( 'value   : The object must be serialized into a byte array', 2 );
 161          $this->line( ': using a System.ComponentModel.TypeConverter', 4 );
 162          $this->line( ': and then encoded with base64 encoding.', 4 );
 163          $this->line( '-->', 1 );
 164      }
 165  
 166  	private function add_schema_declaration() {
 167          $this->line( '<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">', 1 );
 168          $this->line( '<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />', 2 );
 169          $this->line( '<xsd:element name="root" msdata:IsDataSet="true">', 2 );
 170          $this->line( '<xsd:complexType>', 3 );
 171          $this->line( '<xsd:choice maxOccurs="unbounded">', 4 );
 172  
 173          $this->line( '<xsd:element name="metadata">', 5 );
 174          $this->line( '<xsd:complexType>', 6 );
 175          $this->line( '<xsd:sequence>', 7 );
 176          $this->line( '<xsd:element name="value" type="xsd:string" minOccurs="0" />', 8 );
 177          $this->line( '</xsd:sequence>', 7 );
 178          $this->line( '<xsd:attribute name="name" use="required" type="xsd:string" />', 7 );
 179          $this->line( '<xsd:attribute name="type" type="xsd:string" />', 7 );
 180          $this->line( '<xsd:attribute name="mimetype" type="xsd:string" />', 7 );
 181          $this->line( '<xsd:attribute ref="xml:space" />', 7 );
 182          $this->line( '</xsd:complexType>', 6 );
 183          $this->line( '</xsd:element>', 5 );
 184  
 185          $this->line( '<xsd:element name="assembly">', 5 );
 186          $this->line( '<xsd:complexType>', 6 );
 187          $this->line( '<xsd:attribute name="alias" type="xsd:string" />', 7 );
 188          $this->line( '<xsd:attribute name="name" type="xsd:string" />', 7 );
 189          $this->line( '</xsd:complexType>', 6 );
 190          $this->line( '</xsd:element>', 5 );
 191  
 192          $this->line( '<xsd:element name="data">', 5 );
 193          $this->line( '<xsd:complexType>', 6 );
 194          $this->line( '<xsd:sequence>', 7 );
 195          $this->line( '<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />', 8 );
 196          $this->line( '<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />', 8 );
 197          $this->line( '</xsd:sequence>', 7 );
 198          $this->line( '<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />', 7 );
 199          $this->line( '<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />', 7 );
 200          $this->line( '<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />', 7 );
 201          $this->line( '<xsd:attribute ref="xml:space" />', 7 );
 202          $this->line( '</xsd:complexType>', 6 );
 203          $this->line( '</xsd:element>', 5 );
 204  
 205          $this->line( '<xsd:element name="resheader">', 5 );
 206          $this->line( '<xsd:complexType>', 6 );
 207          $this->line( '<xsd:sequence>', 7 );
 208          $this->line( '<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />', 8 );
 209          $this->line( '</xsd:sequence>', 7 );
 210          $this->line( '<xsd:attribute name="name" type="xsd:string" use="required" />', 7 );
 211          $this->line( '</xsd:complexType>', 6 );
 212          $this->line( '</xsd:element>', 5 );
 213  
 214          $this->line( '</xsd:choice>', 4 );
 215          $this->line( '</xsd:complexType>', 3 );
 216          $this->line( '</xsd:element>', 2 );
 217          $this->line( '</xsd:schema>', 1 );
 218      }
 219  
 220  }
 221  
 222  GP::$formats['resx'] = new GP_Format_ResX();


Generated: Sat Sep 19 01:01:55 2020 Cross-referenced by PHPXref 0.7.1