| | 1 | <?php |
| | 2 | |
| | 3 | class GP_Format_Strings { |
| | 4 | |
| | 5 | var $extension = 'strings'; |
| | 6 | |
| | 7 | var $exported = ''; |
| | 8 | |
| | 9 | function sort_entries( $a, $b ) { |
| | 10 | if ( $a->context == $b->context ) { |
| | 11 | return 0; |
| | 12 | } |
| | 13 | return ( $a->context > $b->context ) ? +1 : -1; |
| | 14 | } |
| | 15 | |
| | 16 | function print_exported_file( $project, $locale, $translation_set, $entries ) { |
| | 17 | $result = ''; |
| | 18 | $prefix = pack( 'CC', 0xff, 0xfe ); // Add BOM |
| | 19 | |
| | 20 | $sorted_entries = $entries; |
| | 21 | usort( $sorted_entries, array( 'GP_Format_Strings', 'sort_entries' ) ); |
| | 22 | foreach ( $sorted_entries as $entry ) { |
| | 23 | $original = str_replace( "\n", "\\n", $entry->context ); |
| | 24 | $translation = str_replace( "\n", "\\n", empty( $entry->translations ) ? $entry->context : $entry->translations[0] ); |
| | 25 | $comment = preg_replace( "/(^\s+)|(\s+$)/us", "", $entry->extracted_comments ); |
| | 26 | if ( $comment == "" ) { |
| | 27 | $comment = "No comment provided by engineer."; |
| | 28 | } |
| | 29 | $result .= "/* $comment */\n\"$original\" = \"$translation\";\n\n"; |
| | 30 | } |
| | 31 | |
| | 32 | return $prefix . mb_convert_encoding( $result, 'UTF-16LE' ); |
| | 33 | } |
| | 34 | |
| | 35 | function read_translations_from_file( $file_name, $project = null ) { |
| | 36 | if ( is_null( $project ) ) return false; |
| | 37 | $translations = $this->read_originals_from_file( $file_name ); |
| | 38 | if ( !$translations ) return false; |
| | 39 | $originals = GP::$original->by_project_id( $project->id ); |
| | 40 | $new_translations = new Translations; |
| | 41 | foreach( $translations->entries as $key => $entry ) { |
| | 42 | // we have been using read_originals_from_file to parse the file |
| | 43 | // so we need to swap singular and translation |
| | 44 | if ( $entry->context == $entry->singular ) { |
| | 45 | $entry->translations = array(); |
| | 46 | } else { |
| | 47 | $entry->translations = array( $entry->singular ); |
| | 48 | } |
| | 49 | $entry->singular = null; |
| | 50 | foreach( $originals as $original ) { |
| | 51 | if ( $original->context == $entry->context ) { |
| | 52 | $entry->singular = $original->singular; |
| | 53 | break; |
| | 54 | } |
| | 55 | } |
| | 56 | if ( !$entry->singular ) { |
| | 57 | error_log( sprintf( __("Missing context %s in project #%d"), $entry->context, $project->id ) ); |
| | 58 | continue; |
| | 59 | } |
| | 60 | |
| | 61 | $new_translations->add_entry( $entry ); |
| | 62 | } |
| | 63 | return $new_translations; |
| | 64 | |
| | 65 | } |
| | 66 | |
| | 67 | function read_originals_from_file( $file_name ) { |
| | 68 | $entries = new Translations; |
| | 69 | $f = fopen( $file_name, 'r' ); |
| | 70 | if ( !$f ) return false; |
| | 71 | $context = $comment = null; |
| | 72 | $lineno = 1; |
| | 73 | while ( false !== ( $line = fgets( $f ) ) ) { |
| | 74 | if ( $lineno == 1 ) { |
| | 75 | if ( substr( $line, 0, 2 ) == pack( "CC", 0xef, 0xff ) ) { |
| | 76 | $line = substr( $line, 2 ); |
| | 77 | } |
| | 78 | } |
| | 79 | $line = mb_convert_encoding( $line, 'UTF-8', 'UTF-16BE' ); |
| | 80 | if ( is_null( $context ) ) { |
| | 81 | if ( preg_match( '/^\/\*\s*(.*)\s*\*\/$/', $line, $matches ) ) { |
| | 82 | if ( $matches[1] !== "No comment provided by engineer." ) { |
| | 83 | $comment = $matches[1]; |
| | 84 | } else { |
| | 85 | $comment = null; |
| | 86 | } |
| | 87 | } else if ( preg_match( '/^"(.*)" = "(.*)";$/', $line, $matches ) ) { |
| | 88 | $entry = new Translation_Entry(); |
| | 89 | $entry->context = $this->unescape( $matches[1] ); |
| | 90 | $entry->singular = $this->unescape( $matches[2] ); |
| | 91 | if ( ! is_null( $comment )) { |
| | 92 | $entry->extracted_comments = $comment; |
| | 93 | $comment = null; |
| | 94 | } |
| | 95 | $entry->translations = array(); |
| | 96 | $entries->add_entry( $entry ); |
| | 97 | $entry = null; |
| | 98 | } |
| | 99 | } |
| | 100 | $lineno++; |
| | 101 | } |
| | 102 | return $entries; |
| | 103 | } |
| | 104 | |
| | 105 | |
| | 106 | function unescape( $string ) { |
| | 107 | return stripcslashes( $string ); |
| | 108 | } |
| | 109 | |
| | 110 | function escape( $string ) { |
| | 111 | $string = addcslashes( $string, "'\n"); |
| | 112 | return $string; |
| | 113 | } |
| | 114 | } |
| | 115 | |
| | 116 | GP::$formats['strings'] = new GP_Format_Strings; |
| | 117 | No newline at end of file |