Skip to content

Commit

Permalink
[TASK] [issue-3] Now the folder is automatically created
Browse files Browse the repository at this point in the history
 - Removed --output param and replaced with dynamic file path
 - Removed --delimiter for correct character
 - Removed --enclose for correct character
  • Loading branch information
lewisvoncken committed Apr 9, 2018
1 parent 2679adf commit 004216f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 62 deletions.
56 changes: 15 additions & 41 deletions Console/Command/CollectMissingTranslationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,10 @@
class CollectMissingTranslationsCommand extends Command
{
const INPUT_KEY_DIRECTORY = 'directory';
const INPUT_KEY_OUTPUT = 'output';
const SHORTCUT_KEY_OUTPUT = 'o';
const INPUT_KEY_MAGENTO = 'magento';
const SHORTCUT_KEY_MAGENTO = 'm';
const INPUT_KEY_LOCALE = 'locale';
const SHORTCUT_KEY_LOCALE = 'l';
const INPUT_KEY_DELIMITER = 'delimiter';
const SHORTCUT_KEY_DELIMITER = 'd';
const INPUT_KEY_ENCLOSURE = 'enclosure';
const SHORTCUT_KEY_ENCLOSURE = 'e';
const INPUT_KEY_STORE = 'store';
const SHORTCUT_KEY_STORE = 's';

Expand All @@ -58,17 +52,25 @@ class CollectMissingTranslationsCommand extends Command
*/
protected $state;

/**
* @var Experius\MissingTranslations\Helper\Data
*/
protected $helper;

/**
* CollectMissingTranslationsCommand constructor.
* @param Magento\Store\Model\App\Emulation $emulation
* @param Magento\Framework\App\State $state
* @param \Magento\Store\Model\App\Emulation $emulation
* @param \Magento\Framework\App\State $state
* @param \Experius\MissingTranslations\Helper\Data $helper
*/
public function __construct(
\Magento\Store\Model\App\Emulation $emulation,
\Magento\Framework\App\State $state
\Magento\Framework\App\State $state,
\Experius\MissingTranslations\Helper\Data $helper
) {
$this->emulation = $emulation;
$this->state = $state;
$this->helper = $helper;
parent::__construct();
}

Expand All @@ -90,25 +92,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->state->setAreaCode('frontend');
$this->emulation->startEnvironmentEmulation($input->getOption(self::INPUT_KEY_STORE));

$enclosure = '"';
if ($input->getOption(self::INPUT_KEY_ENCLOSURE)) {
$enclosure = $input->getOption(self::INPUT_KEY_ENCLOSURE);
}
$delimiter = ',';
if ($input->getOption(self::INPUT_KEY_DELIMITER)) {
$delimiter = $input->getOption(self::INPUT_KEY_DELIMITER);
}

$fileName = $this->helper->getFileName($input->getOption(self::INPUT_KEY_LOCALE), false);
$generator->generate(
$directory,
$input->getOption(self::INPUT_KEY_OUTPUT),
$fileName,
$input->getOption(self::INPUT_KEY_MAGENTO),
$input->getOption(self::INPUT_KEY_LOCALE),
$delimiter,
$enclosure
$input->getOption(self::INPUT_KEY_LOCALE)
);
$this->emulation->stopEnvironmentEmulation();
$output->writeln('<info>Collected Missing Translations for specified store</info>');
$output->writeln('<info>Collected Missing Translations for specified store and stored in ' . $fileName . ' </info>');
}

