Index: tools/i18n/add-textdomain.php
===================================================================
--- tools/i18n/add-textdomain.php	(revision 28220)
+++ tools/i18n/add-textdomain.php	(working copy)
@@ -25,28 +25,34 @@
 		exit(1);
 	}
 
-	function process_token($token_text, $inplace) {
-		if ($inplace)
-			$this->modified_contents .= $token_text;
-		else
-			echo $token_text;
+	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;
+		}
 	}
 
+	function process_string( $domain, $string ) {
+		$tokens = token_get_all( $string );
+		return $this->process_tokens( $domain, $string );
+	}
 
-	function process_file($domain, $source_filename, $inplace) {
+	function process_tokens( $domain, $tokens ) {
 
 		$this->modified_contents = '';
 		$domain = addslashes($domain);
 
-		$source = file_get_contents($source_filename);
-		$tokens = token_get_all($source);
-
 		$in_func = false;
 		$args_started = false;
 		$parens_balance = 0;
 		$found_domain = false;
 
-		foreach($tokens as $token) {
+		foreach($tokens as $index => $token) {
 			$string_success = false;
 			if (is_array($token)) {
 				list($id, $text) = $token;
@@ -67,20 +73,25 @@
 			} elseif (')' == $token) {
 				--$parens_balance;
 				if ($in_func && 0 == $parens_balance) {
-					$token = $found_domain? ')' : ", '$domain')";
+					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 .= ')';
+					}
+					// $token = $found_domain? ')' : ", '$domain')";
 					$in_func = false;
 					$args_started = false;
 					$found_domain = false;
 				}
 			}
-			$this->process_token($token, $inplace);
+			$this->modified_contents .= $token;
 		}
 
-		if ($inplace) {
-			$f = fopen($source_filename, 'w');
-			fwrite($f, $this->modified_contents);
-			fclose($f);
-		}
+		return $this->modified_contents;
 	}
 }
 
Index: tools/i18n/t/AddTextdomainTest.php
===================================================================
--- tools/i18n/t/AddTextdomainTest.php	(revision 0)
+++ tools/i18n/t/AddTextdomainTest.php	(working copy)
@@ -0,0 +1,29 @@
+<?php
+
+require_once dirname( dirname( __FILE__ ) ) . '/add-textdomain.php';
+
+class AddTextDomainTest extends PHPUnit_Framework_TestCase {
+
+	function setUp() {
+		$this->addtextdomainer = new AddTextdomain;
+	}
+
+	/**
+     * @dataProvider dataStrings
+     */
+	function test_basic_add_textdomain( $source, $expected ) {
+		$tokens   = token_get_all( $source );
+		$result = $this->addtextdomainer->process_tokens( 'foo', $tokens, true );
+		$this->assertEquals( $expected, $result );
+	}
+
+	function dataStrings() {
+		return array(
+			array( "<?php __('string'); ?>", "<?php __('string', 'foo'); ?>" ),
+			array( '<?php __("string"); ?>', "<?php __(\"string\", 'foo'); ?>" ),
+			array( "<?php __( 'string' ); ?>", "<?php __( 'string', 'foo' ); ?>" ),
+			array( '<?php __( "string" ); ?>', "<?php __( \"string\", 'foo' ); ?>" ),
+		);
+	}
+
+}
