Skip to content

Commit

Permalink
Fixed loading .env file in DeepLTranslatorCommand
Browse files Browse the repository at this point in the history
Previous implementation expected the .env file in the ./vendor/om/potrans/ directory.
This PR adds a --dir option where the root directory for the .env file and the relative file paths to the input file and output directory can be specified with a fallback to the current working directory.
It also fixes line indention.
  • Loading branch information
aimeos authored Jan 10, 2025
1 parent d67cd97 commit 94382dc
Showing 1 changed file with 36 additions and 27 deletions.
63 changes: 36 additions & 27 deletions src/commands/DeepLTranslatorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected function configure(): void {
->addArgument('output', InputArgument::OPTIONAL, 'Output PO, MO files directory', '~/Downloads')
->addOption('from', null, InputOption::VALUE_REQUIRED, 'Source language (default: en)', 'en')
->addOption('to', null, InputOption::VALUE_REQUIRED, 'Target language (default: cs)', 'cs')
->addOption('dir', null, InputOption::VALUE_REQUIRED, 'Root directory (default: current working directory)')
->addOption('force', null, InputOption::VALUE_NONE, 'Force re-translate including translated sentences')
->addOption('wait', null, InputOption::VALUE_REQUIRED, 'Wait between translations in milliseconds', false)
->addOption('apikey', null, InputOption::VALUE_REQUIRED, 'Deepl API Key')
Expand All @@ -35,46 +36,55 @@ protected function configure(): void {
protected function execute(InputInterface $input, OutputInterface $output): int {
try {

// Load .env file if it exists
if (file_exists(__DIR__ . '/../../.env')) {
$dotenv = Dotenv::createImmutable(__DIR__ . '/../..');
$dotenv->load();
}
$dir = $input->getOption('dir') ?? getcwd();

// Load .env file if it exists
if (file_exists($dir . '/.env')) {
Dotenv::createImmutable($dir)->load();
}

// Input PO file
$inputFile = $input->getArgument('input');
if ($inputFile[0] !== '/') {
$inputFile = $dir . '/' . $inputFile;
}

if (!file_exists($inputFile)) {
throw new RuntimeException(sprintf('Input file "%s" not found', $inputFile));
}

// Output directory
$outputDir = realpath($input->getArgument('output')) . DIRECTORY_SEPARATOR;
$outputDir = $input->getArgument('output');
if ($outputDir[0] !== '/') {
$outputDir = $dir . '/' . $outputDir;
}
$outputDir = realpath($outputDir) . DIRECTORY_SEPARATOR;

if (!is_dir($outputDir)) {
throw new InvalidOptionException('Invalid directory path: ' . $outputDir);
}


// Get API key from .env or command line
$apikey = $_ENV['DEEPL_API_KEY'] ?? $input->getOption('apikey');

if (!$apikey) {
throw new InvalidOptionException('DeepL API Key is required. Set it in .env file or use --apikey option.');
}

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

if (!$translator instanceof \potrans\translator\Translator) {
throw new InvalidOptionException('Invalid translator instance: ' . $customTranslatorPath);
}
} else {
$translator = new DeepLTranslator(
new Translator($apikey),
);
}
$apikey = $_ENV['DEEPL_API_KEY'] ?? $input->getOption('apikey');

if (!$apikey) {
throw new InvalidOptionException('DeepL API Key is required. Set it in .env file or use --apikey option.');
}

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

if (!$translator instanceof \potrans\translator\Translator) {
throw new InvalidOptionException('Invalid translator instance: ' . $customTranslatorPath);
}
} else {
$translator = new DeepLTranslator(
new Translator($apikey),
);
}

// Setup caching
$cache = $input->getOption('cache') ?
Expand Down Expand Up @@ -167,5 +177,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int
public function getDescription(): string {
return 'Translate PO file with DeepL Translator API';
}

}
}

0 comments on commit 94382dc

Please sign in to comment.