Answer the question
In order to leave comments, you need to log in
How to translate comments in *.php files?
I inherited a project that was written by the brave Chinese. About 200 files and all comments are in Chinese :).
How can all these comments be automatically (maybe with the help of Google Translate) translated into English. It is clear that the translation will be crooked, but it's better than Chinese comments :)
Answer the question
In order to leave comments, you need to log in
I think that the easiest way, on such volumes, is to learn Chinese to write a script: we tear out a comment, ask Google and replace or supplement the comment in the file with Google's answer.
Try this (make sure you have tokenizer enabled ):
<?php
const SCRIPTS_DIR = '/data/www/framework/lib';
$dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(SCRIPTS_DIR));
$replace = array(
'<hh user=param>' => '#PARAM#',
'<hh user=return>' => '#RETURN#',
'<hh user=desc>' => '#DESC#',
'<hh user=throws>' => '#THROWS#',
'<hh user=see>' => '#SEE#',
'<hh user=link>' => '#LINK#',
'<hh user=access>' => '#ACCESS#',
'<hh user=since>' => '#SINCE#',
'<hh user=static>' => '#STATIC#',
);
foreach($dir as $source) {
if($dir->isDot()) {
continue;
}
$file = file_get_contents($source->getPathname());
$tokens = token_get_all($file);
foreach($tokens as $token) {
if(!is_array($token)) {
continue;
}
if(in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
$text = preg_replace('![\n\r][\t ]*\*!sSim', "\n", $token[1]);
$text = str_replace(array_keys($replace), array_values($replace), $text);
$translate = Yandex::translate($tokens[1]); //
$translate = str_replace(array_values($replace), array_keys($replace), $text);
$file = str_replace($tokens[1], $translate, $file);
}
}
file_put_contents($source->getPathname(), $file);
}
I sympathize.
If there is definitely no chance of rewriting it from scratch, then it may come in handy:
php.net/manual/ru/reflectionclass.getdoccomment.php (for class descriptions)
www.php.net/manual/ru/reflectionfunctionabstract.getdoccomment.php (for methods)
You can tear out comments, as already mentioned above, through reflections. True, this will not help much to replace them.
Here's how you can edit comments:
function translateComments($file)
{
$expr = "/((?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:\/\/.*))/";
$source = file_get_contents($file);
preg_match_all($expr, $comments, $source); //capture the comments
foreach($matchs[0] as $id => $phpdoc){
$source = str_replace($phpdoc, translateText($phpdoc), $source); // replace the scores of empty
}
file_put_contents($file, $source);
}
so ... here, in theory, writing is not very long:
- we take a file.
- we pull out the comment with regexp.
- gives Google.
- we get an answer.
- we stuff it instead of what it was.
it’s unlikely that anyone wrote this, it’s too special a decision, but I did it on pearl, as I described a couple of lines above, though it wasn’t necessary to translate the text, but to replace something else in the text.
for especially fans it is possible to use sed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question