wp/wp-includes/pomo/po.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
   187 
   187 
   188 	/**
   188 	/**
   189 	 * Builds a string from the entry for inclusion in PO file
   189 	 * Builds a string from the entry for inclusion in PO file
   190 	 *
   190 	 *
   191 	 * @static
   191 	 * @static
   192 	 * @param object &$entry the entry to convert to po string
   192 	 * @param Translation_Entry &$entry the entry to convert to po string
   193 	 * @return string|bool PO-style formatted string for the entry or
   193 	 * @return false|string PO-style formatted string for the entry or
   194 	 * 	false if the entry is empty
   194 	 * 	false if the entry is empty
   195 	 */
   195 	 */
   196 	function export_entry(&$entry) {
   196 	function export_entry(&$entry) {
   197 		if (is_null($entry->singular)) return false;
   197 		if (is_null($entry->singular)) return false;
   198 		$po = array();
   198 		$po = array();
   213 			}
   213 			}
   214 		}
   214 		}
   215 		return implode("\n", $po);
   215 		return implode("\n", $po);
   216 	}
   216 	}
   217 
   217 
       
   218 	/**
       
   219 	 * @param string $filename
       
   220 	 * @return boolean
       
   221 	 */
   218 	function import_from_file($filename) {
   222 	function import_from_file($filename) {
   219 		$f = fopen($filename, 'r');
   223 		$f = fopen($filename, 'r');
   220 		if (!$f) return false;
   224 		if (!$f) return false;
   221 		$lineno = 0;
   225 		$lineno = 0;
   222 		while (true) {
   226 		while (true) {
   236 			return false;
   240 			return false;
   237 		}
   241 		}
   238 		return true;
   242 		return true;
   239 	}
   243 	}
   240 
   244 
       
   245 	/**
       
   246 	 * @param resource $f
       
   247 	 * @param int      $lineno
       
   248 	 * @return null|false|array
       
   249 	 */
   241 	function read_entry($f, $lineno = 0) {
   250 	function read_entry($f, $lineno = 0) {
   242 		$entry = new Translation_Entry();
   251 		$entry = new Translation_Entry();
   243 		// where were we in the last step
   252 		// where were we in the last step
   244 		// can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural
   253 		// can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural
   245 		$context = '';
   254 		$context = '';
   272 				// comments have to be at the beginning
   281 				// comments have to be at the beginning
   273 				if ($context && $context != 'comment') {
   282 				if ($context && $context != 'comment') {
   274 					return false;
   283 					return false;
   275 				}
   284 				}
   276 				// add comment
   285 				// add comment
   277 				$this->add_comment_to_entry($entry, $line);;
   286 				$this->add_comment_to_entry($entry, $line);
   278 			} elseif (preg_match('/^msgctxt\s+(".*")/', $line, $m)) {
   287 			} elseif (preg_match('/^msgctxt\s+(".*")/', $line, $m)) {
   279 				if ($is_final($context)) {
   288 				if ($is_final($context)) {
   280 					PO::read_line($f, 'put-back');
   289 					PO::read_line($f, 'put-back');
   281 					$lineno--;
   290 					$lineno--;
   282 					break;
   291 					break;
   341 			$entry->translations = array();
   350 			$entry->translations = array();
   342 		}
   351 		}
   343 		return array('entry' => $entry, 'lineno' => $lineno);
   352 		return array('entry' => $entry, 'lineno' => $lineno);
   344 	}
   353 	}
   345 
   354 
       
   355 	/**
       
   356 	 * @staticvar string   $last_line
       
   357 	 * @staticvar boolean  $use_last_line
       
   358 	 * @param     resource $f
       
   359 	 * @param     string   $action
       
   360 	 * @return boolean
       
   361 	 */
   346 	function read_line($f, $action = 'read') {
   362 	function read_line($f, $action = 'read') {
   347 		static $last_line = '';
   363 		static $last_line = '';
   348 		static $use_last_line = false;
   364 		static $use_last_line = false;
   349 		if ('clear' == $action) {
   365 		if ('clear' == $action) {
   350 			$last_line = '';
   366 			$last_line = '';
   359 		$last_line = $line;
   375 		$last_line = $line;
   360 		$use_last_line = false;
   376 		$use_last_line = false;
   361 		return $line;
   377 		return $line;
   362 	}
   378 	}
   363 
   379 
       
   380 	/**
       
   381 	 * @param Translation_Entry $entry
       
   382 	 * @param string            $po_comment_line
       
   383 	 */
   364 	function add_comment_to_entry(&$entry, $po_comment_line) {
   384 	function add_comment_to_entry(&$entry, $po_comment_line) {
   365 		$first_two = substr($po_comment_line, 0, 2);
   385 		$first_two = substr($po_comment_line, 0, 2);
   366 		$comment = trim(substr($po_comment_line, 2));
   386 		$comment = trim(substr($po_comment_line, 2));
   367 		if ('#:' == $first_two) {
   387 		if ('#:' == $first_two) {
   368 			$entry->references = array_merge($entry->references, preg_split('/\s+/', $comment));
   388 			$entry->references = array_merge($entry->references, preg_split('/\s+/', $comment));
   373 		} else {
   393 		} else {
   374 			$entry->translator_comments = trim($entry->translator_comments . "\n" . $comment);
   394 			$entry->translator_comments = trim($entry->translator_comments . "\n" . $comment);
   375 		}
   395 		}
   376 	}
   396 	}
   377 
   397 
       
   398 	/**
       
   399 	 * @param string $s
       
   400 	 * @return sring
       
   401 	 */
   378 	function trim_quotes($s) {
   402 	function trim_quotes($s) {
   379 		if ( substr($s, 0, 1) == '"') $s = substr($s, 1);
   403 		if ( substr($s, 0, 1) == '"') $s = substr($s, 1);
   380 		if ( substr($s, -1, 1) == '"') $s = substr($s, 0, -1);
   404 		if ( substr($s, -1, 1) == '"') $s = substr($s, 0, -1);
   381 		return $s;
   405 		return $s;
   382 	}
   406 	}