Skip to content

Commit 4b97711

Browse files
committed
Added: dotenv support to DeepL
1 parent 8ffccee commit 4b97711

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed

.env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DEEPL_API_KEY=your-deepl-api-key

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.env
12
cache
23
vendor
34
tests/*.mo

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"gettext/gettext": "^5.6",
3232
"google/cloud-translate": "^1.12",
3333
"symfony/cache": "^6.0",
34-
"deeplcom/deepl-php": "^1.2"
34+
"deeplcom/deepl-php": "^1.2",
35+
"vlucas/phpdotenv": "^5.6"
3536
},
3637
"require-dev": {
3738
"nette/tester": "^2.4"

readme.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Description:
2222
Translate PO file with Google Translator API
2323
2424
Usage:
25-
google [options] [--] <input> [<output>]
25+
google [options] [--] <input> [<output>]
2626
2727
Arguments:
2828
input Input PO file path
@@ -141,6 +141,15 @@ For more information visit https://www.deepl.com/pro-api
141141
2. Visit [Account summary](https://www.deepl.com/pro-account/summary)
142142
3. Search for Authentication Key for DeepL API
143143

144+
145+
### Environment variables
146+
147+
You can use environment variables to set DeepL API key.
148+
149+
```dotenv:.env
150+
DEEPL_API_KEY=your-deepl-api-key
151+
```
152+
144153
## Install
145154

146155
```shell

src/commands/DeepLTranslatorCommand.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Input\InputOption;
1717
use Symfony\Component\Console\Output\OutputInterface;
1818
use Throwable;
19+
use Dotenv\Dotenv;
1920

2021
class DeepLTranslatorCommand extends Command {
2122

@@ -34,6 +35,13 @@ protected function configure(): void {
3435
protected function execute(InputInterface $input, OutputInterface $output): int {
3536
try {
3637

38+
// Load .env file if it exists
39+
if (file_exists(__DIR__ . '/../../.env')) {
40+
$dotenv = Dotenv::createImmutable(__DIR__ . '/../..');
41+
$dotenv->load();
42+
}
43+
44+
3745
// Input PO file
3846
$inputFile = $input->getArgument('input');
3947
if (!file_exists($inputFile)) {
@@ -46,20 +54,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4654
throw new InvalidOptionException('Invalid directory path: ' . $outputDir);
4755
}
4856

49-
// Crete new DeepL translator
50-
$customTranslatorPath = $input->getOption('translator');
51-
if ($customTranslatorPath && file_exists($customTranslatorPath)) {
52-
$translator = require_once $customTranslatorPath;
5357

54-
if (!$translator instanceof \potrans\translator\Translator) {
55-
throw new InvalidOptionException('Invalid translator instance: ' . $customTranslatorPath);
56-
}
57-
} else {
58-
$apikey = (string) $input->getOption('apikey');
59-
$translator = new DeepLTranslator(
60-
new Translator($apikey),
61-
);
62-
}
58+
// Get API key from .env or command line
59+
$apikey = $_ENV['DEEPL_API_KEY'] ?? $input->getOption('apikey');
60+
61+
if (!$apikey) {
62+
throw new InvalidOptionException('DeepL API Key is required. Set it in .env file or use --apikey option.');
63+
}
64+
65+
// Crete new DeepL translator
66+
$customTranslatorPath = $input->getOption('translator');
67+
if ($customTranslatorPath && file_exists($customTranslatorPath)) {
68+
$translator = require_once $customTranslatorPath;
69+
70+
if (!$translator instanceof \potrans\translator\Translator) {
71+
throw new InvalidOptionException('Invalid translator instance: ' . $customTranslatorPath);
72+
}
73+
} else {
74+
$translator = new DeepLTranslator(
75+
new Translator($apikey),
76+
);
77+
}
6378

6479
// Setup caching
6580
$cache = $input->getOption('cache') ?

0 commit comments

Comments
 (0)