Skip to content

Commit fc2188a

Browse files
chriysMarc Cámara
authored and
Marc Cámara
committed
Exclude current locale from the language selector (#521)
* allow exclusion of current locale * refactor exclusion of current locale * update README.md * add param in docblock
1 parent 6d64043 commit fc2188a

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ public function getSupportedLocales()
247247

248248
//Should be called like this:
249249
{{ LaravelLocalization::getSupportedLocales() }}
250+
251+
// To exclude current locale from returned array
252+
{{ LaravelLocalization::getSupportedLocales(true) }}
250253
```
251254

252255
This function will return all supported locales and their properties as an array.
@@ -384,6 +387,20 @@ If you're supporting multiple locales in your project you will probably want to
384387
@endforeach
385388
</ul>
386389
```
390+
391+
If you want to exclude current locale from the list, in the language selector.
392+
```
393+
<ul>
394+
@foreach(LaravelLocalization::getSupportedLocales(true) as $localeCode => $properties)
395+
<li>
396+
<a rel="alternate" hreflang="{{ $localeCode }}" href="{{ LaravelLocalization::getLocalizedURL($localeCode, null, [], true) }}">
397+
{{ $properties['native'] }}
398+
</a>
399+
</li>
400+
@endforeach
401+
</ul>
402+
```
403+
387404
Here default language will be forced in getLocalizedURL() to be present in the URL even `hideDefaultLocaleInURL = true`.
388405

389406
## Translated Routes

src/Mcamara/LaravelLocalization/LaravelLocalization.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -368,25 +368,29 @@ public function getDefaultLocale()
368368
/**
369369
* Return an array of all supported Locales.
370370
*
371+
* @param boolean $excludeCurrent
371372
* @throws SupportedLocalesNotDefined
372373
*
373374
* @return array
374375
*/
375-
public function getSupportedLocales()
376+
public function getSupportedLocales($excludeCurrent = false)
376377
{
377-
if (!empty($this->supportedLocales)) {
378-
return $this->supportedLocales;
378+
if (empty($this->supportedLocales)) {
379+
$this->supportedLocales = $this->configRepository->get('laravellocalization.supportedLocales');
379380
}
380381

381-
$locales = $this->configRepository->get('laravellocalization.supportedLocales');
382-
383-
if (empty($locales) || !is_array($locales)) {
382+
if (empty($this->supportedLocales) || !is_array($this->supportedLocales)) {
384383
throw new SupportedLocalesNotDefined();
385384
}
386385

387-
$this->supportedLocales = $locales;
386+
if ($excludeCurrent) {
387+
$locales = $this->supportedLocales;
388+
unset($locales[$this->currentLocale]);
389+
390+
return $locales;
391+
}
388392

389-
return $locales;
393+
return $this->supportedLocales;
390394
}
391395

392396
/**

tests/LocalizerTests.php

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Routing\Route;
4+
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
45

56
class LocalizerTests extends \Orchestra\Testbench\BrowserKit\TestCase
67
{
@@ -457,6 +458,22 @@ public function testGetSupportedLocales()
457458
);
458459
}
459460

461+
public function testGetSupportedLocalesExceptCurrent()
462+
{
463+
$allLocalesCount = count($this->supportedLocales);
464+
$currentLocale = $this->defaultLocale;
465+
466+
$this->assertThat(
467+
LaravelLocalization::getSupportedLocales(true),
468+
$this->logicalNot(
469+
$this->arrayHasKey($currentLocale)
470+
)
471+
);
472+
$this->assertCount($allLocalesCount - 1, LaravelLocalization::getSupportedLocales(true));
473+
474+
$this->assertCount($allLocalesCount, LaravelLocalization::getSupportedLocales(false));
475+
}
476+
460477
public function testGetCurrentLocaleName()
461478
{
462479
$this->assertEquals(

0 commit comments

Comments
 (0)