Skip to content
This repository was archived by the owner on Aug 7, 2022. It is now read-only.
Merged
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
40 changes: 37 additions & 3 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ public function handle()
copy(base_path("vendor/laravel-lang/lang/locales/{$locale}/validation-inline.php"), resource_path("lang/{$locale}/validation.php"));
} else {
copy(base_path("vendor/laravel-lang/lang/locales/{$locale}/validation.php"), resource_path("lang/{$locale}/validation.php"));
$this->mergeAttributes($locale);
}

$discoveredPackages = $this->discoveredPackages();

// Add 'fortify' translations if 'jetstream' is installed
if (in_array('jetstream', $discoveredPackages)) {
array_push($discoveredPackages, 'fortify');
array_push($discoveredPackages, 'fortify', 'jetstream-ext');
}

$this->loadJsonFile($locale, $discoveredPackages);
Expand All @@ -67,8 +68,8 @@ public function handle()

if (!empty($discoveredPackages)) {
$this->info(
'Translations for ['. implode(', ', $discoveredPackages) .'] '
. Str::plural('package', count($discoveredPackages)) .' merged!'
'Translations for [' . implode(', ', $discoveredPackages) . '] '
. Str::plural('package', count($discoveredPackages)) . ' merged!'
);
}
}
Expand Down Expand Up @@ -149,4 +150,37 @@ protected function discoveredPackages(): array
return in_array($package, $packagesToInstall);
}));
}

private function mergeAttributes($locale)
{
$attributesSource = base_path("vendor/laravel-lang/lang/locales/{$locale}/validation-attributes.php");
if (!File::exists($attributesSource)) return;

$attributes = File::get($attributesSource);

$separator = <<<PHP
return [

PHP;
$endOfLine = <<<PHP
];

PHP;
$attributes = Str::replace($endOfLine, "];", $attributes);
$split = explode($separator, $attributes);
$this->replaceInFile("];", $split[1], resource_path("lang/{$locale}/validation.php"));
}

/**
* Replace a given string within a given file.
*
* @param string $search
* @param string $replace
* @param string $path
* @return void
*/
protected function replaceInFile($search, $replace, $path)
{
file_put_contents($path, str_replace($search, $replace, file_get_contents($path)));
}
}
92 changes: 92 additions & 0 deletions tests/Feature/CopyPhpFilesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Luisprmat\LaravelLangInstaller\Tests\Feature;

use Illuminate\Support\Facades\File;
use Luisprmat\LaravelLangInstaller\Tests\TestCase;

class CopyPhpFilesTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
parent::setUp();
$this->app->setBasePath(__DIR__ . '/../fixtures');

File::ensureDirectoryExists(config_path());
File::copy(__DIR__ . '/../stubs/config/app.php', config_path('app.php'));
File::put(base_path('composer.json'), $this->buildComposerWithDependencies(['"any/package": "^1.0"']));
}

protected function tearDown(): void
{
parent::tearDown();
File::deleteDirectory(config_path());
File::deleteDirectory(resource_path());
File::delete(base_path('composer.json'));
}

/** @test */
function merge_attributes_in_validation()
{
$this->artisan('lang:add');

$this->assertTrue(File::exists(resource_path('lang/es/validation.php')));

$expected = <<<PHP
<?php

return [
'before_or_equal' => ':attribute debe ser una fecha anterior o igual a :date.',
'between' => [
'array' => ':attribute tiene que tener entre :min - :max elementos.',
'file' => ':attribute debe pesar entre :min - :max kilobytes.',
'numeric' => ':attribute tiene que estar entre :min - :max.',
'string' => ':attribute tiene que tener entre :min - :max caracteres.',
],
'boolean' => 'El campo :attribute debe tener un valor verdadero o falso.',
'attributes' => [
'address' => 'dirección',
'hour' => 'hora',
],
];

PHP;
$this->assertEquals(
$expected,
File::get(resource_path('lang/es/validation.php'))
);
}

/** @test */
function merge_attributes_in_validation_other_language()
{
$this->artisan('lang:add xx_GB');

$this->assertTrue(File::exists(resource_path('lang/xx_GB/validation.php')));

$expected = <<<PHP
<?php

return [
'before_or_equal' => 'The :attribute must be a date before or equal to :date.',
'between' => [
'numeric' => 'The :attribute must be between :min and :max.',
'file' => 'The :attribute must be between :min and :max kilobytes.',
'string' => 'The :attribute must be between :min and :max characters.',
'array' => 'The :attribute must have between :min and :max items.',
],
'boolean' => 'The :attribute field must be true or false.',
'attributes' => [
'address' => 'address',
'hour' => 'hour',
],
];

PHP;
$this->assertEquals(
$expected,
File::get(resource_path('lang/xx_GB/validation.php'))
);
}
}