Ticket #21616: 21616.3.diff
File 21616.3.diff, 5.0 KB (added by , 11 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 /** 29 * Add textdomain to a single file. 30 * 31 * @since x.x.x 32 * 33 * @see AddTextdomain::process_string() 34 * 35 * @param string $domain Text domain. 36 * @param string $source_filename Filename with optional path. 37 * @param bool $inplace True to modifies the PHP file in place. False to print to standard output. 38 */ 39 public function process_file( $domain, $source_filename, $inplace ) { 40 $new_source = $this->process_string( $domain, file_get_contents( $source_filename ) ); 41 42 if ($inplace) { 43 $f = fopen( $source_filename, 'w' ); 44 fwrite( $f, $new_source ); 45 fclose( $f ); 46 } else { 47 echo $new_source; 48 } 33 49 } 34 50 51 /** 52 * Add textdomain to a string of PHP. 53 * 54 * Functions calls should be wrapped in opening and closing PHP delimiters as usual. 55 * 56 * @since x.x.x 57 * 58 * @see AddTextdomain::process_tokens() 59 * 60 * @param string $domain Text domain. 61 * @param string $string PHP code to parse. 62 * 63 * @return string Modified source. 64 */ 65 public function process_string( $domain, $string ) { 66 $tokens = token_get_all( $string ); 67 return $this->process_tokens( $domain, $tokens ); 68 } 35 69 36 function process_file($domain, $source_filename, $inplace) { 70 /** 71 * Add textdomain to a set of PHP tokens. 72 * 73 * @since x.x.x 74 * 75 * @param string $domain Text domain. 76 * @param array $tokens PHP tokens. An array of token identifiers. Each individual token identifier is either a 77 * single character (i.e.: ;, ., >, !, etc.), or a three element array containing the token 78 * index in element 0, the string content of the original token in element 1 and the line 79 * number in element 2. 80 * 81 * @return string Modified source. 82 */ 83 public function process_tokens( $domain, $tokens ) { 37 84 38 85 $this->modified_contents = ''; 39 86 $domain = addslashes($domain); 40 87 41 $source = file_get_contents($source_filename);42 $tokens = token_get_all($source);43 44 88 $in_func = false; 45 89 $args_started = false; 46 90 $parens_balance = 0; 47 91 $found_domain = false; 48 92 49 foreach($tokens as $ token) {93 foreach($tokens as $index => $token) { 50 94 $string_success = false; 51 95 if (is_array($token)) { 52 96 list($id, $text) = $token; … … 67 111 } elseif (')' == $token) { 68 112 --$parens_balance; 69 113 if ($in_func && 0 == $parens_balance) { 70 $token = $found_domain? ')' : ", '$domain')"; 114 if ( ! $found_domain ) { 115 $token = ", '$domain'"; 116 if ( T_WHITESPACE == $tokens[ $index - 1 ][0] ) { 117 $token .= ' '; // Maintain code standards if previously present 118 // Remove previous whitespace token to account for it. 119 $this->modified_contents = trim( $this->modified_contents ); 120 } 121 $token .= ')'; 122 } 71 123 $in_func = false; 72 124 $args_started = false; 73 125 $found_domain = false; 74 126 } 75 127 } 76 $this-> process_token($token, $inplace);128 $this->modified_contents .= $token; 77 129 } 78 130 79 if ($inplace) { 80 $f = fopen($source_filename, 'w'); 81 fwrite($f, $this->modified_contents); 82 fclose($f); 83 } 131 return $this->modified_contents; 84 132 } 85 133 } 86 134 -
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 ); 17 $this->assertEquals( $expected, $result ); 18 } 19 20 function dataStrings() { 21 return array( 22 array( "<?php __('string'); ?>", "<?php __('string', 'foo'); ?>" ), // Simple single quotes 23 array( '<?php __("string"); ?>', "<?php __(\"string\", 'foo'); ?>" ), // Simple double quotes 24 array( "<?php __( 'string' ); ?>", "<?php __( 'string', 'foo' ); ?>" ), // Simple single quotes CS 25 array( '<?php __( "string" ); ?>', "<?php __( \"string\", 'foo' ); ?>" ), // Simple double quotes CS 26 array( "<?php __( 'string', 'string2' ); ?>", "<?php __( 'string', 'string2', 'foo' ); ?>" ), // Multiple string args 27 array( '<?php __( \'string\', $var ); ?>', '<?php __( \'string\', $var, \'foo\' ); ?>' ), // Multiple string / var args 28 array( "<?php __( 'string', 'foo' ); ?>", "<?php __( 'string', 'foo' ); ?>" ), // Existing textdomain 29 ); 30 } 31 32 }