[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * GlotPress Format Mac OSX / iOS Strings Translate class
   4   *
   5   * @since 1.0.0
   6   *
   7   * @package GlotPress
   8   */
   9  
  10  /**
  11   * Format class used to support Mac OS X / iOS Translate strings file format.
  12   *
  13   * @since 1.0.0
  14   */
  15  class GP_Format_Strings extends GP_Format {
  16      /**
  17       * Name of file format, used in file format dropdowns.
  18       *
  19       * @since 1.0.0
  20       *
  21       * @var string
  22       */
  23      public $name = 'Mac OS X / iOS Strings File (.strings)';
  24  
  25      /**
  26       * File extension of the file format, used to autodetect formats and when creating the output file names.
  27       *
  28       * @since 1.0.0
  29       *
  30       * @var string
  31       */
  32      public $extension = 'strings';
  33  
  34      /**
  35       * Generates a string the contains the $entries to export in the strings file format.
  36       *
  37       * @since 1.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 strings string.
  48       */
  49  	public function print_exported_file( $project, $locale, $translation_set, $entries ) {
  50          $result = '';
  51  
  52          $result .= '/* Translation-Revision-Date: ' . GP::$translation->last_modified( $translation_set ) . "+0000 */\n";
  53          $result .= "/* Plural-Forms: nplurals={$locale->nplurals}; plural={$locale->plural_expression}; */\n";
  54          $result .= '/* Generator: GlotPress/' . GP_VERSION . " */\n";
  55  
  56          $language_code = $this->get_language_code( $locale );
  57          if ( false !== $language_code ) {
  58              $result .= '/* Language: ' . $language_code . " */\n";
  59          }
  60  
  61          $result .= "\n";
  62  
  63          $sorted_entries = $entries;
  64          usort( $sorted_entries, array( 'GP_Format_Strings', 'sort_entries' ) );
  65  
  66          foreach ( $sorted_entries as $entry ) {
  67              $translation = $this->escape( empty( $entry->translations ) ? $entry->singular : $entry->translations[0] );
  68  
  69              $original    = str_replace( "\n", "\\n", $this->escape( $entry->singular ) );
  70              $translation = str_replace( "\n", "\\n", $translation );
  71              $comment     = preg_replace( '/(^\s+)|(\s+$)/us', '', $entry->extracted_comments );
  72  
  73              if ( $comment == "" ) {
  74                  $comment = "No comment provided by engineer.";
  75              }
  76  
  77              $result .= "/* $comment */\n\"$original\" = \"$translation\";\n\n";
  78          }
  79  
  80          return $result;
  81      }
  82  
  83      /**
  84       * Reads a set of original strings from a strings file.
  85       *
  86       * @since 1.0.0
  87       *
  88       * @param string $file_name The name of the uploaded strings file.
  89       * @return Translations|bool The extracted originals on success, false on failure.
  90       */
  91  	public function read_originals_from_file( $file_name ) {
  92          $entries = new Translations;
  93          $file = file_get_contents( $file_name );
  94  
  95          if ( false === $file ) {
  96              return false;
  97          }
  98  
  99          /**
 100           * Check to see if the input file is UTF-16LE encoded, if so convert it to UTF-8.
 101           *
 102           * Note, Apple recommends UTF-8 but some of their tools (like genstrings) export
 103           * UTF-16LE (or BE, but GP has never supported that) so to remain backwards
 104           * compatible we support both for importing, but we only export UTF-8.
 105           */
 106          if ( mb_check_encoding( $file, 'UTF-16LE' ) ) {
 107              $file = mb_convert_encoding( $file, 'UTF-8', 'UTF-16LE' );
 108          }
 109  
 110          // Convert multi-line comments into a single line.
 111          $file = preg_replace_callback(
 112              '/\/\*\s*(.*?)\s*\*\//s',
 113              function( $m ) {
 114                  return str_replace( PHP_EOL, '\n', $m[0] );
 115              },
 116              $file
 117          );
 118  
 119          $context = $comment = null;
 120          $lines = explode( "\n", $file );
 121  
 122          foreach ( $lines as $line ) {
 123              if ( is_null( $context ) ) {
 124                  if ( preg_match( '/^\/\*\s*(.*)\s*\*\/$/', $line, $matches ) ) {
 125                      $matches[1] = trim( str_replace( '\n', PHP_EOL, $matches[1] ) );
 126  
 127                      if ( $matches[1] !== "No comment provided by engineer." ) {
 128                          $comment = $matches[1];
 129                      } else {
 130                          $comment = null;
 131                      }
 132                  } else if ( preg_match( '/^"(.*)" = "(.*)";$/', $line, $matches ) ) {
 133                      $entry = new Translation_Entry();
 134                      $entry->context = $this->unescape( $matches[1] );
 135                      $entry->singular = $this->unescape( $matches[2] );
 136  
 137                      if ( ! is_null( $comment )) {
 138                          $entry->extracted_comments = $comment;
 139                          $comment = null;
 140                      }
 141  
 142                      $entry->translations = array();
 143                      $entries->add_entry( $entry );
 144                  }
 145              }
 146          }
 147  
 148          return $entries;
 149      }
 150  
 151      /**
 152       * Sorts the translation entries based on the context attribute.
 153       *
 154       * @since 1.0.0
 155       *
 156       * @param string $a First string to compare.
 157       * @param string $b Second string to compare.
 158       * @return int +1 or -1 based on the order to sort.
 159       */
 160  	private function sort_entries( $a, $b ) {
 161          if ( $a->context == $b->context ) {
 162              return 0;
 163          }
 164  
 165          return ( $a->context > $b->context ) ? +1 : -1;
 166      }
 167  
 168      /**
 169       * Strips any escaping from a string.
 170       *
 171       * @since 1.0.0
 172       *
 173       * @param string $string The string to strip escapes from.
 174       * @return string The unescaped string.
 175       */
 176  	private function unescape( $string ) {
 177          return stripcslashes( $string );
 178      }
 179  
 180      /**
 181       * Adds escaping to a string.
 182       *
 183       * @since 1.0.0
 184       *
 185       * @param string $string The string to add escapes to.
 186       * @return string The escaped string.
 187       */
 188  	private function escape( $string ) {
 189          return addcslashes( $string, '"\\/' );
 190      }
 191  
 192  }
 193  
 194  GP::$formats['strings'] = new GP_Format_Strings;


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