/**
Expand All @@ -124,12 +116,6 @@ protected function configure()
InputArgument::OPTIONAL,
'Directory path to parse. Not needed if --magento flag is set'
),
new InputOption(
self::INPUT_KEY_OUTPUT,
self::SHORTCUT_KEY_OUTPUT,
InputOption::VALUE_REQUIRED,
'Path (including filename) to an output file. With no file specified, defaults to stdout.'
),
new InputOption(
self::INPUT_KEY_MAGENTO,
self::SHORTCUT_KEY_MAGENTO,
Expand All @@ -143,18 +129,6 @@ protected function configure()
InputOption::VALUE_REQUIRED,
'Use the --locale parameter to parse specific language.'
),
new InputOption(
self::INPUT_KEY_DELIMITER,
self::SHORTCUT_KEY_DELIMITER,
InputArgument::OPTIONAL,
'Use the --delimiter parameter to change the csv delimiter.'
),
new InputOption(
self::INPUT_KEY_ENCLOSURE,
self::SHORTCUT_KEY_ENCLOSURE,
InputArgument::OPTIONAL,
'Use the --delimiter parameter to change the csv enclosure.'
),
new InputOption(
self::INPUT_KEY_STORE,
self::SHORTCUT_KEY_STORE,
Expand Down
11 changes: 8 additions & 3 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,18 @@ public function removeFromFile($line = false, $locale = 'en_US')
* Get filename of missing translation file based of locale
*
* @param string $locale
* @param bool $requiredExists
* @return bool|string
*/
public function getFileName($locale = 'en_US')
public function getFileName($locale = 'en_US', $requiredExists = true)
{
$vendor = $this->getLanguageVendor();
$filename = $this->directoryList->getRoot() . '/app/i18n/'. $vendor . '/missing/' . $locale . '.csv';
$directoryPath = $this->directoryList->getRoot() . '/app/i18n/'. $vendor . '/missing/';
if (!is_dir($directoryPath)) {
mkdir($directoryPath, 0777, true);
}
$filename = $directoryPath . $locale . '.csv';

return (file_exists($filename)) ? $filename : false;
return (file_exists($filename) || $requiredExists == false) ? $filename : false;
}
}
8 changes: 5 additions & 3 deletions Module/I18n/Dictionary/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ public function __construct(
* @param string $directory
* @param string $outputFilename
* @param bool $withContext
* @throws \UnexpectedValueException
* @param string $locale
* @param string $delimiter
* @param string $enclosure
* @return void
*/
public function generate($directory, $outputFilename, $withContext = false, $locale = 'en_US', $delimiter = ',', $enclosure = '"')
Expand All @@ -103,10 +105,10 @@ public function generate($directory, $outputFilename, $withContext = false, $loc
* @param string $outputFilename
* @return WriterInterface
*/
protected function getDictionaryWriter($outputFilename, $delimiter = ',', $enclusere = '"')
protected function getDictionaryWriter($outputFilename)
{
if (null === $this->writer) {
$this->writer = $this->factory->createDictionaryWriter($outputFilename, $delimiter, $enclusere);
$this->writer = $this->factory->createDictionaryWriter($outputFilename);
}
return $this->writer;
}
Expand Down
11 changes: 2 additions & 9 deletions Module/I18n/Dictionary/Writer/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,19 @@ class Csv implements WriterInterface
*/
protected $_fileHandler;

protected $delimiter;

protected $enclosure;

/**
* Writer construct
*
* @param string $outputFilename
* @throws \InvalidArgumentException
*/
public function __construct($outputFilename, $delimiter = ',', $enclosure = '"')
public function __construct($outputFilename)
{
if (false === ($fileHandler = @fopen($outputFilename, 'w'))) {
throw new \InvalidArgumentException(
sprintf('Cannot open file for write dictionary: "%s"', $outputFilename)
);
}
$this->_fileHandler = $fileHandler;
$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
}

/**
Expand All @@ -59,7 +52,7 @@ public function write(Phrase $phrase)
$fields[] = $contextValue;
}

fputcsv($this->_fileHandler, $fields, $this->delimiter, $this->enclosure);
fputcsv($this->_fileHandler, $fields);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions Module/I18n/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ class Factory
*
* @param string $filename
* @return \Experius\MissingTranslations\Module\I18n\Dictionary\WriterInterface
* @throws \InvalidArgumentException
*/
public function createDictionaryWriter($filename = null, $delimiter = ',', $enclosure = '"')
public function createDictionaryWriter($filename = null)
{
if (!$filename) {
$writer = new Dictionary\Writer\Csv\Stdo();
} else {
switch (pathinfo($filename, \PATHINFO_EXTENSION)) {
case 'csv':
default:
$writer = new Dictionary\Writer\Csv($filename, $delimiter, $enclosure);
$writer = new Dictionary\Writer\Csv($filename);
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ Add a CLI command to Collect missing translations in specified folder or the ent
## CLI

```
php bin/magento experius_missingtranslations:collect [-o|--output="..."] [-m|--magento] [-l|--locale="..."] [-d|--delimiter="..."] [-e|--enclosure="..."] [-s|--store="..."] [directory]
php bin/magento experius_missingtranslations:collect [-m|--magento] [-l|--locale="..."] [-s|--store="..."] [directory]
```

Use the command like this:

```
php bin/magento experius_missingtranslations:collect --output app/i18n/experius/missing/nl_NL.csv --magento --locale nl_NL
php bin/magento experius_missingtranslations:collect --magento --locale nl_NL
```

then edit the file, remove the suffig `missing` and eventually transform it to a language pack by adding a `language.xml` and a `registration.php`
Expand All @@ -44,7 +44,7 @@ Besides transforming the file to a language pack it is possible to add new trans
For example generate missing nl_NL strings:

```
php bin/magento experius_missingtranslations:collect --output app/i18n/experius/missing/nl_NL.csv --magento --locale nl_NL
php bin/magento experius_missingtranslations:collect --magento --locale nl_NL
```

## Translations to database
Expand Down

0 comments on commit 004216f

Please sign in to comment.