Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dmason30 committed Oct 27, 2023
1 parent 6039937 commit 5aaed4b
Show file tree
Hide file tree
Showing 49 changed files with 935 additions and 126 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ docs
phpunit.xml
phpstan.neon
testbench.yaml
vendor
/vendor
node_modules
da
da.json
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
],
"require": {
"php": "^8.2",
"spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^10.0"
"illuminate/contracts": "^10.0",
"spatie/laravel-package-tools": "^1.14.0"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down Expand Up @@ -81,4 +81,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
40 changes: 39 additions & 1 deletion config/translation-faker.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
<?php

// config for Fidum/LaravelTranslationFaker
return [
/*
|--------------------------------------------------------------------------
| Default Locale
|--------------------------------------------------------------------------
|
| This is the default base locale used to copy the language files from. If
| you change this value make sure to provide a matching "replacers" config
| for that locale below.
|
*/
'default' => 'en',

/*
|--------------------------------------------------------------------------
| Locale Replacers
|--------------------------------------------------------------------------
|
| The following lists the "replacers" that find the keys listed below and
| replaces them with their mapped value. Please avoid using colon ':' in
| this configuration as it may mess with our placeholder detection. Any
| locales you want to use as the base language must be configured here.
|
| Note: This is case sensitive, so please provide replaces for different
| cases as needed.
|
*/
'replacers' => [
'en' => [
'A' => 'Ä',
'a' => 'ä',
'E' => 'Ë',
'e' => 'ë',
'I' => 'Ï',
'i' => 'ï',
'O' => 'Ö',
'o' => 'ö',
'U' => 'Ü',
'u' => 'ü',
],
],
];
19 changes: 0 additions & 19 deletions database/factories/ModelFactory.php

This file was deleted.

19 changes: 0 additions & 19 deletions database/migrations/create_translation_faker_table.php.stub

This file was deleted.

Empty file removed resources/views/.gitkeep
Empty file.
97 changes: 97 additions & 0 deletions src/Commands/FakeTranslationCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Fidum\LaravelTranslationFaker\Commands;

use Fidum\LaravelTranslationFaker\Contracts\Finders\LanguageFileFinder;
use Fidum\LaravelTranslationFaker\Contracts\Printers\LanguageFilePrinter;
use Fidum\LaravelTranslationFaker\Finders\LanguageNamespaceFinder;
use Fidum\LaravelTranslationFaker\Readers\LanguageFileReader;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\SplFileInfo;

class FakeTranslationCommand extends Command
{
public $signature = 'translation:fake
{locale : The output locale to store faked language files.}
{--b|baseLocale= : The base locale to copy language files from.}';

public $description = 'My command';

public function handle(
LanguageFileFinder $finder,
LanguageFilePrinter $printer,
LanguageFileReader $reader,
LanguageNamespaceFinder $namespaceFinder,
Filesystem $filesystem,
): int {
$locale = $this->argument('locale');
$baseLocale = $this->option('baseLocale') ?: config('translation-faker.default');
$replacers = Arr::wrap(config('translation-faker.replacers'));
$namespaces = $namespaceFinder->execute();

if (! array_key_exists($baseLocale, $replacers)) {
$this->components->error("Please add $baseLocale base locale to replacer configuration.");

return self::FAILURE;
}

$replacers = $replacers[$baseLocale];

foreach ($namespaces as $path) {
$files = $finder->execute($path, $baseLocale);

/** @var SplFileInfo $file */
foreach ($files as $file) {
$outputFilename = "$locale.json";

$outputDirectory = Str::of($file->getPath())
->finish(DIRECTORY_SEPARATOR)
->replace(
[DIRECTORY_SEPARATOR.$baseLocale.DIRECTORY_SEPARATOR, "$baseLocale.json"],
[DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR, $outputFilename],
)->before($outputFilename)->toString();

$outputPath = Str::of($file->getPathname())
->replace(
[DIRECTORY_SEPARATOR.$baseLocale.DIRECTORY_SEPARATOR, "$baseLocale.json"],
[DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR, $outputFilename],
)->toString();

$this->components->task(
"Ensuring directory exists $outputDirectory",
fn () => $filesystem->ensureDirectoryExists($outputDirectory),
OutputInterface::VERBOSITY_DEBUG,
);

$translations = $reader
->getTranslations($file)
->dot()
->mapWithKeys(fn ($langLine, $langKey) => [$langKey => $this->convertToUmlaut($replacers, $langLine)])
->undot();

$this->components->task(
"Writing to $outputPath",
fn () => $filesystem->put($outputPath, $printer->execute($file, $translations)),
OutputInterface::VERBOSITY_DEBUG,
);
}
}

$this->components->info("Translations successfully generated from '$baseLocale' to '$locale'.");

return self::SUCCESS;
}

private function convertToUmlaut(array $replacers, string $text): string
{
$str = Str::of($text)->explode(' ')->map(fn ($string) => Str::match('/:\w+/', $string)
? $string
: Str::replace(array_keys($replacers), array_values($replacers), $string));

return $str->implode(' ');
}
}
19 changes: 0 additions & 19 deletions src/Commands/LaravelTranslationFakerCommand.php

This file was deleted.

10 changes: 10 additions & 0 deletions src/Contracts/Finders/Finder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Fidum\LaravelTranslationFaker\Contracts\Finders;

use Illuminate\Support\Collection;

interface Finder
{
public function execute(): Collection;
}
10 changes: 10 additions & 0 deletions src/Contracts/Finders/LanguageFileFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Fidum\LaravelTranslationFaker\Contracts\Finders;

use Illuminate\Support\Collection;

interface LanguageFileFinder
{
public function execute(string $path, string $locale): Collection;
}
7 changes: 7 additions & 0 deletions src/Contracts/Finders/LanguageNamespaceFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Fidum\LaravelTranslationFaker\Contracts\Finders;

interface LanguageNamespaceFinder extends Finder
{
}
11 changes: 11 additions & 0 deletions src/Contracts/Printers/LanguageFilePrinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Fidum\LaravelTranslationFaker\Contracts\Printers;

use Illuminate\Support\Collection;
use Symfony\Component\Finder\SplFileInfo;

interface LanguageFilePrinter
{
public function execute(SplFileInfo $file, Collection $items): string;
}
11 changes: 11 additions & 0 deletions src/Contracts/Readers/LanguageFileReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Fidum\LaravelTranslationFaker\Contracts\Readers;

use Illuminate\Support\Collection;
use Symfony\Component\Finder\SplFileInfo;

interface LanguageFileReader
{
public function getTranslations(SplFileInfo $file): Collection;
}
16 changes: 0 additions & 16 deletions src/Facades/LaravelTranslationFaker.php

This file was deleted.

43 changes: 43 additions & 0 deletions src/Finders/LanguageFileFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Fidum\LaravelTranslationFaker\Finders;

use Fidum\LaravelTranslationFaker\Contracts\Finders\LanguageFileFinder as LanguageFileFinderContract;
use Fidum\LaravelTranslationFaker\Managers\LanguageFileReaderManager;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Symfony\Component\Finder\SplFileInfo;

readonly class LanguageFileFinder implements LanguageFileFinderContract
{
public function __construct(
protected Filesystem $filesystem,
protected LanguageFileReaderManager $manager,
) {
}

public function execute(string $path, string $locale): Collection
{
if ($this->filesystem->exists($path)) {
$files = new Collection($this->filesystem->allFiles($path));
$extensions = $this->manager->getEnabledDrivers();

return $files->filter(function (SplFileInfo $file) use ($extensions, $locale) {
if (in_array($file->getExtension(), $extensions)) {
if ($file->getFilenameWithoutExtension() === $locale) {
return true;
}

return str_contains(
$file->getPathname(),
DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR
);
}

return false;
});
}

return new Collection();
}
}
34 changes: 34 additions & 0 deletions src/Finders/LanguageNamespaceFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Fidum\LaravelTranslationFaker\Finders;

use Fidum\LaravelTranslationFaker\Contracts\Finders\LanguageNamespaceFinder as LanguageNamespaceFinderContract;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Support\Collection;

readonly class LanguageNamespaceFinder implements LanguageNamespaceFinderContract
{
public function __construct(protected Translator $translator)
{
}

public function execute(): Collection
{
$namespacesCollection = new Collection();

// Get Translator namespaces
$loader = $this->translator->getLoader();

foreach ($loader->namespaces() as $hint => $path) {
$namespacesCollection->put($hint, $path);
}

// Add default namespace
$namespacesCollection->put('', app()->langPath());

// Return namespaces collection after removing non existing paths
return $namespacesCollection->filter(function ($path) {
return file_exists($path) ? $path : false;
});
}
}
7 changes: 0 additions & 7 deletions src/LaravelTranslationFaker.php

This file was deleted.

Loading

0 comments on commit 5aaed4b

Please sign in to comment.