Changeset 36603
- Timestamp:
- 02/20/2016 09:43:51 PM (9 years ago)
- Location:
- trunk/tools/i18n
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/i18n/add-textdomain.php
r36600 r36603 18 18 * Constructor. 19 19 */ 20 function __construct() {20 public function __construct() { 21 21 $makepot = new MakePOT; 22 22 $this->funcs = array_keys( $makepot->rules ); … … 24 24 } 25 25 26 function usage() { 26 /** 27 * Prints CLI usage. 28 */ 29 public function usage() { 27 30 $usage = "Usage: php add-textdomain.php [-i] <domain> <file>\n\nAdds the string <domain> as a last argument to all i18n function calls in <file>\nand prints the modified php file on standard output.\n\nOptions:\n -i Modifies the PHP file in place, instead of printing it to standard output.\n"; 28 31 fwrite(STDERR, $usage); … … 30 33 } 31 34 32 function process_token($token_text, $inplace) { 33 if ($inplace) 34 $this->modified_contents .= $token_text; 35 else 36 echo $token_text; 35 /** 36 * Adds textdomain to a single file. 37 * 38 * @see AddTextdomain::process_string() 39 * 40 * @param string $domain Text domain. 41 * @param string $source_filename Filename with optional path. 42 * @param bool $inplace True to modifies the PHP file in place. False to print to standard output. 43 */ 44 public function process_file( $domain, $source_filename, $inplace ) { 45 $new_source = $this->process_string( $domain, file_get_contents( $source_filename ) ); 46 47 if ( $inplace ) { 48 $f = fopen( $source_filename, 'w' ); 49 fwrite( $f, $new_source ); 50 fclose( $f ); 51 } else { 52 echo $new_source; 53 } 37 54 } 38 55 56 /** 57 * Adds textdomain to a string of PHP. 58 * 59 * Functions calls should be wrapped in opening and closing PHP delimiters as usual. 60 * 61 * @see AddTextdomain::process_tokens() 62 * 63 * @param string $domain Text domain. 64 * @param string $string PHP code to parse. 65 * @return string Modified source. 66 */ 67 public function process_string( $domain, $string ) { 68 $tokens = token_get_all( $string ); 69 return $this->process_tokens( $domain, $tokens ); 70 } 39 71 40 function process_file($domain, $source_filename, $inplace) { 41 72 /** 73 * Adds textdomain to a set of PHP tokens. 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 * @return string Modified source. 81 */ 82 public function process_tokens( $domain, $tokens ) { 42 83 $this->modified_contents = ''; 43 $domain = addslashes($domain); 44 45 $source = file_get_contents($source_filename); 46 $tokens = token_get_all($source); 84 $domain = addslashes( $domain ); 47 85 48 86 $in_func = false; … … 51 89 $found_domain = false; 52 90 53 foreach($tokens as $ token) {91 foreach($tokens as $index => $token) { 54 92 $string_success = false; 55 93 if (is_array($token)) { … … 72 110 --$parens_balance; 73 111 if ($in_func && 0 == $parens_balance) { 74 $token = $found_domain? ')' : ", '$domain')"; 112 if ( ! $found_domain ) { 113 $token = ", '$domain'"; 114 if ( T_WHITESPACE == $tokens[ $index - 1 ][0] ) { 115 $token .= ' '; // Maintain code standards if previously present 116 // Remove previous whitespace token to account for it. 117 $this->modified_contents = trim( $this->modified_contents ); 118 } 119 $token .= ')'; 120 } 75 121 $in_func = false; 76 122 $args_started = false; … … 78 124 } 79 125 } 80 $this-> process_token($token, $inplace);126 $this->modified_contents .= $token; 81 127 } 82 128 83 if ($inplace) { 84 $f = fopen($source_filename, 'w'); 85 fwrite($f, $this->modified_contents); 86 fclose($f); 87 } 129 return $this->modified_contents; 88 130 } 89 131 } 90 132 91 92 // run the CLI only if the file 93 // wasn't included 133 // Run the CLI only if the file wasn't included. 94 134 $included_files = get_included_files(); 95 135 if ($included_files[0] == __FILE__) { -
trunk/tools/i18n/t/AddTextdomainTest.php
r36389 r36603 11 11 class AddTextDomainTest extends PHPUnit_Framework_TestCase { 12 12 13 function __construct() { 14 $this->atd = new AddTextdomain; 13 function setUp() { 14 parent::setUp(); 15 $this->atd = new AddTextdomain(); 15 16 } 16 17 17 18 function test_add() { 18 # copy to a new file, so that we don't corrupt the old one19 // Copy to a new file, so that we don't corrupt the old one. 19 20 copy( 'data/add-textdomain-0.php', 'data/add-textdomain-0-work.php' ); 20 21 $this->atd->process_file( 'test-domain', 'data/add-textdomain-0-work.php', true ); … … 22 23 unlink( 'data/add-textdomain-0-work.php' ); 23 24 } 25 26 /** 27 * @dataProvider data_textdomain_sources 28 */ 29 function test_basic_add_textdomain( $source, $expected ) { 30 $tokens = token_get_all( $source ); 31 $result = $this->atd->process_tokens( 'foo', $tokens ); 32 $this->assertEquals( $expected, $result ); 33 } 34 35 function data_textdomain_sources() { 36 return array( 37 array( "<?php __('string'); ?>", "<?php __('string', 'foo'); ?>" ), // Simple single quotes 38 array( '<?php __("string"); ?>', "<?php __(\"string\", 'foo'); ?>" ), // Simple double quotes 39 array( "<?php __( 'string' ); ?>", "<?php __( 'string', 'foo' ); ?>" ), // Simple single quotes CS 40 array( '<?php __( "string" ); ?>', "<?php __( \"string\", 'foo' ); ?>" ), // Simple double quotes CS 41 array( "<?php __( 'string', 'string2' ); ?>", "<?php __( 'string', 'string2', 'foo' ); ?>" ), // Multiple string args 42 array( '<?php __( \'string\', $var ); ?>', '<?php __( \'string\', $var, \'foo\' ); ?>' ), // Multiple string / var args 43 array( "<?php __( 'string', 'foo' ); ?>", "<?php __( 'string', 'foo' ); ?>" ), // Existing textdomain 44 ); 45 } 24 46 } -
trunk/tools/i18n/t/data/add-textdomain-0-result.php
r36390 r36603 1 1 <?php 2 2 function call_some_i18n_methods() { 3 __( 'Hello World' , 'test-domain');4 _e( 'Hello World' , 'test-domain');5 _n( 'Single', 'Plural', 1 , 'test-domain');6 _n_noop( 'Single Noop', 'Plural Noop', 1 , 'test-domain');7 _x( 'Hello World', 'Testing' , 'test-domain');8 _ex( 'Hello World', 'Testing' , 'test-domain');9 _nx( 'Hello World', 'Testing' , 'test-domain');10 _nx_noop( 'Hello World Noop', 'Testing' , 'test-domain');11 esc_attr__( 'Attribute' , 'test-domain');12 esc_html__( 'HTML' , 'test-domain');13 esc_attr_e( 'Attribute' , 'test-domain');14 esc_html_e( 'HTML' , 'test-domain');15 esc_attr_x( 'Attribute', 'Testing' , 'test-domain');16 esc_html_x( 'HTML', 'Testing' , 'test-domain');17 translate_nooped_plural( 'Plural Noop', 2 , 'test-domain');3 __( 'Hello World', 'test-domain' ); 4 _e( 'Hello World', 'test-domain' ); 5 _n( 'Single', 'Plural', 1, 'test-domain' ); 6 _n_noop( 'Single Noop', 'Plural Noop', 1, 'test-domain' ); 7 _x( 'Hello World', 'Testing', 'test-domain' ); 8 _ex( 'Hello World', 'Testing', 'test-domain' ); 9 _nx( 'Hello World', 'Testing', 'test-domain' ); 10 _nx_noop( 'Hello World Noop', 'Testing', 'test-domain' ); 11 esc_attr__( 'Attribute', 'test-domain' ); 12 esc_html__( 'HTML', 'test-domain' ); 13 esc_attr_e( 'Attribute', 'test-domain' ); 14 esc_html_e( 'HTML', 'test-domain' ); 15 esc_attr_x( 'Attribute', 'Testing', 'test-domain' ); 16 esc_html_x( 'HTML', 'Testing', 'test-domain' ); 17 translate_nooped_plural( 'Plural Noop', 2, 'test-domain' ); 18 18 }
Note: See TracChangeset
for help on using the changeset viewer.