Skip to content

Instantly share code, notes, and snippets.

@tranghaviet
Last active September 14, 2019 10:59

Revisions

  1. tranghaviet revised this gist Sep 14, 2019. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Google TTS
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    # Add Audio Program type to GoldenDict
    bash -c "p=$(echo "%GDWORD%" |sed 's/ /+/g'); wget -q -U Mozilla -O - translate.google.com.vn/translate_tts?ie=UTF-8\&q=$p\&tl=en\&client=tw-ob | mpg123 -"
  2. tranghaviet created this gist Sep 14, 2019.
    34 changes: 34 additions & 0 deletions google-goldendict.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    <?php

    // Usage
    // php -f /path/to/google-goldendict.php "words you want to translate
    // In GoldenDict go to Dictionaries/Programs, choose "Add..."
    // name: Google translation
    // Type: HTML
    // Command Line: php -f /path/to/google-goldendict.php %GDWORD%

    $text = $argv[1];

    $postdata = http_build_query(['q' => $text]);

    $opts = [
    'http' => [
    'method' => 'POST',
    'header' => 'Content-type: application/x-www-form-urlencoded',
    'content' => $postdata,
    ],
    ];

    $context = stream_context_create($opts);
    $url = 'http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=vi&dt=t&ie=UTF-8&oe=UTF-8';
    $resultText = file_get_contents($url, false, $context);
    $resultText = preg_replace('/,+/', ',', $resultText);

    $results = json_decode($resultText);
    if (is_array($results) && count($results) > 0) {
    foreach ($results[0] as $result) {
    echo str_replace("\n", '
    ', $result[0]);
    }
    }
    echo "<hr><a href='https://translate.google.com/#auto/en/$text'>Open in Google Translate</a>";
    5,509 changes: 5,509 additions & 0 deletions trans.sh
    5,509 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.