Skip to content

Commit 87ed119

Browse files
committed
Fix #12 - Cache doesn't take into account changed language
1 parent 5b62f73 commit 87ed119

5 files changed

Lines changed: 15 additions & 32 deletions

File tree

composer.json

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
{
22
"name": "om/potrans",
33
"description": "Command line tool for translate Gettext with Google Translator API",
4-
"keywords": [
5-
"potrans",
6-
"gettext",
7-
"google",
8-
"translator"
9-
],
10-
"license": [
11-
"BSD-3-Clause"
12-
],
4+
"keywords": ["potrans", "gettext", "google", "translator"],
5+
"license": ["BSD-3-Clause"],
136
"authors": [
147
{
158
"name": "Roman Ožana",
169
"email": "roman@ozana.cz",
1710
"homepage": "https://ozana.cz"
1811
}
1912
],
20-
"bin": [
21-
"bin/potrans"
22-
],
13+
"bin": ["bin/potrans"],
2314
"require": {
2415
"symfony/console": "^6.0",
2516
"gettext/gettext": "^5.6",
2617
"google/cloud-translate": "^1.12",
27-
"symfony/cache": "^6.0"
18+
"symfony/cache": "^6.0",
19+
"ext-curl": "*",
20+
"ext-json": "*"
2821
},
2922
"require-dev": {
3023
"nette/tester": "^2.3"

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ bin/potrans google tests/example-cs_CZ.po ~/Downloads --credentials=your-credent
4848
You can also change source and target language with `--form` and `--to` parametters:
4949

5050
```bash
51-
bin/potrans google tests/example-cs_CZ.po ~/Downloads --credentials=your-credentials-file.json --from=ru --to=en
51+
bin/potrans google tests/example-cs_CZ.po ~/Downloads --credentials=your-credentials-file.json --from=en --to=de
5252
```
5353

5454
### Google Translate API Pricing

src/commands/DeepLTranslatorCommand.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
use Gettext\Generator\PoGenerator;
77
use Gettext\Loader\PoLoader;
88
use Gettext\Translation;
9-
use GuzzleHttp\Client;
10-
use potrans\commands\DeepLTranslator;
11-
use potrans\commands\GoogleTranslator;
12-
use potrans\commands\ITranslator;
139
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
1410
use Symfony\Component\Console\Command\Command;
1511
use Symfony\Component\Console\Exception\InvalidOptionException;
@@ -19,7 +15,7 @@
1915
use Symfony\Component\Console\Input\InputInterface;
2016
use Symfony\Component\Console\Input\InputOption;
2117
use Symfony\Component\Console\Output\OutputInterface;
22-
use Symfony\Component\HttpClient\HttpClient;
18+
use Throwable;
2319

2420
class DeepLTranslatorCommand extends Command {
2521

@@ -82,7 +78,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8278
// translated counter
8379
$translated++;
8480

85-
$translation = $cache->getItem(md5($sentence->getOriginal()));
81+
$key = md5($sentence->getOriginal() . $from . $to);
82+
$translation = $cache->getItem($key);
8683
if (!$translation->isHit() || !$input->getOption('cache')) {
8784

8885
$curl = curl_init();
@@ -169,7 +166,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
169166
// done!
170167
$output->writeln('<info>DONE!</info>');
171168

172-
} catch (\Throwable $error) {
169+
} catch (Throwable $error) {
173170
$output->writeln(
174171
[
175172
'',

src/commands/GoogleTranslatorCommand.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,17 @@
77
use Gettext\Loader\PoLoader;
88
use Gettext\Translation;
99
use Gettext\Translations;
10-
use Google\ApiCore\ValidationException;
1110
use Google\Cloud\Translate\V3\TranslationServiceClient;
12-
use potrans\commands\DeepLTranslator;
13-
use potrans\commands\GoogleTranslator;
14-
use potrans\commands\ITranslator;
1511
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
16-
use Symfony\Component\Cache\Adapter\NullAdapter;
1712
use Symfony\Component\Console\Command\Command;
1813
use Symfony\Component\Console\Exception\InvalidOptionException;
1914
use Symfony\Component\Console\Exception\RuntimeException;
2015
use Symfony\Component\Console\Helper\ProgressBar;
2116
use Symfony\Component\Console\Input\InputArgument;
22-
use Symfony\Component\Console\Input\InputDefinition;
2317
use Symfony\Component\Console\Input\InputInterface;
2418
use Symfony\Component\Console\Input\InputOption;
2519
use Symfony\Component\Console\Output\OutputInterface;
26-
use Symfony\Contracts\Cache\CacheInterface;
27-
use Symfony\Contracts\Cache\ItemInterface;
20+
use Throwable;
2821

2922
class GoogleTranslatorCommand extends Command {
3023

@@ -113,7 +106,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
113106
// translated counter
114107
$translated++;
115108

116-
$translation = $cache->getItem(md5($sentence->getOriginal()));
109+
$key = md5($sentence->getOriginal() . $from . $to);
110+
$translation = $cache->getItem($key);
117111

118112
if (!$translation->isHit() || !$input->getOption('cache')) {
119113

@@ -178,7 +172,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
178172
$poGenerator->generateFile($translations, $poOutputFile);
179173

180174
$output->writeln('<info>DONE!</info>');
181-
} catch (\Throwable $error) {
175+
} catch (Throwable $error) {
182176
$output->writeln(
183177
[
184178
'',

src/potrans.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use potrans\commands\DeepLTranslatorCommand;
66
use potrans\commands\GoogleTranslatorCommand;
7-
use potrans\commands\BaseInputDefinition;
87
use Symfony\Component\Console\Application;
98

109
if (file_exists(__DIR__ . '/../vendor/autoload.php')) {

0 commit comments

Comments
 (0)