wp/wp-includes/pomo/po.php
author ymh <ymh.work@gmail.com>
Mon, 14 Oct 2019 17:39:30 +0200
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
permissions -rw-r--r--
resynchronize code repo with production
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * Class for working with PO files
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     5
 * @version $Id: po.php 1158 2015-11-20 04:31:23Z dd32 $
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @package pomo
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * @subpackage po
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
require_once dirname(__FILE__) . '/translations.php';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    12
if ( ! defined( 'PO_MAX_LINE_LEN' ) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    13
	define('PO_MAX_LINE_LEN', 79);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    14
}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
ini_set('auto_detect_line_endings', 1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * Routines for working with PO files
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    21
if ( ! class_exists( 'PO', false ) ):
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
class PO extends Gettext_Translations {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
	var $comments_before_headers = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
	 * Exports headers to a PO entry
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
	 * @return string msgid/msgstr PO entry for this PO file headers, doesn't contain newline at the end
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
	function export_headers() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
		$header_string = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
		foreach($this->headers as $header => $value) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
			$header_string.= "$header: $value\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
		$poified = PO::poify($header_string);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
		if ($this->comments_before_headers)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
			$before_headers = $this->prepend_each_line(rtrim($this->comments_before_headers)."\n", '# ');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
			$before_headers = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
		return rtrim("{$before_headers}msgid \"\"\nmsgstr $poified");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
	 * Exports all entries to PO format
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
	 * @return string sequence of mgsgid/msgstr PO strings, doesn't containt newline at the end
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
	function export_entries() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		//TODO sorting
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
		return implode("\n\n", array_map(array('PO', 'export_entry'), $this->entries));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
	 * Exports the whole PO file as a string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
	 * @param bool $include_headers whether to include the headers in the export
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
	 * @return string ready for inclusion in PO file string for headers and all the enrtries
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
	function export($include_headers = true) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
		$res = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
		if ($include_headers) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
			$res .= $this->export_headers();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
			$res .= "\n\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
		$res .= $this->export_entries();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		return $res;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	 * Same as {@link export}, but writes the result to a file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
	 * @param string $filename where to write the PO string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
	 * @param bool $include_headers whether to include tje headers in the export
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
	 * @return bool true on success, false on error
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
	function export_to_file($filename, $include_headers = true) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
		$fh = fopen($filename, 'w');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
		if (false === $fh) return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
		$export = $this->export($include_headers);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
		$res = fwrite($fh, $export);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
		if (false === $res) return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
		return fclose($fh);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
	 * Text to include as a comment before the start of the PO contents
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
	 * Doesn't need to include # in the beginning of lines, these are added automatically
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
	function set_comment_before_headers( $text ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
		$this->comments_before_headers = $text;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
	 * Formats a string in PO-style
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
	 * @static
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
	 * @param string $string the string to format
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
	 * @return string the poified string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
	 */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   102
	public static function poify($string) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
		$quote = '"';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
		$slash = '\\';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
		$newline = "\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
		$replaces = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
			"$slash" 	=> "$slash$slash",
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
			"$quote"	=> "$slash$quote",
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
			"\t" 		=> '\t',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
		$string = str_replace(array_keys($replaces), array_values($replaces), $string);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
		$po = $quote.implode("${slash}n$quote$newline$quote", explode($newline, $string)).$quote;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
		// add empty string on first line for readbility
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
		if (false !== strpos($string, $newline) &&
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
				(substr_count($string, $newline) > 1 || !($newline === substr($string, -strlen($newline))))) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
			$po = "$quote$quote$newline$po";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
		// remove empty strings
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		$po = str_replace("$newline$quote$quote", '', $po);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
		return $po;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
	 * Gives back the original string from a PO-formatted string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
	 * @static
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
	 * @param string $string PO-formatted string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
	 * @return string enascaped string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
	 */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   133
	public static function unpoify($string) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   134
		$escapes = array('t' => "\t", 'n' => "\n", 'r' => "\r", '\\' => '\\');
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
		$lines = array_map('trim', explode("\n", $string));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
		$lines = array_map(array('PO', 'trim_quotes'), $lines);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
		$unpoified = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
		$previous_is_backslash = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
		foreach($lines as $line) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
			preg_match_all('/./u', $line, $chars);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
			$chars = $chars[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
			foreach($chars as $char) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
				if (!$previous_is_backslash) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
					if ('\\' == $char)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
						$previous_is_backslash = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
					else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
						$unpoified .= $char;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
					$previous_is_backslash = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
					$unpoified .= isset($escapes[$char])? $escapes[$char] : $char;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
		}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   154
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   155
		// Standardise the line endings on imported content, technically PO files shouldn't contain \r
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   156
		$unpoified = str_replace( array( "\r\n", "\r" ), "\n", $unpoified );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   157
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
		return $unpoified;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
	 * Inserts $with in the beginning of every new line of $string and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
	 * returns the modified string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
	 * @static
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
	 * @param string $string prepend lines in this string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
	 * @param string $with prepend lines with this string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
	 */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   169
	public static function prepend_each_line($string, $with) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
		$lines = explode("\n", $string);
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   171
		$append = '';
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   172
		if ("\n" === substr($string, -1) && '' === end($lines)) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   173
			// Last line might be empty because $string was terminated
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   174
			// with a newline, remove it from the $lines array,
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   175
			// we'll restore state by re-terminating the string at the end
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   176
			array_pop($lines);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   177
			$append = "\n";
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   178
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   179
		foreach ($lines as &$line) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   180
			$line = $with . $line;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   181
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   182
		unset($line);
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   183
		return implode("\n", $lines) . $append;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
	 * Prepare a text as a comment -- wraps the lines and prepends #
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
	 * and a special character to each line
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
	 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
	 * @param string $text the comment text
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
	 * @param string $char character to denote a special PO comment,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
	 * 	like :, default is a space
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
	 */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   195
	public static function comment_block($text, $char=' ') {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
		$text = wordwrap($text, PO_MAX_LINE_LEN - 3);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
		return PO::prepend_each_line($text, "#$char ");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
	 * Builds a string from the entry for inclusion in PO file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
	 * @static
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   204
	 * @param Translation_Entry $entry the entry to convert to po string (passed by reference).
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   205
	 * @return false|string PO-style formatted string for the entry or
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
	 * 	false if the entry is empty
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
	 */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   208
	public static function export_entry(&$entry) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   209
		if ( null === $entry->singular || '' === $entry->singular ) return false;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
		$po = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
		if (!empty($entry->translator_comments)) $po[] = PO::comment_block($entry->translator_comments);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
		if (!empty($entry->extracted_comments)) $po[] = PO::comment_block($entry->extracted_comments, '.');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
		if (!empty($entry->references)) $po[] = PO::comment_block(implode(' ', $entry->references), ':');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
		if (!empty($entry->flags)) $po[] = PO::comment_block(implode(", ", $entry->flags), ',');
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   215
		if ($entry->context) $po[] = 'msgctxt '.PO::poify($entry->context);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
		$po[] = 'msgid '.PO::poify($entry->singular);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
		if (!$entry->is_plural) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
			$translation = empty($entry->translations)? '' : $entry->translations[0];
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   219
			$translation = PO::match_begin_and_end_newlines( $translation, $entry->singular );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
			$po[] = 'msgstr '.PO::poify($translation);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
			$po[] = 'msgid_plural '.PO::poify($entry->plural);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
			$translations = empty($entry->translations)? array('', '') : $entry->translations;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
			foreach($translations as $i => $translation) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   225
				$translation = PO::match_begin_and_end_newlines( $translation, $entry->plural );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
				$po[] = "msgstr[$i] ".PO::poify($translation);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
		return implode("\n", $po);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   232
	public static function match_begin_and_end_newlines( $translation, $original ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   233
		if ( '' === $translation ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   234
			return $translation;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   235
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   236
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   237
		$original_begin = "\n" === substr( $original, 0, 1 );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   238
		$original_end = "\n" === substr( $original, -1 );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   239
		$translation_begin = "\n" === substr( $translation, 0, 1 );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   240
		$translation_end = "\n" === substr( $translation, -1 );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   241
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   242
		if ( $original_begin ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   243
			if ( ! $translation_begin ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   244
				$translation = "\n" . $translation;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   245
			}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   246
		} elseif ( $translation_begin ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   247
			$translation = ltrim( $translation, "\n" );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   248
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   249
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   250
		if ( $original_end ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   251
			if ( ! $translation_end ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   252
				$translation .= "\n";
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   253
			}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   254
		} elseif ( $translation_end ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   255
			$translation = rtrim( $translation, "\n" );
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   256
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   257
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   258
		return $translation;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   259
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   260
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   261
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   262
	 * @param string $filename
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   263
	 * @return boolean
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   264
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
	function import_from_file($filename) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
		$f = fopen($filename, 'r');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
		if (!$f) return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
		$lineno = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
		while (true) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
			$res = $this->read_entry($f, $lineno);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
			if (!$res) break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
			if ($res['entry']->singular == '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
				$this->set_headers($this->make_headers($res['entry']->translations[0]));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
				$this->add_entry($res['entry']);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
		PO::read_line($f, 'clear');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
		if ( false === $res ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
		if ( ! $this->headers && ! $this->entries ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   288
	/**
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   289
	 * Helper function for read_entry
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   290
	 * @param string $context
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   291
	 * @return bool
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   292
	 */
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   293
	protected static function is_final($context) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   294
		return ($context === 'msgstr') || ($context === 'msgstr_plural');
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   295
	}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   296
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   297
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   298
	 * @param resource $f
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   299
	 * @param int      $lineno
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   300
	 * @return null|false|array
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   301
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
	function read_entry($f, $lineno = 0) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
		$entry = new Translation_Entry();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
		// where were we in the last step
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
		// can be: comment, msgctxt, msgid, msgid_plural, msgstr, msgstr_plural
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
		$context = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
		$msgstr_index = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
		while (true) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
			$lineno++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
			$line = PO::read_line($f);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
			if (!$line)  {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
				if (feof($f)) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   313
					if (self::is_final($context))
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
						break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
					elseif (!$context) // we haven't read a line and eof came
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
						return null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
					else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
						return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
					return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
			if ($line == "\n") continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
			$line = trim($line);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
			if (preg_match('/^#/', $line, $m)) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
				// the comment is the start of a new entry
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   327
				if (self::is_final($context)) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
					PO::read_line($f, 'put-back');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
					$lineno--;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
				// comments have to be at the beginning
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
				if ($context && $context != 'comment') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
					return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
				// add comment
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   337
				$this->add_comment_to_entry($entry, $line);
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
			} elseif (preg_match('/^msgctxt\s+(".*")/', $line, $m)) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   339
				if (self::is_final($context)) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
					PO::read_line($f, 'put-back');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
					$lineno--;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
				if ($context && $context != 'comment') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
					return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
				$context = 'msgctxt';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
				$entry->context .= PO::unpoify($m[1]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
			} elseif (preg_match('/^msgid\s+(".*")/', $line, $m)) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   350
				if (self::is_final($context)) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
					PO::read_line($f, 'put-back');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
					$lineno--;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
					break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
				if ($context && $context != 'msgctxt' && $context != 'comment') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
					return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
				$context = 'msgid';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
				$entry->singular .= PO::unpoify($m[1]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
			} elseif (preg_match('/^msgid_plural\s+(".*")/', $line, $m)) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
				if ($context != 'msgid') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
					return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
				$context = 'msgid_plural';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
				$entry->is_plural = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
				$entry->plural .= PO::unpoify($m[1]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
			} elseif (preg_match('/^msgstr\s+(".*")/', $line, $m)) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
				if ($context != 'msgid') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
					return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
				$context = 'msgstr';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
				$entry->translations = array(PO::unpoify($m[1]));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
			} elseif (preg_match('/^msgstr\[(\d+)\]\s+(".*")/', $line, $m)) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
				if ($context != 'msgid_plural' && $context != 'msgstr_plural') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
					return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
				$context = 'msgstr_plural';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
				$msgstr_index = $m[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
				$entry->translations[$m[1]] = PO::unpoify($m[2]);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
			} elseif (preg_match('/^".*"$/', $line)) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
				$unpoified = PO::unpoify($line);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
				switch ($context) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
					case 'msgid':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
						$entry->singular .= $unpoified; break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
					case 'msgctxt':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
						$entry->context .= $unpoified; break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
					case 'msgid_plural':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
						$entry->plural .= $unpoified; break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
					case 'msgstr':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
						$entry->translations[0] .= $unpoified; break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
					case 'msgstr_plural':
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
						$entry->translations[$msgstr_index] .= $unpoified; break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
					default:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
						return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
			} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
				return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
		}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   400
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   401
		$have_translations = false;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   402
		foreach ( $entry->translations as $t ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   403
			if ( $t || ('0' === $t) ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   404
				$have_translations = true;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   405
				break;
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   406
			}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   407
		}
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   408
		if ( false === $have_translations ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
			$entry->translations = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
		}
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   411
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
		return array('entry' => $entry, 'lineno' => $lineno);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   415
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   416
	 * @staticvar string   $last_line
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   417
	 * @staticvar boolean  $use_last_line
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   418
	 *
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   419
	 * @param     resource $f
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   420
	 * @param     string   $action
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   421
	 * @return boolean
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   422
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
	function read_line($f, $action = 'read') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
		static $last_line = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
		static $use_last_line = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
		if ('clear' == $action) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
			$last_line = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
		if ('put-back' == $action) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
			$use_last_line = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
			return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
		$line = $use_last_line? $last_line : fgets($f);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
		$line = ( "\r\n" == substr( $line, -2 ) ) ? rtrim( $line, "\r\n" ) . "\n" : $line;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
		$last_line = $line;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
		$use_last_line = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
		return $line;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   441
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   442
	 * @param Translation_Entry $entry
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   443
	 * @param string            $po_comment_line
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   444
	 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
	function add_comment_to_entry(&$entry, $po_comment_line) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
		$first_two = substr($po_comment_line, 0, 2);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
		$comment = trim(substr($po_comment_line, 2));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
		if ('#:' == $first_two) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
			$entry->references = array_merge($entry->references, preg_split('/\s+/', $comment));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
		} elseif ('#.' == $first_two) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
			$entry->extracted_comments = trim($entry->extracted_comments . "\n" . $comment);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
		} elseif ('#,' == $first_two) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
			$entry->flags = array_merge($entry->flags, preg_split('/,\s*/', $comment));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
			$entry->translator_comments = trim($entry->translator_comments . "\n" . $comment);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   459
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   460
	 * @param string $s
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   461
	 * @return sring
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   462
	 */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   463
	public static function trim_quotes($s) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
		if ( substr($s, 0, 1) == '"') $s = substr($s, 1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
		if ( substr($s, -1, 1) == '"') $s = substr($s, 0, -1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
		return $s;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
endif;