Skip to content

Commit

Permalink
translate command
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Jun 18, 2024
1 parent 05763dc commit 7343aa4
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
56 changes: 56 additions & 0 deletions src/Commands/TranslateTranslationsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Elegantly\Translator\Commands;

use Elegantly\Translator\Facades\Translator;
use Illuminate\Console\Command;

class TranslateTranslationsCommand extends Command
{
public $signature = 'translator:translate {--from=} {--to=} {--all}';

public $description = 'Translate translations from the given locale to the target one.';

public function handle(): int
{
$from = (string) $this->argument('from');
$to = $this->argument('to');
$all = (bool) $this->argument('all');

$namespaces = Translator::getNamespaces($from);

$targets = collect($to ? [(string) $to] : Translator::getLanguages())->filter(fn ($locale) => $locale !== $from);

foreach ($targets as $target) {

$this->info("Translating from {$from} to {$target}:");

foreach ($namespaces as $namespace) {

if ($all) {
$keys = Translator::getTranslations($from, $namespace)->dot()->keys()->toArray();
} else {
$keys = Translator::getMissingTranslations($from, $target, $namespace);
}

if (! $this->confirm(count($keys)." found for {$target}.{$namespace}, would you like to continue?")) {
continue;
}

$translations = Translator::translateTranslations(
$from,
$target,
$namespace,
$keys
);

$this->table(
headers: ['key', 'Translation'],
rows: $translations->dot()->map(fn ($value, $key) => ["{$namespace}.{$key}", $value])
);
}
}

return self::SUCCESS;
}
}
9 changes: 6 additions & 3 deletions src/Facades/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace Elegantly\Translator\Facades;

use Elegantly\Translator\Translations;
use Illuminate\Support\Facades\Facade;

/**
* @method static array getLanguages()
* @method static array getNamespaces(string $locale)
* @method static array<int, string> getLanguages()
* @method static array<int, string> getNamespaces(string $locale)
* @method static Translations getTranslations(string $locale, string $namespace)
* @method static Translations sortTranslations(string $locale, string $namespace)
* @method static array getMissingTranslations(string $referenceLocale, string $targetLocale, string $namespace)
* @method static Translations translateTranslations(string $referenceLocale, string $targetLocale, string $namespace, array $keys)
* @method static array<int, string> getMissingTranslations(string $referenceLocale, string $targetLocale, string $namespace)
* @method static array getAllMissingTranslations(string $referenceLocale)
* @method static void sortAllTranslations()
*
Expand Down
16 changes: 15 additions & 1 deletion src/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* @extends Collection<string|int, array|string|int|float|null>
*/
class Translations extends Collection
final class Translations extends Collection
{
//

Expand Down Expand Up @@ -91,4 +91,18 @@ public function getMissingTranslationsIn(Translations $translations): array
->keys()
->toArray();
}

public function only($keys): static
{
return new static(
$this->toBase()->dot()->only($keys)->undot()
);
}

public function except($keys): static
{
return new static(
$this->toBase()->dot()->except($keys)->undot()
);
}
}
4 changes: 2 additions & 2 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function (Translations $translations) use ($values) {

return $translations;
}
);
)->only(array_keys($values));
}

public function setTranslation(
Expand Down Expand Up @@ -139,7 +139,7 @@ function (Translations $translations) use ($referenceLocale, $targetLocale, $nam

return $translations;
}
);
)->only($keys);
}

public function translateTranslation(
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/TranslationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,29 @@
'f.a',
]);
});

it('filters (nested) translations using only', function () {
$translations = new Translations([
'a' => 'text',
'b' => 'text',
'c' => [
'a' => 'text',
'b' => 'text',
],
'd' => 'text',
'e' => 'text',
'f' => [
'a' => 'text',
],
]);

expect(
$translations->only(['a', 'c.a', 'd'])->toArray()
)->toBe([
'a' => 'text',
'c' => [
'a' => 'text',
],
'd' => 'text',
]);
});

0 comments on commit 7343aa4

Please sign in to comment.