Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lucaswxp/915f1635d8f210a9a077 to your computer and use it in GitHub Desktop.
Save lucaswxp/915f1635d8f210a9a077 to your computer and use it in GitHub Desktop.
CakePHP script for generating pot translations files

This script executes cake i18n extract with all the defaults args and returns the command output.

function extractPotFiles(){
  $app = ROOT . DS . APP_DIR;
  $cake = $app . DS . 'Console' . DS . 'cake';
  $olddir = getcwd();
  chdir($app);

  $descriptorspec = array(
     0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
     1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
     2 => array('pipe', 'a')
  );

  $process = proc_open("$cake i18n extract --overwrite", $descriptorspec, $pipes);

  $output = '';
  if (is_resource($process)) {
      fwrite($pipes[0], "\r\n");
      fwrite($pipes[0], "\r\n");
      fwrite($pipes[0], "y\r\n"); // Would you like to extract the messages from the CakePHP core
      fwrite($pipes[0], "\r\n");
      fwrite($pipes[0], "\r\n");
      fclose($pipes[0]);

      $output .= stream_get_contents($pipes[1]);
      fclose($pipes[1]);

      proc_close($process);
  }
  chdir($olddir);
  return $output;
}

The return will be something like:

Welcome to CakePHP v2.6.1 Console
---------------------------------------------------------------
App : app
Path: /Applications/AMPPS/www/doare/new/doare-app/app/
---------------------------------------------------------------
Current paths: None
What is the path you would like to extract?
[Q]uit [D]one  
[/Applications/AMPPS/www/project/app/] > 
Current paths: /Applications/AMPPS/www/projectapp/app/
What is the path you would like to extract?
[Q]uit [D]one  
[D] > 
Would you like to extract the messages from the CakePHP core? (y/n) 
[n] > What is the path you would like to output?
[Q]uit  
[/Applications/AMPPS/www/project/app/Locale] > 
Would you like to merge all domain and category strings into the default.pot file? (y/n) 
[n] > 

Extracting...
---------------------------------------------------------------
Paths:
   /Applications/AMPPS/www/project/app/
   /Applications/AMPPS/www/project/lib/Cake/
Output Directory: /Applications/AMPPS/www/project/app/Locale/
---------------------------------------------------------------

Done.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment