Skip to content
This repository was archived by the owner on Aug 7, 2022. It is now read-only.

Commit 915c7e6

Browse files
authored
Merge pull request #4 from luisprmat/main
Added support for jetstream-ext (PR #1859 laravel-lang/lang) and fix for merge attributes in validation
2 parents 42c2288 + 28c02a8 commit 915c7e6

File tree

2 files changed

+129
-3
lines changed

2 files changed

+129
-3
lines changed

src/Console/InstallCommand.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ public function handle()
4646
copy(base_path("vendor/laravel-lang/lang/locales/{$locale}/validation-inline.php"), resource_path("lang/{$locale}/validation.php"));
4747
} else {
4848
copy(base_path("vendor/laravel-lang/lang/locales/{$locale}/validation.php"), resource_path("lang/{$locale}/validation.php"));
49+
$this->mergeAttributes($locale);
4950
}
5051

5152
$discoveredPackages = $this->discoveredPackages();
5253

5354
// Add 'fortify' translations if 'jetstream' is installed
5455
if (in_array('jetstream', $discoveredPackages)) {
55-
array_push($discoveredPackages, 'fortify');
56+
array_push($discoveredPackages, 'fortify', 'jetstream-ext');
5657
}
5758

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

6869
if (!empty($discoveredPackages)) {
6970
$this->info(
70-
'Translations for ['. implode(', ', $discoveredPackages) .'] '
71-
. Str::plural('package', count($discoveredPackages)) .' merged!'
71+
'Translations for [' . implode(', ', $discoveredPackages) . '] '
72+
. Str::plural('package', count($discoveredPackages)) . ' merged!'
7273
);
7374
}
7475
}
@@ -149,4 +150,37 @@ protected function discoveredPackages(): array
149150
return in_array($package, $packagesToInstall);
150151
}));
151152
}
153+
154+
private function mergeAttributes($locale)
155+
{
156+
$attributesSource = base_path("vendor/laravel-lang/lang/locales/{$locale}/validation-attributes.php");
157+
if (!File::exists($attributesSource)) return;
158+
159+
$attributes = File::get($attributesSource);
160+
161+
$separator = <<<PHP
162+
return [
163+
164+
PHP;
165+
$endOfLine = <<<PHP
166+
];
167+
168+
PHP;
169+
$attributes = Str::replace($endOfLine, "];", $attributes);
170+
$split = explode($separator, $attributes);
171+
$this->replaceInFile("];", $split[1], resource_path("lang/{$locale}/validation.php"));
172+
}
173+
174+
/**
175+
* Replace a given string within a given file.
176+
*
177+
* @param string $search
178+
* @param string $replace
179+
* @param string $path
180+
* @return void
181+
*/
182+
protected function replaceInFile($search, $replace, $path)
183+
{
184+
file_put_contents($path, str_replace($search, $replace, file_get_contents($path)));
185+
}
152186
}

tests/Feature/CopyPhpFilesTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Luisprmat\LaravelLangInstaller\Tests\Feature;
4+
5+
use Illuminate\Support\Facades\File;
6+
use Luisprmat\LaravelLangInstaller\Tests\TestCase;
7+
8+
class CopyPhpFilesTest extends TestCase
9+
{
10+
protected function setUp(): void
11+
{
12+
parent::setUp();
13+
parent::setUp();
14+
$this->app->setBasePath(__DIR__ . '/../fixtures');
15+
16+
File::ensureDirectoryExists(config_path());
17+
File::copy(__DIR__ . '/../stubs/config/app.php', config_path('app.php'));
18+
File::put(base_path('composer.json'), $this->buildComposerWithDependencies(['"any/package": "^1.0"']));
19+
}
20+
21+
protected function tearDown(): void
22+
{
23+
parent::tearDown();
24+
File::deleteDirectory(config_path());
25+
File::deleteDirectory(resource_path());
26+
File::delete(base_path('composer.json'));
27+
}
28+
29+
/** @test */
30+
function merge_attributes_in_validation()
31+
{
32+
$this->artisan('lang:add');
33+
34+
$this->assertTrue(File::exists(resource_path('lang/es/validation.php')));
35+
36+
$expected = <<<PHP
37+
<?php
38+
39+
return [
40+
'before_or_equal' => ':attribute debe ser una fecha anterior o igual a :date.',
41+
'between' => [
42+
'array' => ':attribute tiene que tener entre :min - :max elementos.',
43+
'file' => ':attribute debe pesar entre :min - :max kilobytes.',
44+
'numeric' => ':attribute tiene que estar entre :min - :max.',
45+
'string' => ':attribute tiene que tener entre :min - :max caracteres.',
46+
],
47+
'boolean' => 'El campo :attribute debe tener un valor verdadero o falso.',
48+
'attributes' => [
49+
'address' => 'dirección',
50+
'hour' => 'hora',
51+
],
52+
];
53+
54+
PHP;
55+
$this->assertEquals(
56+
$expected,
57+
File::get(resource_path('lang/es/validation.php'))
58+
);
59+
}
60+
61+
/** @test */
62+
function merge_attributes_in_validation_other_language()
63+
{
64+
$this->artisan('lang:add xx_GB');
65+
66+
$this->assertTrue(File::exists(resource_path('lang/xx_GB/validation.php')));
67+
68+
$expected = <<<PHP
69+
<?php
70+
71+
return [
72+
'before_or_equal' => 'The :attribute must be a date before or equal to :date.',
73+
'between' => [
74+
'numeric' => 'The :attribute must be between :min and :max.',
75+
'file' => 'The :attribute must be between :min and :max kilobytes.',
76+
'string' => 'The :attribute must be between :min and :max characters.',
77+
'array' => 'The :attribute must have between :min and :max items.',
78+
],
79+
'boolean' => 'The :attribute field must be true or false.',
80+
'attributes' => [
81+
'address' => 'address',
82+
'hour' => 'hour',
83+
],
84+
];
85+
86+
PHP;
87+
$this->assertEquals(
88+
$expected,
89+
File::get(resource_path('lang/xx_GB/validation.php'))
90+
);
91+
}
92+
}

0 commit comments

Comments
 (0)