[ 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              $context     = str_replace( "\n", "\\n", $this->escape( $entry->context ) );
  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\"$context\" = \"$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-16/32 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 UTF-16BE. To remain backwards compatible we support both for importing,
 104           * but we only export UTF-8.
 105           */
 106          foreach (
 107              array(
 108                  // See https://www.php.net/manual/en/function.mb-detect-encoding.php#91051.
 109                  'UTF-8'    => chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ),
 110                  'UTF-32BE' => chr( 0x00 ) . chr( 0x00 ) . chr( 0xFE ) . chr( 0xFF ),
 111                  'UTF-32LE' => chr( 0xFF ) . chr( 0xFE ) . chr( 0x00 ) . chr( 0x00 ),
 112                  'UTF-16LE' => chr( 0xFF ) . chr( 0xFE ),
 113                  'UTF-16BE' => chr( 0xFE ) . chr( 0xFF ),
 114              ) as $encoding => $bom
 115          ) {
 116              if ( 0 === strpos( $file, $bom ) ) {
 117                  if ( 'UTF-8' !== $encoding ) {
 118                      $file = mb_convert_encoding( $file, 'UTF-8', $encoding );
 119                  }
 120                  break;
 121              }
 122          }
 123  
 124          // Convert multi-line comments into a single line.
 125          $file = preg_replace_callback(
 126              '/\/\*\s*(.*?)\s*\*\//s',
 127              function( $m ) {
 128                  return str_replace( PHP_EOL, '\n', $m[0] );
 129              },
 130              $file
 131          );
 132  
 133          $context = $comment = null;
 134          $lines   = explode( "\n", $file );
 135  
 136          foreach ( $lines as $line ) {
 137              if ( is_null( $context ) ) {
 138                  if ( preg_match( '/^\/\*\s*(.*)\s*\*\/$/', $line, $matches ) ) {
 139                      $matches[1] = trim( str_replace( '\n', PHP_EOL, $matches[1] ) );
 140  
 141                      if ( 'No comment provided by engineer.' !== $matches[1] ) {
 142                          $comment = $matches[1];
 143                      } else {
 144                          $comment = null;
 145                      }
 146                  } elseif ( preg_match( '/^"(.*)" = "(.*)";$/', $line, $matches ) ) {
 147                      $entry           = new Translation_Entry();
 148                      $entry->context  = $this->unescape( $matches[1] );
 149                      $entry->singular = $this->unescape( $matches[2] );
 150  
 151                      if ( ! is_null( $comment ) ) {
 152                          $entry->extracted_comments = $comment;
 153                          $comment                   = null;
 154                      }
 155  
 156                      $entry->translations = array();
 157                      $entries->add_entry( $entry );
 158                  }
 159              }
 160          }
 161  
 162          return $entries;
 163      }
 164  
 165      /**
 166       * Sorts the translation entries based on the context attribute.
 167       *
 168       * @since 1.0.0
 169       *
 170       * @param string $a First string to compare.
 171       * @param string $b Second string to compare.
 172       * @return int +1 or -1 based on the order to sort.
 173       */
 174  	private function sort_entries( $a, $b ) {
 175          if ( $a->context == $b->context ) {
 176              return 0;
 177          }
 178  
 179          return ( $a->context > $b->context ) ? +1 : -1;
 180      }
 181  
 182      /**
 183       * Strips any escaping from a string.
 184       *
 185       * @since 1.0.0
 186       *
 187       * @param string $string The string to strip escapes from.
 188       * @return string The unescaped string.
 189       */
 190  	private function unescape( $string ) {
 191          return stripcslashes( $string );
 192      }
 193  
 194      /**
 195       * Adds escaping to a string.
 196       *
 197       * @since 1.0.0
 198       *
 199       * @param string $string The string to add escapes to.
 200       * @return string The escaped string.
 201       */
 202  	private function escape( $string ) {
 203          return addcslashes( $string, '"\\/' );
 204      }
 205  
 206  }
 207  
 208  GP::$formats['strings'] = new GP_Format_Strings();


Generated: Thu Nov 21 01:01:07 2024 Cross-referenced by PHPXref 0.7.1