generated from fidum/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
935 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,7 @@ docs | |
phpunit.xml | ||
phpstan.neon | ||
testbench.yaml | ||
vendor | ||
/vendor | ||
node_modules | ||
da | ||
da.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' => 'ü', | ||
], | ||
], | ||
]; |
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
database/migrations/create_translation_faker_table.php.stub
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(' '); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace Fidum\LaravelTranslationFaker\Contracts\Finders; | ||
|
||
interface LanguageNamespaceFinder extends Finder | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.