Ticket #21616: 21616.diff
File 21616.diff, 3.3 KB (added by , 10 years ago) |
---|
-
tools/i18n/add-textdomain.php
25 25 exit(1); 26 26 } 27 27 28 function process_token($token_text, $inplace) { 29 if ($inplace) 30 $this->modified_contents .= $token_text; 31 else 32 echo $token_text; 28 function process_file($domain, $source_filename, $inplace) { 29 $new_source = $this->process_string( $domain, file_get_contents( $source_filename ) ); 30 31 if ($inplace) { 32 $f = fopen($source_filename, 'w'); 33 fwrite($f, $new_source); 34 fclose($f); 35 } else { 36 echo $new_source; 37 } 33 38 } 34 39 40 function process_string( $domain, $string ) { 41 $tokens = token_get_all( $string ); 42 return $this->process_tokens( $domain, $string ); 43 } 35 44 36 function process_ file($domain, $source_filename, $inplace) {45 function process_tokens( $domain, $tokens ) { 37 46 38 47 $this->modified_contents = ''; 39 48 $domain = addslashes($domain); 40 49 41 $source = file_get_contents($source_filename);42 $tokens = token_get_all($source);43 44 50 $in_func = false; 45 51 $args_started = false; 46 52 $parens_balance = 0; 47 53 $found_domain = false; 48 54 49 foreach($tokens as $ token) {55 foreach($tokens as $index => $token) { 50 56 $string_success = false; 51 57 if (is_array($token)) { 52 58 list($id, $text) = $token; … … 67 73 } elseif (')' == $token) { 68 74 --$parens_balance; 69 75 if ($in_func && 0 == $parens_balance) { 70 $token = $found_domain? ')' : ", '$domain')"; 76 if ( ! $found_domain ) { 77 $token = ", '$domain'"; 78 if ( T_WHITESPACE == $tokens[ $index - 1 ][0] ) { 79 $token .= ' '; // Maintain code standards if previously present 80 // Remove previous whitespace token to account for it. 81 $this->modified_contents = trim( $this->modified_contents ); 82 } 83 $token .= ')'; 84 } 85 // $token = $found_domain? ')' : ", '$domain')"; 71 86 $in_func = false; 72 87 $args_started = false; 73 88 $found_domain = false; 74 89 } 75 90 } 76 $this-> process_token($token, $inplace);91 $this->modified_contents .= $token; 77 92 } 78 93 79 if ($inplace) { 80 $f = fopen($source_filename, 'w'); 81 fwrite($f, $this->modified_contents); 82 fclose($f); 83 } 94 return $this->modified_contents; 84 95 } 85 96 } 86 97 -
tools/i18n/t/AddTextdomainTest.php
1 <?php 2 3 require_once dirname( dirname( __FILE__ ) ) . '/add-textdomain.php'; 4 5 class AddTextDomainTest extends PHPUnit_Framework_TestCase { 6 7 function setUp() { 8 $this->addtextdomainer = new AddTextdomain; 9 } 10 11 /** 12 * @dataProvider dataStrings 13 */ 14 function test_basic_add_textdomain( $source, $expected ) { 15 $tokens = token_get_all( $source ); 16 $result = $this->addtextdomainer->process_tokens( 'foo', $tokens, true ); 17 $this->assertEquals( $expected, $result ); 18 } 19 20 function dataStrings() { 21 return array( 22 array( "<?php __('string'); ?>", "<?php __('string', 'foo'); ?>" ), 23 array( '<?php __("string"); ?>', "<?php __(\"string\", 'foo'); ?>" ), 24 array( "<?php __( 'string' ); ?>", "<?php __( 'string', 'foo' ); ?>" ), 25 array( '<?php __( "string" ); ?>', "<?php __( \"string\", 'foo' ); ?>" ), 26 ); 27 } 28 29 }