private PoStreamReader::readLine()
Reads a line from the PO stream and stores data internally.
Expands $this->_current_item based on new data for the current item. If this line ends the current item, it is saved with setItemFromArray() with data from $this->_current_item.
An internal state machine is maintained in this reader using $this->_context as the reading state. PO items are in between COMMENT states (when items have at least one line or comment in between them) or indicated by MSGSTR or MSGSTR_ARR followed immediately by an MSGID or MSGCTXT (when items closely follow each other).
Return value
FALSE if an error was logged, NULL otherwise. The errors are considered non-blocking, so reading can continue, while the errors are collected for later presentation.
File
- core/lib/Drupal/Component/Gettext/PoStreamReader.php, line 245
Class
- PoStreamReader
- Implements Gettext PO stream reader.
Namespace
Drupal\Component\Gettext
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | private function readLine() { // Read a line and set the stream finished indicator if it was not // possible anymore. $line = fgets ( $this ->_fd); $this ->_finished = ( $line === FALSE); if (! $this ->_finished) { if ( $this ->_line_number == 0) { // The first line might come with a UTF-8 BOM, which should be removed. $line = str_replace ( "\xEF\xBB\xBF" , '' , $line ); // Current plurality for 'msgstr[]'. $this ->_current_plural_index = 0; } // Track the line number for error reporting. $this ->_line_number++; // Initialize common values for error logging. $log_vars = array ( '%uri' => $this ->getURI(), '%line' => $this ->_line_number, ); // Trim away the linefeed. \\n might appear at the end of the string if // another line continuing the same string follows. We can remove that. $line = trim( strtr ( $line , array ( "\\\n" => "" ))); if (! strncmp ( '#' , $line , 1)) { // Lines starting with '#' are comments. if ( $this ->_context == 'COMMENT' ) { // Already in comment context, add to current comment. $this ->_current_item[ '#' ][] = substr ( $line , 1); } elseif (( $this ->_context == 'MSGSTR' ) || ( $this ->_context == 'MSGSTR_ARR' )) { // We are currently in string context, save current item. $this ->setItemFromArray( $this ->_current_item); // Start a new entry for the comment. $this ->_current_item = array (); $this ->_current_item[ '#' ][] = substr ( $line , 1); $this ->_context = 'COMMENT' ; return ; } else { // A comment following any other context is a syntax error. $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains an error: "msgstr" was expected but not found on line %line.' , $log_vars ); return FALSE; } return ; } elseif (! strncmp ( 'msgid_plural' , $line , 12)) { // A plural form for the current source string. if ( $this ->_context != 'MSGID' ) { // A plural form can only be added to an msgid directly. $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains an error: "msgid_plural" was expected but not found on line %line.' , $log_vars ); return FALSE; } // Remove 'msgid_plural' and trim away whitespace. $line = trim( substr ( $line , 12)); // Only the plural source string is left, parse it. $quoted = $this ->parseQuoted( $line ); if ( $quoted === FALSE) { // The plural form must be wrapped in quotes. $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains a syntax error on line %line.' , $log_vars ); return FALSE; } // Append the plural source to the current entry. if ( is_string ( $this ->_current_item[ 'msgid' ])) { // The first value was stored as string. Now we know the context is // plural, it is converted to array. $this ->_current_item[ 'msgid' ] = array ( $this ->_current_item[ 'msgid' ]); } $this ->_current_item[ 'msgid' ][] = $quoted ; $this ->_context = 'MSGID_PLURAL' ; return ; } elseif (! strncmp ( 'msgid' , $line , 5)) { // Starting a new message. if (( $this ->_context == 'MSGSTR' ) || ( $this ->_context == 'MSGSTR_ARR' )) { // We are currently in string context, save current item. $this ->setItemFromArray( $this ->_current_item); // Start a new context for the msgid. $this ->_current_item = array (); } elseif ( $this ->_context == 'MSGID' ) { // We are currently already in the context, meaning we passed an id with no data. $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains an error: "msgid" is unexpected on line %line.' , $log_vars ); return FALSE; } // Remove 'msgid' and trim away whitespace. $line = trim( substr ( $line , 5)); // Only the message id string is left, parse it. $quoted = $this ->parseQuoted( $line ); if ( $quoted === FALSE) { // The message id must be wrapped in quotes. $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains an error: invalid format for "msgid" on line %line.' , $log_vars , $log_vars ); return FALSE; } $this ->_current_item[ 'msgid' ] = $quoted ; $this ->_context = 'MSGID' ; return ; } elseif (! strncmp ( 'msgctxt' , $line , 7)) { // Starting a new context. if (( $this ->_context == 'MSGSTR' ) || ( $this ->_context == 'MSGSTR_ARR' )) { // We are currently in string context, save current item. $this ->setItemFromArray( $this ->_current_item); $this ->_current_item = array (); } elseif (! empty ( $this ->_current_item[ 'msgctxt' ])) { // A context cannot apply to another context. $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains an error: "msgctxt" is unexpected on line %line.' , $log_vars ); return FALSE; } // Remove 'msgctxt' and trim away whitespaces. $line = trim( substr ( $line , 7)); // Only the msgctxt string is left, parse it. $quoted = $this ->parseQuoted( $line ); if ( $quoted === FALSE) { // The context string must be quoted. $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains an error: invalid format for "msgctxt" on line %line.' , $log_vars ); return FALSE; } $this ->_current_item[ 'msgctxt' ] = $quoted ; $this ->_context = 'MSGCTXT' ; return ; } elseif (! strncmp ( 'msgstr[' , $line , 7)) { // A message string for a specific plurality. if (( $this ->_context != 'MSGID' ) && ( $this ->_context != 'MSGCTXT' ) && ( $this ->_context != 'MSGID_PLURAL' ) && ( $this ->_context != 'MSGSTR_ARR' )) { // Plural message strings must come after msgid, msgxtxt, // msgid_plural, or other msgstr[] entries. $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains an error: "msgstr[]" is unexpected on line %line.' , $log_vars ); return FALSE; } // Ensure the plurality is terminated. if ( strpos ( $line , ']' ) === FALSE) { $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains an error: invalid format for "msgstr[]" on line %line.' , $log_vars ); return FALSE; } // Extract the plurality. $frombracket = strstr ( $line , '[' ); $this ->_current_plural_index = substr ( $frombracket , 1, strpos ( $frombracket , ']' ) - 1); // Skip to the next whitespace and trim away any further whitespace, // bringing $line to the message text only. $line = trim( strstr ( $line , " " )); $quoted = $this ->parseQuoted( $line ); if ( $quoted === FALSE) { // The string must be quoted. $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains an error: invalid format for "msgstr[]" on line %line.' , $log_vars ); return FALSE; } if (!isset( $this ->_current_item[ 'msgstr' ]) || ! is_array ( $this ->_current_item[ 'msgstr' ])) { $this ->_current_item[ 'msgstr' ] = array (); } $this ->_current_item[ 'msgstr' ][ $this ->_current_plural_index] = $quoted ; $this ->_context = 'MSGSTR_ARR' ; return ; } elseif (! strncmp ( "msgstr" , $line , 6)) { // A string pair for an msgid (with optional context). if (( $this ->_context != 'MSGID' ) && ( $this ->_context != 'MSGCTXT' )) { // Strings are only valid within an id or context scope. $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains an error: "msgstr" is unexpected on line %line.' , $log_vars ); return FALSE; } // Remove 'msgstr' and trim away away whitespaces. $line = trim( substr ( $line , 6)); // Only the msgstr string is left, parse it. $quoted = $this ->parseQuoted( $line ); if ( $quoted === FALSE) { // The string must be quoted. $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains an error: invalid format for "msgstr" on line %line.' , $log_vars ); return FALSE; } $this ->_current_item[ 'msgstr' ] = $quoted ; $this ->_context = 'MSGSTR' ; return ; } elseif ( $line != '' ) { // Anything that is not a token may be a continuation of a previous token. $quoted = $this ->parseQuoted( $line ); if ( $quoted === FALSE) { // This string must be quoted. $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains an error: string continuation expected on line %line.' , $log_vars ); return FALSE; } // Append the string to the current item. if (( $this ->_context == 'MSGID' ) || ( $this ->_context == 'MSGID_PLURAL' )) { if ( is_array ( $this ->_current_item[ 'msgid' ])) { // Add string to last array element for plural sources. $last_index = count ( $this ->_current_item[ 'msgid' ]) - 1; $this ->_current_item[ 'msgid' ][ $last_index ] .= $quoted ; } else { // Singular source, just append the string. $this ->_current_item[ 'msgid' ] .= $quoted ; } } elseif ( $this ->_context == 'MSGCTXT' ) { // Multiline context name. $this ->_current_item[ 'msgctxt' ] .= $quoted ; } elseif ( $this ->_context == 'MSGSTR' ) { // Multiline translation string. $this ->_current_item[ 'msgstr' ] .= $quoted ; } elseif ( $this ->_context == 'MSGSTR_ARR' ) { // Multiline plural translation string. $this ->_current_item[ 'msgstr' ][ $this ->_current_plural_index] .= $quoted ; } else { // No valid context to append to. $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri contains an error: unexpected string on line %line.' , $log_vars ); return FALSE; } return ; } } // Empty line read or EOF of PO stream, close out the last entry. if (( $this ->_context == 'MSGSTR' ) || ( $this ->_context == 'MSGSTR_ARR' )) { $this ->setItemFromArray( $this ->_current_item); $this ->_current_item = array (); } elseif ( $this ->_context != 'COMMENT' ) { $this ->_errors[] = SafeMarkup::format( 'The translation stream %uri ended unexpectedly at line %line.' , $log_vars ); return FALSE; } } |
Please login to continue.