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

Commit b6b9b86

Browse files
committed
Optimize [locale].json with only required translations
1 parent 7a62f69 commit b6b9b86

File tree

3 files changed

+102
-15
lines changed

3 files changed

+102
-15
lines changed

src/Console/InstallCommand.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ public function handle()
4949
}
5050

5151
$discoveredPackages = $this->discoveredPackages();
52+
53+
// Add 'fortify' translations if 'jetstream' is installed
54+
if (in_array('jetstream', $discoveredPackages)) {
55+
array_push($discoveredPackages, 'fortify');
56+
}
57+
5258
$this->loadJsonFile($locale, $discoveredPackages);
5359

5460
if (!$this->option('no-default')) {
@@ -85,24 +91,24 @@ private function loadJsonFile($locale, $packages = [])
8591
$baseSource = json_decode(File::get(base_path('vendor/laravel-lang/lang/source/en.json')));
8692
$jsonLocale = json_decode(File::get(base_path("vendor/laravel-lang/lang/locales/{$locale}/{$locale}.json")), true);
8793

88-
$modify = array_filter($jsonLocale, function ($item) use ($baseSource) {
89-
return in_array($item, $baseSource);
94+
$showTags = $baseSource;
95+
96+
foreach ($packages as $package) {
97+
$showTags = array_merge(
98+
$showTags,
99+
json_decode(File::get(base_path("vendor/laravel-lang/lang/source/packages/{$package}.json")))
100+
);
101+
}
102+
103+
$showTags = array_unique($showTags);
104+
sort($showTags);
105+
106+
$modify = array_filter($jsonLocale, function ($item) use ($showTags) {
107+
return in_array($item, $showTags);
90108
}, ARRAY_FILTER_USE_KEY);
91109

92110
$modifiedJson = json_encode($modify, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
93111

94-
// dd($baseSource, $jsonLocale, $modifiedJson);
95-
// $modifiedJson = <<<JSON
96-
//{
97-
// "Add Base": "Añadir base",
98-
// "Changes Base": "Cambios base",
99-
// "If you already have an account, you may accept this invitation by clicking the button below: base": "Si ya tiene una cuenta, puede aceptar esta invitación haciendo clic en el botón de abajo: base",
100-
// "Whoops! all": "¡Ups! todo"
101-
//}
102-
//
103-
//JSON;
104-
105-
// copy(base_path("vendor/laravel-lang/lang/locales/{$locale}/{$locale}.json"), resource_path("lang/{$locale}.json"));
106112
File::put(resource_path("lang/{$locale}.json"), $modifiedJson);
107113
}
108114

tests/Feature/InstallLanguageTest.php

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function it_doesnt_modify_config_app_locale_if_pass_no_default_option()
112112
}
113113

114114
/** @test */
115-
function it_install_json_locale_only_with_base_translations()
115+
function it_installs_json_locale_only_with_base_translations()
116116
{
117117
$this->artisan('lang:add es');
118118
$expected = <<<JSON
@@ -142,4 +142,84 @@ function it_install_json_locale_only_with_base_translations()
142142
File::get(resource_path('lang/xx_GB.json'))
143143
);
144144
}
145+
146+
/** @test */
147+
function it_installs_json_with_base_plus_discovered_packages_one()
148+
{
149+
File::put(base_path('composer.json'), $this->buildComposerWithDependencies(
150+
['"laravel/cashier": "^13.5"']
151+
));
152+
153+
$this->artisan('lang:add es');
154+
$expected = <<<JSON
155+
{
156+
"Add Base": "Añadir base",
157+
"Cancel both": "Cancelar ambos",
158+
"Card cashier": "Tarjeta cashier",
159+
"Changes Base": "Cambios base",
160+
"Confirm your :amount payment cashier": "Confirme su pago de :amount cashier",
161+
"If you already have an account, you may accept this invitation by clicking the button below: base": "Si ya tiene una cuenta, puede aceptar esta invitación haciendo clic en el botón de abajo: base",
162+
"Whoops! all": "¡Ups! todo"
163+
}
164+
JSON;
165+
$this->assertEquals(
166+
$expected,
167+
File::get(resource_path('lang/es.json'))
168+
);
169+
170+
}
171+
172+
/** @test */
173+
function it_installs_json_with_base_plus_discovered_packages_other()
174+
{
175+
File::put(base_path('composer.json'), $this->buildComposerWithDependencies(
176+
['"laravel/breeze": "^1.4"']
177+
));
178+
179+
$this->artisan('lang:add es');
180+
$expected = <<<JSON
181+
{
182+
"Add Base": "Añadir base",
183+
"Cancel both": "Cancelar ambos",
184+
"Changes Base": "Cambios base",
185+
"Confirm Password breeze": "Confirmar contraseña breeze",
186+
"If you already have an account, you may accept this invitation by clicking the button below: base": "Si ya tiene una cuenta, puede aceptar esta invitación haciendo clic en el botón de abajo: base",
187+
"Log in breeze": "Iniciar sesión breeze",
188+
"Whoops! all": "¡Ups! todo"
189+
}
190+
JSON;
191+
$this->assertEquals(
192+
$expected,
193+
File::get(resource_path('lang/es.json'))
194+
);
195+
196+
}
197+
198+
/** @test */
199+
function it_installs_json_with_base_plus_discovered_packages_mixed()
200+
{
201+
File::put(base_path('composer.json'), $this->buildComposerWithDependencies(
202+
['"laravel/cashier": "^13.5"', '"laravel/breeze": "^1.4"']
203+
));
204+
205+
$this->artisan('lang:add es');
206+
$expected = <<<JSON
207+
{
208+
"Add Base": "Añadir base",
209+
"Cancel both": "Cancelar ambos",
210+
"Card cashier": "Tarjeta cashier",
211+
"Changes Base": "Cambios base",
212+
"Confirm Password breeze": "Confirmar contraseña breeze",
213+
"Confirm your :amount payment cashier": "Confirme su pago de :amount cashier",
214+
"If you already have an account, you may accept this invitation by clicking the button below: base": "Si ya tiene una cuenta, puede aceptar esta invitación haciendo clic en el botón de abajo: base",
215+
"Log in breeze": "Iniciar sesión breeze",
216+
"Whoops! all": "¡Ups! todo"
217+
}
218+
JSON;
219+
$this->assertEquals(
220+
$expected,
221+
File::get(resource_path('lang/es.json'))
222+
);
223+
224+
}
145225
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[
22
"Cancel both",
3+
"Card cashier",
34
"Confirm your :amount payment cashier",
45
"Whoops! all"
56
]

0 commit comments

Comments
 (0)