{"id":1683,"date":"2013-04-01T23:49:59","date_gmt":"2013-04-01T19:49:59","guid":{"rendered":"http:\/\/sandalov.org\/blog\/?p=1683"},"modified":"2013-12-12T15:20:56","modified_gmt":"2013-12-12T11:20:56","slug":"access-google-translate-from-a-terminal","status":"publish","type":"post","link":"https:\/\/sandalov.org\/blog\/1683\/","title":{"rendered":"Access google translate from a terminal"},"content":{"rendered":"<div class=\"2d836d85a73757df15d0de19d7bef594\" data-index=\"1\" style=\"float: none; margin:10px 0 10px 0; text-align:center;\">\n<script type=\"text\/javascript\"><!--\r\ngoogle_ad_client = \"ca-pub-2287684400845931\";\r\n\/* valerymorozov *\/\r\ngoogle_ad_slot = \"5606708872\";\r\ngoogle_ad_width = 728;\r\ngoogle_ad_height = 90;\r\n\/\/-->\r\n<\/script>\r\n<script type=\"text\/javascript\"\r\nsrc=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\">\r\n<\/script>\n<\/div>\n<p>Example:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\njohn@raffles3:~$ translate chien\r\ndog\r\njohn@raffles3:~$ translate legs fr\r\njambes\r\njohn@raffles3:~$ translate legs fr en\r\nlegacy\r\njohn@raffles3:~$ translate \u624b\u7d19\r\nLetter\r\njohn@raffles3:~$ translate \u624b\u7d19 zh-TW en\r\nToilet paper\r\njohn@raffles3:~$ translate --help\r\ntranslate &lt;text&gt; &#x5B;&#x5B;&lt;source language&gt;] &lt;target language&gt;]\r\nif target missing, use DEFAULT_TARGET_LANG\r\nif source missing, use auto\r\n<\/pre>\n<p>Requirements:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt-get install curl html2text\r\n<\/pre>\n<p>The script:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n# access translate.google.com from terminal\r\n\r\nhelp='translate &lt;text&gt; &#x5B;&#x5B;&lt;source language&gt;] &lt;target language&gt;]\r\nif target missing, use DEFAULT_TARGET_LANG\r\nif source missing, use auto'\r\n\r\n# adjust to taste\r\nDEFAULT_TARGET_LANG=en\r\n\r\nif &#x5B;&#x5B; $1 = -h || $1 = --help ]]\r\nthen\r\n    echo &quot;$help&quot;\r\n    exit\r\nfi\r\n\r\nif &#x5B;&#x5B; $3 ]]; then\r\n    source=&quot;$2&quot;\r\n    target=&quot;$3&quot;\r\nelif &#x5B;&#x5B; $2 ]]; then\r\n    source=auto\r\n    target=&quot;$2&quot;\r\nelse\r\n    source=auto\r\n    target=&quot;$DEFAULT_TARGET_LANG&quot;\r\nfi\r\n\r\nresult=$(curl -s -i --user-agent &quot;&quot; -d &quot;sl=$source&quot; -d &quot;tl=$target&quot; --data-urlencode &quot;text=$1&quot; http:\/\/translate.google.com)\r\nencoding=$(awk '\/Content-Type: .* charset=\/ {sub(\/^.*charset=&#x5B;&quot;'\\'']?\/,&quot;&quot;); sub(\/&#x5B; &quot;'\\''].*$\/,&quot;&quot;); print}' &lt;&lt;&lt;&quot;$result&quot;)\r\n#iconv -f $encoding &lt;&lt;&lt;&quot;$result&quot; | awk 'BEGIN {RS=&quot;&lt;div&quot;};\/&lt;span&#x5B;^&gt;]* id=&#x5B;&quot;'\\'']?result_box&#x5B;&quot;'\\'']?\/ {sub(\/^.*id=&#x5B;&quot;'\\'']?result_box&#x5B;&quot;'\\'']?(&gt;| &#x5B;^&gt;]*&gt;)(&#x5B; \\n\\t]*&lt;&#x5B;^&gt;]*&gt;)*\/,&quot;&quot;);sub(\/&lt;.*$\/,&quot;&quot;);print}' | html2text -utf8\r\niconv -f $encoding &lt;&lt;&lt;&quot;$result&quot; |  awk 'BEGIN {RS=&quot;&lt;\/div&gt;&quot;};\/&lt;span&#x5B;^&gt;]* id=&#x5B;&quot;'\\'']?result_box&#x5B;&quot;'\\'']?\/' | html2text -utf8\r\nexit\r\n<\/pre>\n<p>Credits:<a href=\"http:\/\/crunchbang.org\/forums\/viewtopic.php?id=17034\" target=\"_blank\">johnraff<\/a><\/p>\n<div class=\"2d836d85a73757df15d0de19d7bef594\" data-index=\"1\" style=\"float: none; margin:10px 0 10px 0; text-align:center;\">\n<script type=\"text\/javascript\"><!--\r\ngoogle_ad_client = \"ca-pub-2287684400845931\";\r\n\/* valerymorozov *\/\r\ngoogle_ad_slot = \"5606708872\";\r\ngoogle_ad_width = 728;\r\ngoogle_ad_height = 90;\r\n\/\/-->\r\n<\/script>\r\n<script type=\"text\/javascript\"\r\nsrc=\"http:\/\/pagead2.googlesyndication.com\/pagead\/show_ads.js\">\r\n<\/script>\n<\/div>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Example: john@raffles3:~$ translate chien dog john@raffles3:~$ translate legs fr jambes john@raffles3:~$ translate legs fr en legacy john@raffles3:~$ translate \u624b\u7d19 Letter john@raffles3:~$ translate \u624b\u7d19 zh-TW en Toilet paper john@raffles3:~$ translate &#8211;help translate &lt;text&gt; &#x5B;&#x5B;&lt;source language&gt;] &lt;target language&gt;] if target missing, use DEFAULT_TARGET_LANG if source missing, use auto Requirements: sudo apt-get install &hellip; <a class=\"continue-reading-link\" href=\"https:\/\/sandalov.org\/blog\/1683\/\"> Continue reading <span class=\"meta-nav\">&rarr; <\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1683","post","type-post","status-publish","format-standard","hentry","category-unsorted"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/sandalov.org\/blog\/wp-json\/wp\/v2\/posts\/1683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sandalov.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sandalov.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sandalov.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sandalov.org\/blog\/wp-json\/wp\/v2\/comments?post=1683"}],"version-history":[{"count":4,"href":"https:\/\/sandalov.org\/blog\/wp-json\/wp\/v2\/posts\/1683\/revisions"}],"predecessor-version":[{"id":1907,"href":"https:\/\/sandalov.org\/blog\/wp-json\/wp\/v2\/posts\/1683\/revisions\/1907"}],"wp:attachment":[{"href":"https:\/\/sandalov.org\/blog\/wp-json\/wp\/v2\/media?parent=1683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sandalov.org\/blog\/wp-json\/wp\/v2\/categories?post=1683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sandalov.org\/blog\/wp-json\/wp\/v2\/tags?post=1683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}