Skip to content

Instantly share code, notes, and snippets.

@AminulBD
Created June 12, 2018 12:39
Show Gist options
  • Save AminulBD/a16fa2958549a7384cd1b5e8d44ebf75 to your computer and use it in GitHub Desktop.
Save AminulBD/a16fa2958549a7384cd1b5e8d44ebf75 to your computer and use it in GitHub Desktop.
Translate string via CLI with PHP
<?php
$strings = [
'KEY_1' => 'Hello',
'KEY_2' => 'World';
];
$sourceLang = 'en';
$transLang = 'es';
$devMail = 'YOUR_API_MAIL@YOU.com';
$chunks = array_chunk($strings, 50, true);
$i = 1;
foreach ($chunks as $chunk ) {
echo 'Translate in proccess: ' . count($chunk) * $i . ' of ' . count($strings) . PHP_EOL;
foreach ($chunk as $key => $value) {
$url = 'http://api.mymemory.translated.net/get?q=' . urlencode($value) . '&de=' . $devMail . '&langpair=' . $sourceLang . '|' . $transLang;
$request = file_get_contents($url);
$result = json_decode($request);
$response = $result->responseData;
$en = "('EN', '{$key}', '{$value}'),\n";
$es = "('ES', '{$key}', '{$response->translatedText}'),\n";
$data = $en . $es;
file_put_contents('translated-strings.txt', $data, FILE_APPEND | LOCK_EX);
}
$i++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment