Index: trunk/extensions/Translate/ffs/OpenLayersJS.php |
— | — | @@ -0,0 +1,70 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * *INCOMPLETE* OpenLayers JavaScript language class file format handler. |
| 6 | + * |
| 7 | + * @author Robert Leverington |
| 8 | + * @copyright Copyright � 2009 Robert Leverington |
| 9 | + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
| 10 | + * @file |
| 11 | + */ |
| 12 | + |
| 13 | +class JavaScriptFormatReader extends SimpleFormatReader { |
| 14 | + |
| 15 | + private function leftTrim( $string ) { |
| 16 | + $string = ltrim( $string ); |
| 17 | + $string = ltrim( $string, '"' ); |
| 18 | + return $string; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Parse OpenLayer JavaScript language class. |
| 23 | + * Known issues: |
| 24 | + * - It is a requirement for key names to be enclosed in single |
| 25 | + * quotation marks, and for messages to be enclosed in double. |
| 26 | + * - The last key-value pair must have a comma at the end. |
| 27 | + * - Uses seperate $this->leftTrim() function, this is undersired. |
| 28 | + */ |
| 29 | + protected function parseMessages() { |
| 30 | + $this->filename = dirname( __FILE__ ) . '/en.js'; |
| 31 | + $data = file_get_contents( $this->filename ); |
| 32 | + |
| 33 | + // Just get relevant data. |
| 34 | + $dataStart = strpos( $data, '{' ); |
| 35 | + $dataEnd = strrpos( $data, '}' ); |
| 36 | + $data = substr( $data, $dataStart + 1, $dataEnd - $dataStart - 1); |
| 37 | + // Strip comments. |
| 38 | + $data = preg_replace( '#^(\s*?)//(.*?)$#m', '', $data ); |
| 39 | + // Break in to message segements for further parsing. |
| 40 | + $data = explode( '",', $data ); |
| 41 | + |
| 42 | + $messages = array(); |
| 43 | + // Process each segment. |
| 44 | + foreach( $data as $segment ) { |
| 45 | + // Remove excess quote mark at beginning. |
| 46 | + $segment = substr( $segment, 1 ); |
| 47 | + // Add back trailing quote. |
| 48 | + $segment .= '"'; |
| 49 | + // Concatenate seperate strings. |
| 50 | + $segment = explode( '" +', $segment ); |
| 51 | + $segment = array_map( array( $this, 'leftTrim' ), $segment ); |
| 52 | + $segment = implode( $segment ); |
| 53 | + #$segment = preg_replace( '#\" \+(.*?)\"#m', '', $segment ); |
| 54 | + // Break in to key and message. |
| 55 | + $segments = explode( '\':', $segment ); |
| 56 | + $key = $segments[ 0 ]; |
| 57 | + unset( $segments[ 0 ] ); |
| 58 | + $value = implode( $segments ); |
| 59 | + // Strip excess whitespace from both. |
| 60 | + $key = trim( $key ); |
| 61 | + $value = trim( $value ); |
| 62 | + // Remove quotation marks and syntax. |
| 63 | + $key = substr( $key, 1 ); |
| 64 | + $value = substr( $value, 1, -1 ); |
| 65 | + $messages[ $key ] = $value; |
| 66 | + } |
| 67 | + |
| 68 | + return $messages; |
| 69 | + } |
| 70 | + |
| 71 | +} |