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


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