Skip to content

Commit

Permalink
Exclude current locale from the language selector (#521)
Browse files Browse the repository at this point in the history
* allow exclusion of current locale

* refactor exclusion of current locale

* update README.md

* add param in docblock
  • Loading branch information
chriys authored and Marc Cámara committed Jan 23, 2018
1 parent 6d64043 commit fc2188a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ public function getSupportedLocales()

//Should be called like this:
{{ LaravelLocalization::getSupportedLocales() }}

// To exclude current locale from returned array
{{ LaravelLocalization::getSupportedLocales(true) }}
```

This function will return all supported locales and their properties as an array.
Expand Down Expand Up @@ -384,6 +387,20 @@ If you're supporting multiple locales in your project you will probably want to
@endforeach
</ul>
```

If you want to exclude current locale from the list, in the language selector.
```
<ul>
@foreach(LaravelLocalization::getSupportedLocales(true) as $localeCode => $properties)
<li>
<a rel="alternate" hreflang="{{ $localeCode }}" href="{{ LaravelLocalization::getLocalizedURL($localeCode, null, [], true) }}">
{{ $properties['native'] }}
</a>
</li>
@endforeach
</ul>
```

Here default language will be forced in getLocalizedURL() to be present in the URL even `hideDefaultLocaleInURL = true`.

## Translated Routes
Expand Down
20 changes: 12 additions & 8 deletions src/Mcamara/LaravelLocalization/LaravelLocalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,25 +368,29 @@ public function getDefaultLocale()
/**
* Return an array of all supported Locales.
*
* @param boolean $excludeCurrent
* @throws SupportedLocalesNotDefined
*
* @return array
*/
public function getSupportedLocales()
public function getSupportedLocales($excludeCurrent = false)
{
if (!empty($this->supportedLocales)) {
return $this->supportedLocales;
if (empty($this->supportedLocales)) {
$this->supportedLocales = $this->configRepository->get('laravellocalization.supportedLocales');
}

$locales = $this->configRepository->get('laravellocalization.supportedLocales');

if (empty($locales) || !is_array($locales)) {
if (empty($this->supportedLocales) || !is_array($this->supportedLocales)) {
throw new SupportedLocalesNotDefined();
}

$this->supportedLocales = $locales;
if ($excludeCurrent) {
$locales = $this->supportedLocales;
unset($locales[$this->currentLocale]);

return $locales;
}

return $locales;
return $this->supportedLocales;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/LocalizerTests.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Routing\Route;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class LocalizerTests extends \Orchestra\Testbench\BrowserKit\TestCase
{
Expand Down Expand Up @@ -457,6 +458,22 @@ public function testGetSupportedLocales()
);
}

public function testGetSupportedLocalesExceptCurrent()
{
$allLocalesCount = count($this->supportedLocales);
$currentLocale = $this->defaultLocale;

$this->assertThat(
LaravelLocalization::getSupportedLocales(true),
$this->logicalNot(
$this->arrayHasKey($currentLocale)
)
);
$this->assertCount($allLocalesCount - 1, LaravelLocalization::getSupportedLocales(true));

$this->assertCount($allLocalesCount, LaravelLocalization::getSupportedLocales(false));
}

public function testGetCurrentLocaleName()
{
$this->assertEquals(
Expand Down

0 comments on commit fc2188a

Please sign in to comment.