V
V
vgrayster2012-06-04 06:50:29
PHP
vgrayster, 2012-06-04 06:50:29

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

7 answer(s)
E
egorinsk, 2012-06-04
@egorinsk

A good reason to learn Chinese, isn't it?

G
Gregory, 2012-06-04
@difiso

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.

I
Ivan Shalganov, 2012-06-04
@Aco

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);
}

E
EugeneOZ, 2012-06-04
@EugeneOZ

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)

S
Sergey, 2012-06-04
Protko @Fesor

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);
}

The example is abstract, but the essence should be clear. Although it is logical to drive this regular season through preg_replace_callback.

E
eaa, 2012-06-04
@eaa

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.

V
Vampiro, 2012-06-04
@Vampiro

It seems to me that you can feed the entire page, indicating the translation from Chinese to English.
Although it is difficult to say how he will react to the regular season.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question