Created
June 12, 2018 12:39
-
-
Save AminulBD/a16fa2958549a7384cd1b5e8d44ebf75 to your computer and use it in GitHub Desktop.
Translate string via CLI with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$strings = [ | |
'KEY_1' => 'Hello', | |
'KEY_2' => 'World'; | |
]; | |
$sourceLang = 'en'; | |
$transLang = 'es'; | |
$devMail = '[email protected]'; | |
$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