How to get $this->translate('Content') => Content in a Textfile
I am looking for a shell script which scans a direcotry and all its subdirectories for .php and .phtml files. Within these files, I am looking for $this->translate('') statements (also $this->view->translate('')) and I want to s开发者_高级运维ave the content of these statements in a textfile.
The problem is, that there are several different types of this statements:
- $this->translate('Single Quotes') // I need: Single Quotes
- $this->translate("Double Quotes") // I need: Double Quotes
- $this->translate('Single quotes with %1$s placeholders', $xy) // I need: Single quotes with %1$s placeholders
- $this->translate("Double quotes with %1\$s", $xy) // I need: Dobule quotes with %1$s
- $this->view->translate('With view') // I need: With view
- $this->view->translate("With view 2") // I need: With view 2
- $this->translate('Single Quotes with "Doubles"') // I need: Single Quotes with "Doubles"
- $this->translate("Double Quotes with 'Singles') // I need: Double Quotes with 'Singles'
I have already programmed a script and a guy from starmind.com sent me the following lines:
echo -n > give_me_your_favorite_outfile_name.txt
for i in `find . -iname '*php' `
do
echo -n "Processing $i ..."
# echo " +++++++ from $i ++++++++" >> give_me_your_favorite_outfile_name.txt
cat $i | sed -n -e '/->translate(*/p' | sed -e 's/\(.*->translate(.\)\([a-z A-Z \d092\d039\d034]*\)\(.*\)/\2/g' | sed -e 's/\(.*\)\(\d039\)/\1/g' | sed -e 's/\(.*\)\(\d034\)/\1/g' >> give_me_your_favorite_outfile_name.txt
echo " done"
done
for i in `find . -iname '*phtml' `
do
echo -n "Processing $i ..."
# echo " +++++++ from $i ++++++++" >> give_me_your_favorite_outfile_name.txt
cat $i | sed -n -e '/->translate(*/p' | sed -e 's/\(.*->translate(.\)\([a-z A-Z \d092\d039\d034]*\)\(.*\)/\2/g' | sed -e 's/\(.*\)\(\d039\)/\1/g' | sed -e 's/\(.*\)\(\d034\)/\1/g' >> give_me_your_favorite_outfile_name.txt
echo " done"
done
Unfortunately, it does not cover all the above cases, especially the Quotes within Quotes cases. As I am not a shell expert at all and need that script for a verification process, I would be very happy to get help from you guys.
Important: It has to be written in Shell. A PHP Version exists.
find /path -type f \( -name "*.php" -o -name "*.phtml" \) | while IFS= read -r -d $'\0' file
do
while read -r line
do
case "$line" in
*'$this->translate'* | *'$this->view->translate'* )
line="${line#*this*translate(}"
line="${line%%)*}"
case ${line:0:1} in
\$) s=${line:0};;
*) s=${line:1:${#line}-2};;
esac
case "$s" in
*[\"\'],* )
s=${s/\\/}
echo ${s%%[\"\'],*};;
* ) echo "$s";;
esac
esac
done < "$file"
done
This does it using sed
in a Bash while
loop and demonstrates another way to do the find
for variety's sake:
find . -iregex ".*\.php\|.*\.phtml" |
while read f
do
sed -n '/[\"\o047]/ {s/$this->\(view->\|\)translate([\"\o047]\(.*\)[\"\o047].*)/\2/; s.\\..;p}' $f
done > outputfile.txt
Edit:
To take care of other text on the line change the sed
command to this:
sed -n '/[\"\o047]/ {s/.*$this->\(view->\|\)translate([\"\o047]\(.*\)[\"\o047].*).*/\2/; s.\\..;p}' $f
(Just add a .*
at the beginning and end of the search string.)
精彩评论