Skip to content

Commit f2da881

Browse files
committed
Make sure supported locales are formatted correctly
1 parent fc55056 commit f2da881

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/LocalizedRoutesServiceProvider.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,27 @@ protected function registerLocaleHandler()
149149
$useLocalizer = $app['config']->get('localized-routes.use_localizer', false);
150150
$localizer = $useLocalizer ? $app->make(Localizer::class) : null;
151151

152-
return new LocaleHandler($supportedLocales, $localizer);
152+
return new LocaleHandler($this->formatLocales($supportedLocales), $localizer);
153153
});
154154
}
155+
156+
/**
157+
* Format the locales to pass them to Localizer.
158+
*
159+
* @param array $locales
160+
*
161+
* @return array
162+
*/
163+
protected function formatLocales($locales)
164+
{
165+
$keys = array_keys($locales);
166+
167+
// If the locales are in a key => value format (locale => domain)
168+
// then only return the keys; else return the original array.
169+
if (isset($keys[0]) && is_numeric($keys[0])) {
170+
return $locales;
171+
}
172+
173+
return $keys;
174+
}
155175
}

tests/Unit/Middleware/SetLocaleTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,23 @@ public function it_does_not_set_the_app_locale_for_non_localized_routes_if_local
197197
$response->assertOk();
198198
$this->assertEquals('fr', $response->original);
199199
}
200+
201+
/** @test */
202+
public function it_passes_the_supported_locales_to_localizer_in_the_correct_format()
203+
{
204+
$this->withoutExceptionHandling();
205+
$this->setSupportedLocales(['en' => 'en.domain.com', 'nl' => 'nl.domain.com']);
206+
$this->setUseLocalizer(true);
207+
208+
$localizer = Mockery::spy(Localizer::class);
209+
$localizer->shouldReceive('detect')->andReturn('en');
210+
App::instance(Localizer::class, $localizer);
211+
212+
Route::get('route', function () {})
213+
->middleware(['web', SetLocale::class]);
214+
215+
$this->call('GET', '/route')->assertOk();
216+
217+
$localizer->shouldHaveReceived('setSupportedLocales')->with(['en', 'nl']);
218+
}
200219
}

0 commit comments

Comments
 (0)