Skip to content

Commit

Permalink
add fix-mess command
Browse files Browse the repository at this point in the history
  • Loading branch information
boxblinkracer committed Jan 12, 2024
1 parent ebf5fcf commit 1b3e080
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.

### Added

- Add new **validate:mess** command to find keys without any translation. This means that these translations might not be used at all an can be removed.
- Add new **validate:mess** command to find keys without any translation. This means that these translations might not be used at all and can be removed.
- Add new **fix:mess** command to remove keys without any translation. This means that these translations might not be used at all and can be removed.
- It's now possible to use the **%locale%** placeholder also in the **basePath** attribute of the locales node in the XML configuration.

### Changed
Expand Down
39 changes: 27 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ Now that you know this, let's get started!
* [4.1 Validate All Command](#41-validate-all-command)
* [4.2 Validate Mess Command](#42-validate-mess-command)
* [4.3 Fix Structure Command](#43-fix-structure-command)
* [4.4 Export Command](#44-export-command)
* [4.5 Import Command](#45-import-command)
* [4.6 Status Command](#46-status-command)
* [4.7 Translate Command](#47-translate-command)
* [4.8 List Translations Command](#48-list-translations-command)
* [4.9 Migration Command](#49-migration-command)
* [4.4 Fix Mess Command](#44-fix-mess-command)
* [4.5 Export Command](#45-export-command)
* [4.6 Import Command](#46-import-command)
* [4.7 Status Command](#47-status-command)
* [4.8 Translate Command](#48-translate-command)
* [4.9 List Translations Command](#49-list-translations-command)
* [4.10 Migration Command](#410-migration-command)
* [5. Use Cases](#5-use-cases)
* [5.1 Validation in CI pipeline](#51-validation-in-ci-pipeline)
* [5.2 Working with external translation agencies](#52-working-with-external-translation-agencies)
Expand Down Expand Up @@ -316,7 +317,21 @@ php vendor/bin/phpunuhi fix:structure --set="storefront"
<img src="/.github/assets/fix.png">
</p>

### 4.4 Export Command
### 4.4 Fix Mess Command

This command will automatically remove all translation keys that have no value in any of your locales.
Keys detected by the **validate:mess** command might not be used after all.
So this command will remove them.

```bash
# Fixes all sets of the configuration
php vendor/bin/phpunuhi fix:mess

# Fixes only a provided set of your configuration
php vendor/bin/phpunuhi fix:mess --set="storefront"
```

### 4.5 Export Command

You can export your translations **into a CSV file**, a HTML WebEdit spreadsheet, or other supported exchange formats.
These files can then be passed on to an external translator or company.
Expand Down Expand Up @@ -347,7 +362,7 @@ php vendor/bin/phpunuhi export ... --empty
<img src="/.github/assets/csv.png">
</p>

### 4.5 Import Command
### 4.6 Import Command

You can import your translations **from a CSV file** or other supported exchange formats.
This will automatically update the storage (JSON, ...) that has been assigned to the imported translation set.
Expand All @@ -362,7 +377,7 @@ php vendor/bin/phpunuhi import --set=storefront --file=storefront.csv
php vendor/bin/phpunuhi import ... --format=html
```

### 4.6 Status Command
### 4.7 Status Command

Use this command to get statistics and reports of your translations.
This includes the coverage and the number of found words.
Expand All @@ -375,7 +390,7 @@ php vendor/bin/phpunuhi status
<img src="/.github/assets/status.png">
</p>

### 4.7 Translate Command
### 4.8 Translate Command

PHPUnuhi includes the option to use external services to automatically translate missing values for you.

Expand Down Expand Up @@ -412,7 +427,7 @@ php vendor/bin/phpunuhi translate ... --source=en
<img src="/.github/assets/translate.png">
</p>

### 4.8 List Translations Command
### 4.9 List Translations Command

This command allows you to output all available translation keys in your Translation-Sets.
Use this to debug and analyse your translations.
Expand All @@ -421,7 +436,7 @@ Use this to debug and analyse your translations.
php vendor/bin/phpunuhi list:translations
```

### 4.9 Migration Command
### 4.10 Migration Command

It's also possible to migrate your translations from one storage to another.
Just use the migration command and provide the target storage as output format.
Expand Down
6 changes: 5 additions & 1 deletion src/AppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPUnuhi;

use Exception;
use PHPUnuhi\Commands\Core\FixMessCommand;
use PHPUnuhi\Commands\Core\FixStructureCommand;
use PHPUnuhi\Commands\Core\ListTranslationsCommand;
use PHPUnuhi\Commands\Core\MigrateCommand;
Expand Down Expand Up @@ -32,13 +33,16 @@ public static function run(array $arguments): void
$application->add(new ExportCommand());
$application->add(new ImportCommand());
$application->add(new TranslateCommand());
$application->add(new FixStructureCommand());
$application->add(new ListTranslationsCommand());
$application->add(new MigrateCommand());

$application->add(new ValidateAllCommand());
$application->add(new ValidateMessCommand());

$application->add(new FixStructureCommand());
$application->add(new FixMessCommand());


$application->setDefaultCommand('list');

$application->run();
Expand Down
85 changes: 85 additions & 0 deletions src/Commands/Core/FixMessCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace PHPUnuhi\Commands\Core;

use PHPUnuhi\Bundles\Storage\StorageFactory;
use PHPUnuhi\Configuration\ConfigurationLoader;
use PHPUnuhi\Exceptions\ConfigurationException;
use PHPUnuhi\Services\Loaders\Xml\XmlLoader;
use PHPUnuhi\Traits\CommandTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class FixMessCommand extends Command
{
use CommandTrait;

/**
* @return void
*/
protected function configure()
{
$this
->setName('fix:mess')
->setDescription('Fixes the mess in your translations by removing those unused keys.')
->addOption('configuration', null, InputOption::VALUE_REQUIRED, '', '')
->addOption('set', null, InputOption::VALUE_REQUIRED, '', '');

parent::configure();
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @throws ConfigurationException
* @return int
*/
public function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$io->title('PHPUnuhi Fix Mess');
$this->showHeader();

# -----------------------------------------------------------------

$configFile = $this->getConfigFile($input);
$setName = $this->getConfigStringValue('set', $input);

# -----------------------------------------------------------------

$configLoader = new ConfigurationLoader(new XmlLoader());
$config = $configLoader->load($configFile);


$countRemoved = 0;

foreach ($config->getTranslationSets() as $set) {
if ($setName !== '' && $setName !== '0' && $setName !== $set->getName()) {
continue;
}

$io->section('Fixing Translation Set: ' . $set->getName());

foreach ($set->getInvalidTranslationsIDs() as $currentID) {
foreach ($set->getLocales() as $locale) {
$locale->removeTranslation($currentID);
$io->writeln(' [-] removed translation: [' . $locale->getName() . '] ' . $currentID);
$countRemoved++;
}
}

$io->block('saving translations of this set...');

$storageSaver = StorageFactory::getInstance()->getStorage($set);

$storageSaver->saveTranslationSet($set);
}

$io->success($countRemoved . ' translations have been created!');
return 0;
}
}
3 changes: 3 additions & 0 deletions tests/playground/json/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ validate: ## Validate command
validate-mess: ## Validate mess command
cd ../../.. && php bin/phpunuhi validate:mess --configuration=./tests/playground/json/phpunuhi.xml --report-format=junit --report-output=./tests/playground/json/.exports/junit.xml

fix-mess: ## Fix mess command
cd ../../.. && php bin/phpunuhi fix:mess --configuration=./tests/playground/json/phpunuhi.xml

validate-json: ## Validate command with json report
cd ../../.. && php bin/phpunuhi validate --configuration=./tests/playground/json/phpunuhi.xml --report-format=json --report-output=./tests/playground/json/.exports/report.json

Expand Down

0 comments on commit 1b3e080

Please sign in to comment.