Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add untranslated command #5

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ $ php vendor/bin/language-tools sync resources/lang/en/ resources/lang/es/
# Synchronising resources/lang/es/ with resources/lang/en/ ...

```

## Untranslated Command

Find language strings which have not yet been translated.
```bash
$ php language-tools untranslated en/ es/
# Searching for untranslated language strings in es/

# user.php has untranslated language strings ...
"manager" => "Manager",
"avatar" => "Avatar",
"idp" => "IdP",

```
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
],
"require": {
"php": ">=7.2.5",
"symfony/console": "^4.4|^5.0",
"illuminate/support": "^7.30",
"sebastian/diff": "^3.0|^4.0",
"symfony/console": "^4.4|^5.0",
"symfony/finder": "^4.4|^5.0"
},
"autoload": {
Expand All @@ -30,6 +31,9 @@
"php": "7.2.5"
},
"optimize-autoloader": true,
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false
}
}
}
2 changes: 2 additions & 0 deletions language-tools
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ if ((!$loader = includeIfExists(__DIR__.'/vendor/autoload.php')) && (!$loader =

use SupportPal\LanguageTools\Command\CompareCommand;
use SupportPal\LanguageTools\Command\SyncCommand;
use SupportPal\LanguageTools\Command\UntranslatedCommand;
use Symfony\Component\Console\Application;

$console = new Application('SupportPal Language Tools');
$console->add(new CompareCommand);
$console->add(new SyncCommand);
$console->add(new UntranslatedCommand);
$console->run();
64 changes: 64 additions & 0 deletions src/Command/UntranslatedCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php declare(strict_types=1);

namespace SupportPal\LanguageTools\Command;

use SupportPal\LanguageTools\IO\Untranslated\Directory;
use Symfony\Component\Console\Input\InputArgument;

use function count;
use function sprintf;
use function vsprintf;

use const PHP_EOL;

class UntranslatedCommand extends Command
{
/** @var string */
protected static $defaultName = 'untranslated';

/**
* @return void
*/
protected function configure()
{
$this
->setName(self::$defaultName)
->setDefinition([
new InputArgument('dir1', InputArgument::REQUIRED, 'Path to English translation files.'),
new InputArgument('dir2', InputArgument::REQUIRED, 'Path to directory which needs translations updating.'),
])
->setDescription('Find language strings which need translating.')
->setHelp(<<<EOF
The <info>%command.name%</info> finds language strings which need translating:
<info>php %command.full_name% resources/lang/en/ resources/lang/es/</info>
EOF
);
}

public function handle(): int
{
$dir1 = $this->singleArg('dir1');
$dir2 = $this->singleArg('dir2');

$this->info(sprintf('# Searching for untranslated language strings in %s' . PHP_EOL, $dir2));

$instance = new Directory($dir1, $dir2);
$files = $instance->all();
foreach ($instance->all() as $filename => $statistics) {
$this->info(vsprintf('# %d out of %d translations (%d%%) need translating in %s ...', [
$totalUntranslated = $statistics->totalUntranslated(),
$totalStrings = $statistics->totalStrings(),
$totalUntranslated / $totalStrings * 100,
$filename
]));

foreach ($statistics->untranslated() as $string) {
$this->info((string) $string);
}

$this->output->writeln('');
}

return (int) (count($files) > 0);
}
}
33 changes: 33 additions & 0 deletions src/IO/Untranslated/Directory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace SupportPal\LanguageTools\IO\Untranslated;

use SplFileInfo;
use SupportPal\LanguageTools\IO\Directory as IODirectory;

use function count;

class Directory extends IODirectory
{
/**
* @return array<string, FileStatistics>
*/
public function all(): array
{
$results = [];
$this->each(function (SplFileInfo $file, SplFileInfo $otherPath) use (&$results) {
$untranslatedFile = new File($file->getPathname(), $otherPath->getPathname());
$untranslatedStrings = $untranslatedFile->find();
if (count($untranslatedStrings) === 0) {
return null;
}

$results[$file->getFilename()] = new FileStatistics(
$untranslatedFile->totalLanguageStrings(),
$untranslatedStrings
);
});

return $results;
}
}
43 changes: 43 additions & 0 deletions src/IO/Untranslated/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace SupportPal\LanguageTools\IO\Untranslated;

use Illuminate\Support\Arr;
use SupportPal\LanguageTools\IO\File as IOFile;

use function count;

class File extends IOFile
{
public function totalLanguageStrings(): int
{
return count($this->flatten($this->file2));
}

/**
* @return LanguageString[]
*/
public function find(): array
{
$results = [];
$base = $this->flatten($this->file1);
foreach ($this->flatten($this->file2) as $key => $value) {
$baseValue = Arr::get($base, $key);
if ($baseValue !== $value || empty($value)) {
continue;
}

$results[] = new LanguageString($key, $value);
}

return $results;
}

/**
* @return mixed[]
*/
private function flatten(string $path): array
{
return Arr::dot(require $path);
}
}
41 changes: 41 additions & 0 deletions src/IO/Untranslated/FileStatistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types=1);

namespace SupportPal\LanguageTools\IO\Untranslated;

use function count;

class FileStatistics
{
/** @var int */
private $total;

/** @var mixed[] */
private $untranslated;

/**
* @param mixed[] $untranslated
*/
public function __construct(int $total, array $untranslated)
{
$this->total = $total;
$this->untranslated = $untranslated;
}

public function totalStrings(): int
{
return $this->total;
}

public function totalUntranslated(): int
{
return count($this->untranslated);
}

/**
* @return mixed[]
*/
public function untranslated(): array
{
return $this->untranslated;
}
}
27 changes: 27 additions & 0 deletions src/IO/Untranslated/LanguageString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

namespace SupportPal\LanguageTools\IO\Untranslated;

use Stringable;

use function sprintf;

class LanguageString implements Stringable
{
/** @var string */
private $key;

/** @var string */
private $value;

public function __construct(string $key, string $value)
{
$this->key = $key;
$this->value = $value;
}

public function __toString(): string
{
return sprintf('"%s" => "%s",', $this->key, $this->value);
}
}