Skip to content

Instantly share code, notes, and snippets.

@hnq90
Last active August 27, 2015 09:08
Show Gist options
  • Select an option

  • Save hnq90/03ed7e66238d80e3d0a5 to your computer and use it in GitHub Desktop.

Select an option

Save hnq90/03ed7e66238d80e3d0a5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
$dictionaries = include "primary.php";
$apiKey = 'API-KEY';
$total = count($dictionaries);
$i = 0;
$dict = array();
foreach ($dictionaries as $key => $value) {
$i++;
$url = 'https://www.googleapis.com/language/translate/v2?q=' . rawurlencode($key) . '&source=ja&target=en' . '&key=' . $apiKey;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handle);
$responseDecoded = json_decode($response, true);
$responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); //Here we fetch the HTTP response code
curl_close($handle);
if ($responseCode == 200) {
$dict[$key] = $responseDecoded['data']['translations'][0]['translatedText'];
echo "{$i}: {$key} - {$dict[$key]} \n";
}
if ($i%100 == 0 || $i == $total) {
ob_start();
ob_end_clean();
$new_dict = "new_primary.php";
$handle = fopen($new_dict, 'a');
$search = array("\\", "'");
$replace = array("\\\\", "\'");
if (!$handle) {
echo 'Cannot open file: ' . $new_dict;
}
foreach ($dict as $key => $value) {
$key = mb_convert_kana($key, 'KVa');
fwrite($handle, '\'' . str_replace($search, $replace, $key) . '\' => \'' . str_replace($search, $replace, $value) . '\',' . "\n");
}
fclose($handle);
$dict = array();
sleep(5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment