Commit 258f5eac authored by totten's avatar totten

Remove (js,php,smarty,)-extractor.php. Replaced by civistrings.

parent bb4eb242
#!/usr/bin/php
<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License along with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2013
* $Id$
*
*/
/**
* ts() and {ts} calls extractor
*
* Extracts translatable strings from CiviCRM PHP source (ts() calls) and
* Smarty templates ({ts} calls). Outputs a POT file on STDOUT, errors on
* STDERR.
*
* Called from bin/create-pot-files.sh
*/
$phpModifier = "-iname '*.php' ";
$jsModifier = "-iname '*.js' ";
$smartyModifier = "\( -iname '*.tpl' -or -iname '*.hlp' \) ";
if ($argv[1] == 'base') {
// 'base' is a special case for the common-base.pot file
// it fetches a list of directories from bin/basedirs
$phpDir = array();
$jsDir = array();
$tplDir = array();
$phpDir[] = "-iwholename '*/packages/HTML/QuickForm/*'";
foreach (explode("\n", file_get_contents('bin/basedirs')) as $dir) {
$phpDir[] = "-iwholename '*/CRM/$dir/*'";
$jsDir[] = "-iwholename '*/templates/CRM/$dir/*'";
$tplDir[] = "-iwholename '*/templates/CRM/$dir/*'";
}
$phpModifier .= "\( " . implode(' -or ', $phpDir) . " \)";
$jsModifier .= "\( " . implode(' -or ', $jsDir) . " \)";
$smartyModifier .= "\( " . implode(' -or ', $tplDir) . " \)";
}
elseif ($argv[1] == 'extension' || $argv[1] == 'install') {
// nothing to do
// see special handling of $command below.
}
else {
$phpModifier .= "-iwholename '*/CRM/{$argv[1]}/*'";
$jsModifier .= "-iwholename '*/templates/CRM/{$argv[1]}/*'";
$smartyModifier .= "-iwholename '*/templates/CRM/{$argv[1]}/*'";
}
$component = $argv[1];
$dir = $argv[2];
/**
* PHP extraction
*/
function phpExtraction($dir, $component, $phpModifier) {
$phpExtractor = dirname(__FILE__) . '/php-extractor.php';
$command = "find $dir/CRM $dir/packages/HTML/QuickForm $phpModifier -not -wholename '*/CRM/Core/I18n.php' -not -wholename '*/CRM/Core/Smarty/plugins/block.ts.php' | grep -v '/\.svn/' | sort | xargs $phpExtractor $dir";
if ($component == 'extension') {
$command = "find ./ | grep -E '\.(php|inc)' | sort | xargs $phpExtractor $dir";
}
elseif ($component == 'install') {
$command = "find $dir/*.php $dir/*.html | sort | xargs $phpExtractor $dir";
}
fwrite(STDERR, "Running: $command\n");
$phpPot = `$command`;
return $phpPot;
}
/**
* JS extraction
*/
function jsExtraction($dir, $component, $jsModifier) {
$jsExtractor = dirname(__FILE__) . '/js-extractor.php';
$command = "find $dir/js $dir/templates $dir/xml $jsModifier | grep -v '/\.svn/' | sort | xargs $jsExtractor $dir";
if ($component == 'extension') {
$command = "find ./ | grep -vE '\.(git|svn)/' | sort | xargs $jsExtractor $dir";
}
fwrite(STDERR, "Running: $command\n");
$jsPot = `$command`;
return $jsPot;
}
/**
* Smarty/tpl extraction
*/
function smartyExtraction($dir, $component, $smartyModifier) {
$smartyExtractor = dirname(__FILE__) . '/smarty-extractor.php';
$command = "find $dir/templates $dir/xml $smartyModifier | grep -v '/\.svn/' | sort | xargs $smartyExtractor $dir";
if ($component == 'extension') {
$command = "find $dir/templates $dir/xml $smartyModifier | grep -vE '\.(svn|git)/' | sort | xargs $smartyExtractor $dir";
}
fwrite(STDERR, "Running: $command\n");
$smartyPot = `$command`;
return $smartyPot;
}
$phpPot = phpExtraction($dir, $component, $phpModifier);
$jsPot = jsExtraction($dir, $component, $jsModifier);
$smartyPot = smartyExtraction($dir, $component, $smartyModifier);
$block = array();
$blocks = array();
$msgidArray = array();
$resultArray = array();
// rewrite the header to resultArray, removing it from the original
$originalArray = explode("\n", $phpPot . $jsPot . $smartyPot);
while ($originalArray[0] != '') {
$resultArray[] = array_shift($originalArray);
}
$resultArray[] = array_shift($originalArray);
// break the POT contents into separate comments/msgid blocks
foreach ($originalArray as $line) {
// if it's the end of a block, put the $block in $blocks and start a new one
if ($line == '' and $block != array()) {
$blocks[] = $block;
$block = array();
}
else {
// else add the line to the proper $block part
// the lines in the POT file are either comments, single- and multiline
// msgids or empty msgstrs; we ignore the msgstrs
if (substr($line, 0, 1) == '#') {
$block['comments'][] = $line;
}
elseif (substr($line, 0, 6) != 'msgstr') {
$block['msgid'][] = $line;
}
}
}
// combine the msgid parts into single strings and build a new array with msgid
// as key and arrays with comments as value; drop the empty msgids
foreach ($blocks as $block) {
$msgid = implode("\n", $block['msgid']);
if ($msgid != 'msgid ""') {
foreach ($block['comments'] as $comment) {
$msgidArray[$msgid][] = $comment;
}
}
}
// combine the comments indicating the source files into single comment lines
foreach ($msgidArray as $msgid => $commentsArray) {
$newCommentsArray = array();
$sourceComments = array();
foreach ($commentsArray as $comment) {
if (substr($comment, 0, 3) == '#: ') {
$sourceComments[] = substr($comment, 3);
}
else {
$newCommentsArray[] = $comment;
}
}
if (count($sourceComments)) {
$newCommentsArray[] = '#: ' . implode(' ', $sourceComments);
}
$msgidArray[$msgid] = $newCommentsArray;
}
// build the rest of the $resultArray from the $msgidArray
foreach ($msgidArray as $msgid => $commentsArray) {
foreach ($commentsArray as $comment) {
$resultArray[] = $comment;
}
$resultArray[] = $msgid;
// if it's a plural, add plural msgstr, else add singular
if (strpos($msgid, "\nmsgid_plural ")) {
$resultArray[] = "msgstr[0] \"\"\nmsgstr[1] \"\"\n";
}
else {
$resultArray[] = "msgstr \"\"\n";
}
}
// output the $resultArray to STDOUT
fwrite(STDOUT, implode("\n", $resultArray));
#!/usr/bin/php
<?php
/**
* js-extractor.php - rips gettext strings from Javascript ts() calls
*
* ------------------------------------------------------------------------- *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
* ------------------------------------------------------------------------- *
*
* This command line script rips gettext strings from js file, and prints
* them to stdout; this can later be used with the standard gettext tools.
*
* Usage:
* ./js-extractor.php <filename or directory> [file2, ...]
*
* If a parameter is a directory, the template files within will be parsed.
*
* @version $Id$
* @link http://js-gettext.sf.net/
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
/**
* Bootstrap, process command line arguments, and kick off the real work.
*/
function main($argc, $argv) {
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
global $root, $extensions;
$root = $argv[1];
array_splice($argv, 1, 1);
// extensions of js files, used when going through a directory
$extensions = array('js');
for ($ac = 1; $ac < $argc; $ac++) {
if (is_dir($argv[$ac])) {
do_dir($argv[$ac]);
}
else {
do_file($argv[$ac]);
}
}
}
/**
* "fix" string - strip slashes, escape and convert new lines to \n
* @link http://issues.civicrm.org/jira/browse/CRM-10833
*/
function fs($text) {
$quote = $text[0];
// Remove newlines
$text = str_replace("\\\n", '', $text);
// Unescape escaped quotes
$text = str_replace('\\' . $quote, $quote, $text);
// Remove end quotes
$text = substr(ltrim($text, $quote), 0, -1);
// Escape double quotes
$text = str_replace('"', '\"', $text);
return $text;
}
/**
* Rips gettext strings from $file and prints them in C format.
*/
function do_file($file) {
$content = @file_get_contents($file);
$strings = array();
if (empty($content)) {
return;
}
global $root;
// Match all calls to ts()
// Note: \s also matches newlines with the 's' modifier.
preg_match_all('~
[^\w]ts\s* # match "ts" with whitespace
\(\s* # match "(" argument list start
((?:(?:\'(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*")(?:\s*\+\s*)?)+)\s*
[,\)] # match ")" or "," to finish
~sx', $content, $matches);
foreach ($matches[1] as $text) {
if ($text = fs($text)) {
print '#: ' . substr($file, strlen($root) + 1) . "\n";
print 'msgid "' . $text . "\"\n";
print "msgstr \"\"\n\n";
}
}
}
/**
* Go through a directory.
*/
function do_dir($dir) {
$d = dir($dir);
while (FALSE !== ($entry = $d->read())) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry = $dir . '/' . $entry;
if (is_dir($entry)) {
// if a directory, go through it
do_dir($entry);
}
else {
// if file, parse only if extension is matched
$pi = pathinfo($entry);
if (isset($pi['extension']) && in_array($pi['extension'], $GLOBALS['extensions'])) {
do_file($entry);
}
}
}
$d->close();
}
main($_SERVER['argc'], $_SERVER['argv']);
This diff is collapsed.
#!/usr/bin/php
<?php
/**
* smarty-extractor.php - rips gettext strings from Smarty {ts} calls
*
* ------------------------------------------------------------------------- *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
* ------------------------------------------------------------------------- *
*
* This command line script rips gettext strings from smarty file, and prints
* them to stdout; this can later be used with the standard gettext tools.
*
* Usage:
* ./smarty-extractor.php <filename or directory> [file2, ...]
*
* If a parameter is a directory, the template files within will be parsed.
*
* @version $Id$
* @link http://smarty-gettext.sf.net/
* @author Sagi Bashari <sagi@boom.org.il>
* @author Piotr Szotkowski <shot@civicrm.org>
* @copyright 2004 Sagi Bashari
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
/**
* Bootstrap, process command line arguments, and kick off the real work.
*/
function main($argc, $argv) {
global $ldq, $rdq, $cmd, $root, $extensions;
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
$root = $argv[1];
array_splice($argv, 1, 1);
// smarty open tag
$ldq = preg_quote('{');
// smarty close tag
$rdq = preg_quote('}');
// smarty command
$cmd = preg_quote('ts');
// extensions of smarty files, used when going through a directory
$extensions = array('tpl', 'hlp');
for ($ac = 1; $ac < $argc; $ac++) {
if (is_dir($argv[$ac])) {
do_dir($argv[$ac]);
}
else {
do_file($argv[$ac]);
}
}
}
/**
* "fix" string - strip slashes, escape and convert new lines to \n
*/
function fs($str) {
$str = stripslashes($str);
$str = str_replace('"', '\"', $str);
$str = str_replace("\n", '\n', $str);
return $str;
}
/**
* Rips gettext strings from $file and prints them in C format.
*/
function do_file($file) {
$content = @file_get_contents($file);
if (empty($content)) {
return;
}
global $ldq, $rdq, $cmd, $root;
// if there’s a {php} tag, fetch its contents into a file and parse it with php-extractor.php
$phpTagMatches = array();
preg_match_all("/{$ldq}\s*(php)\s*([^{$rdq}]*){$rdq}([^{$ldq}]*){$ldq}\/\\1{$rdq}/", $content, $phpTagMatches);
$phpCode = $phpTagMatches[3][0];
if ($phpCode) {
// we want to create a file with the same path and name, but under the
// tempdir; this allows the php-extractor.php to add the right comment
$tempdir = sys_get_temp_dir();
$filedir = substr(dirname($file), strlen($root) + 1);
$filename = basename($file);
@mkdir("$tempdir/$filedir", 0777, TRUE);
file_put_contents("$tempdir/$filedir/$filename", "<?php $phpCode ?>");
passthru("bin/php-extractor.php $tempdir $tempdir/$filedir/$filename");
}
preg_match_all("/{$ldq}\s*({$cmd})\s*([^{$rdq}]*){$rdq}([^{$ldq}]*){$ldq}\/\\1{$rdq}/", $content, $matches);
for ($i = 0; $i < count($matches[0]); $i++) {
$output = array();
$output[] = '#: ' . substr($file, strlen($root) + 1);
if (preg_match('/plural\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) {
$output[] = 'msgid "' . fs($matches[3][$i]) . '"';
$output[] = 'msgid_plural "' . fs($match[1]) . '"';
$output[] = 'msgstr[0] ""';
$output[] = 'msgstr[1] ""';
}
else {
$output[] = 'msgid "' . fs($matches[3][$i]) . '"';
$output[] = 'msgstr ""';
}
print implode("\n", $output) . "\n\n";
}
preg_match_all("/{$ldq}\s*(docURL)\s*([^{$rdq}]*){$rdq}/", $content, $matches);
for ($i = 0; $i < count($matches[0]); $i++) {
if (preg_match('/text\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) {
print '#: ' . substr($file, strlen($root) + 1) . "\n";
print 'msgid "' . fs($match[1]) . "\"\n";
print "msgstr \"\"\n\n";
}
if (preg_match('/title\s*=\s*["\']?\s*(.[^\"\']*)\s*["\']?/', $matches[2][$i], $match)) {
print '#: ' . substr($file, strlen($root) + 1) . "\n";
print 'msgid "' . fs($match[1]) . "\"\n";
print "msgstr \"\"\n\n";
}
}
}
/**
* Go through a directory.
*/
function do_dir($dir) {
$d = dir($dir);
while (FALSE !== ($entry = $d->read())) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry = $dir . '/' . $entry;
if (is_dir($entry)) {
// if a directory, go through it
do_dir($entry);
}
else {
// if file, parse only if extension is matched
$pi = pathinfo($entry);
if (isset($pi['extension']) && in_array($pi['extension'], $GLOBALS['extensions'])) {
do_file($entry);
}
}
}
$d->close();
}
main($_SERVER['argc'], $_SERVER['argv']);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment