3
3
namespace potrans \commands ;
4
4
5
5
use DeepL \Translator ;
6
- use Gettext \Generator \MoGenerator ;
7
- use Gettext \Generator \PoGenerator ;
8
- use Gettext \Loader \PoLoader ;
9
- use Gettext \Translation ;
6
+ use potrans \PoTranslator ;
7
+ use potrans \translator \DeepLTranslator ;
10
8
use Symfony \Component \Cache \Adapter \FilesystemAdapter ;
9
+ use Symfony \Component \Cache \Adapter \NullAdapter ;
11
10
use Symfony \Component \Console \Command \Command ;
12
11
use Symfony \Component \Console \Exception \InvalidOptionException ;
13
12
use Symfony \Component \Console \Exception \RuntimeException ;
@@ -25,38 +24,46 @@ protected function configure(): void {
25
24
->addArgument ('output ' , InputArgument::OPTIONAL , 'Output PO, MO files directory ' , '~/Downloads ' )
26
25
->addOption ('from ' , null , InputOption::VALUE_REQUIRED , 'Source language (default: en) ' , 'en ' )
27
26
->addOption ('to ' , null , InputOption::VALUE_REQUIRED , 'Target language (default: cs) ' , 'cs ' )
28
- ->addOption ('all ' , null , InputOption::VALUE_NONE , 'Re -translate including translated sentences ' )
27
+ ->addOption ('force ' , null , InputOption::VALUE_NONE , 'Force re -translate including translated sentences ' )
29
28
->addOption ('wait ' , null , InputOption::VALUE_REQUIRED , 'Wait between translations in milliseconds ' , false )
30
29
->addOption ('apikey ' , null , InputOption::VALUE_REQUIRED , 'Deepl API Key ' )
31
30
->addOption ('cache ' , null , InputOption::VALUE_NEGATABLE , 'Load from cache or not ' , true );
32
31
}
33
32
34
33
protected function execute (InputInterface $ input , OutputInterface $ output ): int {
35
- // input PO file loading
36
- $ wait = $ input ->getOption ('wait ' );
37
- $ cache = new FilesystemAdapter ('deepl ' , 3600 , 'cache ' );
38
-
39
34
try {
40
35
36
+ // Input PO file
41
37
$ inputFile = $ input ->getArgument ('input ' );
42
- if (!is_file ($ inputFile )) {
38
+ if (!file_exists ($ inputFile )) {
43
39
throw new RuntimeException (sprintf ('Input file "%s" not found ' , $ inputFile ));
44
40
}
45
41
46
- $ poLoader = new PoLoader ();
47
- $ translations = $ poLoader ->loadFile ($ inputFile );
48
-
49
- // output directory
42
+ // Output directory
50
43
$ outputDir = realpath ($ input ->getArgument ('output ' )) . DIRECTORY_SEPARATOR ;
51
44
if (!is_dir ($ outputDir )) {
52
45
throw new InvalidOptionException ('Invalid directory path: ' . $ outputDir );
53
46
}
54
47
55
- $ from = $ input ->getOption ('from ' );
56
- $ to = $ input ->getOption ('to ' );
57
- $ apikey = $ input ->getOption ('apikey ' );
48
+ // Crete new DeepL translator
49
+ $ apikey = (string ) $ input ->getOption ('apikey ' );
50
+ $ translator = new DeepLTranslator (
51
+ new Translator ($ apikey ),
52
+ );
53
+
54
+ // Setup caching
55
+ $ cache = $ input ->getOption ('cache ' ) ?
56
+ new FilesystemAdapter ('deepl ' , 3600 , 'cache ' ) :
57
+ new NullAdapter ();
58
+
59
+ // Read params
60
+ $ force = (bool ) $ input ->getOption ('force ' );
61
+ $ to = (string ) $ input ->getOption ('to ' );
62
+ $ from = (string ) $ input ->getOption ('from ' );
63
+ $ wait = (int ) $ input ->getOption ('wait ' );
58
64
59
- $ translator = new Translator ($ apikey );
65
+ $ potrans = new PoTranslator ($ translator , $ cache );
66
+ $ translations = $ potrans ->loadFile ($ inputFile );
60
67
61
68
// translator
62
69
$ output ->writeln (
@@ -74,53 +81,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int
74
81
$ progress = new ProgressBar ($ output , count ($ translations ));
75
82
76
83
$ translated = 0 ; // counter
77
- /** @var Translation $sentence */
78
- foreach ($ translations as $ sentence ) {
79
-
80
- if (!$ sentence ->getTranslation () || $ input ->getOption ('all ' )) {
81
- // translated counter
82
- $ translated ++;
83
-
84
- $ key = md5 ($ sentence ->getOriginal () . $ from . $ to );
85
- $ translation = $ cache ->getItem ($ key );
86
-
87
- if (!$ translation ->isHit () || !$ input ->getOption ('cache ' )) {
88
-
89
- // TODO add Text translation options
90
- // @see https://github.com/DeepLcom/deepl-php#text-translation-options
91
- $ response = $ translator ->translateText (
92
- $ sentence ->getOriginal (),
93
- $ from ,
94
- $ to ,
95
- );
96
-
97
- $ translation ->set ($ response ->text ); // set new translation
98
-
99
- // save only successful translations
100
- if ($ response ->text && $ input ->getOption ('cache ' )) {
101
- $ cache ->save ($ translation );
102
- }
103
- }
104
-
105
- $ sentence ->translate ($ translation ->get ());
106
-
107
- // verbose mode show everything
108
- if ($ output ->isVeryVerbose ()) {
109
- $ output ->writeln (
110
- [
111
- '------------------------------------------------------------------------- ' ,
112
- ' > ' . $ sentence ->getOriginal (),
113
- ' > ' . $ sentence ->getTranslation (),
114
- ]
115
- );
116
- }
84
+ foreach ($ potrans ->translate ($ from , $ to , $ force ) as $ sentence ) {
85
+ // verbose mode show everything
86
+ if ($ output ->isVeryVerbose ()) {
87
+ $ output ->writeln (
88
+ [
89
+ '------------------------------------------------------------------------- ' ,
90
+ ' > <info> ' . $ sentence ->getOriginal () . '</info> ' ,
91
+ ' > <comment> ' . $ sentence ->getTranslation () . '</comment> ' ,
92
+ ]
93
+ );
117
94
}
118
95
119
96
// progress
120
97
if (!$ output ->isVeryVerbose ()) {
121
98
$ progress ->advance ();
122
99
}
123
100
101
+ $ translated ++;
102
+
124
103
if ($ wait ) usleep ($ wait );
125
104
}
126
105
@@ -132,20 +111,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
132
111
$ output ->writeln ('<comment>Translated :</comment> ' . $ translated . ' sentences ' );
133
112
134
113
// MO file output
135
- $ moGenerator = new MoGenerator ();
136
114
$ moOutputFile = $ outputDir . pathinfo ($ inputFile , PATHINFO_FILENAME ) . '.mo ' ;
137
115
if ($ output ->isVeryVerbose ()) {
138
116
$ output ->writeln ('<comment>Writing new MO File</comment>: ' . $ moOutputFile );
139
117
}
140
- $ moGenerator -> generateFile ( $ translations , $ moOutputFile );
118
+ $ potrans -> saveMoFile ( $ moOutputFile );
141
119
142
120
// PO file output
143
- $ poGenerator = new PoGenerator ();
144
121
$ poOutputFile = $ outputDir . pathinfo ($ inputFile , PATHINFO_FILENAME ) . '.po ' ;
145
122
if ($ output ->isVeryVerbose ()) {
146
123
$ output ->writeln ('<comment>Writing new PO File</comment>: ' . $ poOutputFile );
147
124
}
148
- $ poGenerator -> generateFile ( $ translations , $ poOutputFile );
125
+ $ potrans -> savePoFile ( $ moOutputFile );
149
126
150
127
// done!
151
128
$ output ->writeln ('<info>DONE!</info> ' );
0 commit comments