<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[36603] trunk/tools/i18n: i18n-tools: Respect the coding standards when adding textdomains with add-textdomain.php.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/36603">36603</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/36603","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ocean90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-02-20 21:43:51 +0000 (Sat, 20 Feb 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>i18n-tools: Respect the coding standards when adding textdomains with add-textdomain.php.
Props GaryJ, groovecoder.
Fixes <a href="https://core.trac.wordpress.org/ticket/21616">#21616</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktoolsi18naddtextdomainphp">trunk/tools/i18n/add-textdomain.php</a></li>
<li><a href="#trunktoolsi18ntAddTextdomainTestphp">trunk/tools/i18n/t/AddTextdomainTest.php</a></li>
<li><a href="#trunktoolsi18ntdataaddtextdomain0resultphp">trunk/tools/i18n/t/data/add-textdomain-0-result.php</a></li>
<li><a href="#trunktoolsi18ntdataaddtextdomain0php">trunk/tools/i18n/t/data/add-textdomain-0.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktoolsi18naddtextdomainphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tools/i18n/add-textdomain.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tools/i18n/add-textdomain.php 2016-02-20 21:35:54 UTC (rev 36602)
+++ trunk/tools/i18n/add-textdomain.php 2016-02-20 21:43:51 UTC (rev 36603)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -17,40 +17,78 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Constructor.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- function __construct() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function __construct() {
</ins><span class="cx" style="display: block; padding: 0 10px"> $makepot = new MakePOT;
</span><span class="cx" style="display: block; padding: 0 10px"> $this->funcs = array_keys( $makepot->rules );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->funcs[] = 'translate_nooped_plural';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- function usage() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Prints CLI usage.
+ */
+ public function usage() {
</ins><span class="cx" style="display: block; padding: 0 10px"> $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";
</span><span class="cx" style="display: block; padding: 0 10px"> fwrite(STDERR, $usage);
</span><span class="cx" style="display: block; padding: 0 10px"> exit(1);
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- function process_token($token_text, $inplace) {
- if ($inplace)
- $this->modified_contents .= $token_text;
- else
- echo $token_text;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Adds textdomain to a single file.
+ *
+ * @see AddTextdomain::process_string()
+ *
+ * @param string $domain Text domain.
+ * @param string $source_filename Filename with optional path.
+ * @param bool $inplace True to modifies the PHP file in place. False to print to standard output.
+ */
+ public function process_file( $domain, $source_filename, $inplace ) {
+ $new_source = $this->process_string( $domain, file_get_contents( $source_filename ) );
+
+ if ( $inplace ) {
+ $f = fopen( $source_filename, 'w' );
+ fwrite( $f, $new_source );
+ fclose( $f );
+ } else {
+ echo $new_source;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Adds textdomain to a string of PHP.
+ *
+ * Functions calls should be wrapped in opening and closing PHP delimiters as usual.
+ *
+ * @see AddTextdomain::process_tokens()
+ *
+ * @param string $domain Text domain.
+ * @param string $string PHP code to parse.
+ * @return string Modified source.
+ */
+ public function process_string( $domain, $string ) {
+ $tokens = token_get_all( $string );
+ return $this->process_tokens( $domain, $tokens );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- function process_file($domain, $source_filename, $inplace) {
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Adds textdomain to a set of PHP tokens.
+ *
+ * @param string $domain Text domain.
+ * @param array $tokens PHP tokens. An array of token identifiers. Each individual token identifier is either a
+ * single character (i.e.: ;, ., >, !, etc.), or a three element array containing the token
+ * index in element 0, the string content of the original token in element 1 and the line
+ * number in element 2.
+ * @return string Modified source.
+ */
+ public function process_tokens( $domain, $tokens ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->modified_contents = '';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $domain = addslashes($domain);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $domain = addslashes( $domain );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $source = file_get_contents($source_filename);
- $tokens = token_get_all($source);
-
</del><span class="cx" style="display: block; padding: 0 10px"> $in_func = false;
</span><span class="cx" style="display: block; padding: 0 10px"> $args_started = false;
</span><span class="cx" style="display: block; padding: 0 10px"> $parens_balance = 0;
</span><span class="cx" style="display: block; padding: 0 10px"> $found_domain = false;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- foreach($tokens as $token) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach($tokens as $index => $token) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $string_success = false;
</span><span class="cx" style="display: block; padding: 0 10px"> if (is_array($token)) {
</span><span class="cx" style="display: block; padding: 0 10px"> list($id, $text) = $token;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -71,26 +109,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif (')' == $token) {
</span><span class="cx" style="display: block; padding: 0 10px"> --$parens_balance;
</span><span class="cx" style="display: block; padding: 0 10px"> if ($in_func && 0 == $parens_balance) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $token = $found_domain? ')' : ", '$domain')";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $found_domain ) {
+ $token = ", '$domain'";
+ if ( T_WHITESPACE == $tokens[ $index - 1 ][0] ) {
+ $token .= ' '; // Maintain code standards if previously present
+ // Remove previous whitespace token to account for it.
+ $this->modified_contents = trim( $this->modified_contents );
+ }
+ $token .= ')';
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> $in_func = false;
</span><span class="cx" style="display: block; padding: 0 10px"> $args_started = false;
</span><span class="cx" style="display: block; padding: 0 10px"> $found_domain = false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->process_token($token, $inplace);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->modified_contents .= $token;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ($inplace) {
- $f = fopen($source_filename, 'w');
- fwrite($f, $this->modified_contents);
- fclose($f);
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return $this->modified_contents;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-// run the CLI only if the file
-// wasn't included
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+// Run the CLI only if the file wasn't included.
</ins><span class="cx" style="display: block; padding: 0 10px"> $included_files = get_included_files();
</span><span class="cx" style="display: block; padding: 0 10px"> if ($included_files[0] == __FILE__) {
</span><span class="cx" style="display: block; padding: 0 10px"> $adddomain = new AddTextdomain();
</span></span></pre></div>
<a id="trunktoolsi18ntAddTextdomainTestphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tools/i18n/t/AddTextdomainTest.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tools/i18n/t/AddTextdomainTest.php 2016-02-20 21:35:54 UTC (rev 36602)
+++ trunk/tools/i18n/t/AddTextdomainTest.php 2016-02-20 21:43:51 UTC (rev 36603)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10,15 +10,37 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> class AddTextDomainTest extends PHPUnit_Framework_TestCase {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- function __construct() {
- $this->atd = new AddTextdomain;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function setUp() {
+ parent::setUp();
+ $this->atd = new AddTextdomain();
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> function test_add() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- # copy to a new file, so that we don't corrupt the old one
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Copy to a new file, so that we don't corrupt the old one.
</ins><span class="cx" style="display: block; padding: 0 10px"> copy( 'data/add-textdomain-0.php', 'data/add-textdomain-0-work.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->atd->process_file( 'test-domain', 'data/add-textdomain-0-work.php', true );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( file_get_contents( 'data/add-textdomain-0-result.php' ), file_get_contents( 'data/add-textdomain-0-work.php' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> unlink( 'data/add-textdomain-0-work.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @dataProvider data_textdomain_sources
+ */
+ function test_basic_add_textdomain( $source, $expected ) {
+ $tokens = token_get_all( $source );
+ $result = $this->atd->process_tokens( 'foo', $tokens );
+ $this->assertEquals( $expected, $result );
+ }
+
+ function data_textdomain_sources() {
+ return array(
+ array( "<?php __('string'); ?>", "<?php __('string', 'foo'); ?>" ), // Simple single quotes
+ array( '<?php __("string"); ?>', "<?php __(\"string\", 'foo'); ?>" ), // Simple double quotes
+ array( "<?php __( 'string' ); ?>", "<?php __( 'string', 'foo' ); ?>" ), // Simple single quotes CS
+ array( '<?php __( "string" ); ?>', "<?php __( \"string\", 'foo' ); ?>" ), // Simple double quotes CS
+ array( "<?php __( 'string', 'string2' ); ?>", "<?php __( 'string', 'string2', 'foo' ); ?>" ), // Multiple string args
+ array( '<?php __( \'string\', $var ); ?>', '<?php __( \'string\', $var, \'foo\' ); ?>' ), // Multiple string / var args
+ array( "<?php __( 'string', 'foo' ); ?>", "<?php __( 'string', 'foo' ); ?>" ), // Existing textdomain
+ );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktoolsi18ntdataaddtextdomain0resultphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tools/i18n/t/data/add-textdomain-0-result.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tools/i18n/t/data/add-textdomain-0-result.php 2016-02-20 21:35:54 UTC (rev 36602)
+++ trunk/tools/i18n/t/data/add-textdomain-0-result.php 2016-02-20 21:43:51 UTC (rev 36603)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,18 +1,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> function call_some_i18n_methods() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- __( 'Hello World' , 'test-domain');
- _e( 'Hello World' , 'test-domain');
- _n( 'Single', 'Plural', 1 , 'test-domain');
- _n_noop( 'Single Noop', 'Plural Noop', 1 , 'test-domain');
- _x( 'Hello World', 'Testing' , 'test-domain');
- _ex( 'Hello World', 'Testing' , 'test-domain');
- _nx( 'Hello World', 'Testing' , 'test-domain');
- _nx_noop( 'Hello World Noop', 'Testing' , 'test-domain');
- esc_attr__( 'Attribute' , 'test-domain');
- esc_html__( 'HTML' , 'test-domain');
- esc_attr_e( 'Attribute' , 'test-domain');
- esc_html_e( 'HTML' , 'test-domain');
- esc_attr_x( 'Attribute', 'Testing' , 'test-domain');
- esc_html_x( 'HTML', 'Testing' , 'test-domain');
- translate_nooped_plural( 'Plural Noop', 2 , 'test-domain');
-}
</del><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ __( 'Hello World', 'test-domain' );
+ _e( 'Hello World', 'test-domain' );
+ _n( 'Single', 'Plural', 1, 'test-domain' );
+ _n_noop( 'Single Noop', 'Plural Noop', 1, 'test-domain' );
+ _x( 'Hello World', 'Testing', 'test-domain' );
+ _ex( 'Hello World', 'Testing', 'test-domain' );
+ _nx( 'Hello World', 'Testing', 'test-domain' );
+ _nx_noop( 'Hello World Noop', 'Testing', 'test-domain' );
+ esc_attr__( 'Attribute', 'test-domain' );
+ esc_html__( 'HTML', 'test-domain' );
+ esc_attr_e( 'Attribute', 'test-domain' );
+ esc_html_e( 'HTML', 'test-domain' );
+ esc_attr_x( 'Attribute', 'Testing', 'test-domain' );
+ esc_html_x( 'HTML', 'Testing', 'test-domain' );
+ translate_nooped_plural( 'Plural Noop', 2, 'test-domain' );
+}
</ins></span></pre></div>
<a id="trunktoolsi18ntdataaddtextdomain0php"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tools/i18n/t/data/add-textdomain-0.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tools/i18n/t/data/add-textdomain-0.php 2016-02-20 21:35:54 UTC (rev 36602)
+++ trunk/tools/i18n/t/data/add-textdomain-0.php 2016-02-20 21:43:51 UTC (rev 36603)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -15,4 +15,4 @@
</span><span class="cx" style="display: block; padding: 0 10px"> esc_attr_x( 'Attribute', 'Testing' );
</span><span class="cx" style="display: block; padding: 0 10px"> esc_html_x( 'HTML', 'Testing' );
</span><span class="cx" style="display: block; padding: 0 10px"> translate_nooped_plural( 'Plural Noop', 2 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-}
</del><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+}
</ins></span></pre>
</div>
</div>
</body>
</html>