示例#1
0
	public function parseMessages( StringMangler $mangler ) {
		if ( $this->filename === false ) {
			return array();
		}

		$ { $this->variableName } = array();
		require( $this->filename );

		return $mangler->mangle( $ { $this->variableName } );
	}
	public function parseMessages( StringMangler $mangler ) {
		if ( $this->filename === false ) {
			return array();
		}

		$ { $this->variableName } = array();
		require( $this->filename );
		$messages = $ { $this->variableName } ;
		foreach ( $messages as $code => $value ) {
			$messages[$code] = $mangler->mangle( $value );
		}

		return $messages;
	}
 /**
  * Parses gettext file as string into internal representation.
  * @param string $data
  * @param bool $useCtxtAsKey Whether to create message keys from the context
  * or use msgctxt (non-standard po-files)
  * @param StringMangler $mangler
  * @param string $keyAlgorithm Key generation algorithm, see generateKeyFromItem
  * @throws MWException
  * @return array
  */
 public static function parseGettextData($data, $useCtxtAsKey, $mangler, $keyAlgorithm)
 {
     $potmode = false;
     // Normalise newlines, to make processing easier
     $data = str_replace("\r\n", "\n", $data);
     /* Delimit the file into sections, which are separated by two newlines.
      * We are permissive and accept more than two. This parsing method isn't
      * efficient wrt memory, but was easy to implement */
     $sections = preg_split('/\\n{2,}/', $data);
     /* First one isn't an actual message. We'll handle it specially below */
     $headerSection = array_shift($sections);
     /* Since this is the header section, we are only interested in the tags
      * and msgid is empty. Somewhere we should extract the header comments
      * too */
     $match = self::expectKeyword('msgstr', $headerSection);
     if ($match !== null) {
         $headerBlock = self::formatForWiki($match, 'trim');
         $headers = self::parseHeaderTags($headerBlock);
         // Check for pot-mode by checking if the header is fuzzy
         $flags = self::parseFlags($headerSection);
         if (in_array('fuzzy', $flags, true)) {
             $potmode = true;
         }
     } else {
         throw new MWException("Gettext file header was not found:\n\n{$data}");
     }
     $template = array();
     $messages = array();
     // Extract some metadata from headers for easier use
     $metadata = array();
     if (isset($headers['X-Language-Code'])) {
         $metadata['code'] = $headers['X-Language-Code'];
     }
     if (isset($headers['X-Message-Group'])) {
         $metadata['group'] = $headers['X-Message-Group'];
     }
     /* At this stage we are only interested how many plurals forms we should
      * be expecting when parsing the rest of this file. */
     $pluralCount = false;
     if (isset($headers['Plural-Forms'])) {
         if (preg_match('/nplurals=([0-9]+).*;/', $headers['Plural-Forms'], $matches)) {
             $pluralCount = $metadata['plural'] = $matches[1];
         }
     }
     // Then parse the messages
     foreach ($sections as $section) {
         $item = self::parseGettextSection($section, $pluralCount, $metadata);
         if ($item === false) {
             continue;
         }
         if ($useCtxtAsKey) {
             if (!isset($item['ctxt'])) {
                 error_log("ctxt missing for: {$section}");
                 continue;
             }
             $key = $item['ctxt'];
         } else {
             $key = self::generateKeyFromItem($item, $keyAlgorithm);
         }
         $key = $mangler->mangle($key);
         $messages[$key] = $potmode ? $item['id'] : $item['str'];
         $template[$key] = $item;
     }
     return array('MESSAGES' => $messages, 'TEMPLATE' => $template, 'METADATA' => $metadata, 'HEADERS' => $headers);
 }