Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting of the default locale #830

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/Mcamara/LaravelLocalization/LaravelLocalization.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,7 @@ public function __construct()
$this->url = $this->app['url'];

// set default locale
$this->defaultLocale = $this->configRepository->get('app.locale');
$supportedLocales = $this->getSupportedLocales();

if (empty($supportedLocales[$this->defaultLocale])) {
throw new UnsupportedLocaleException('Laravel default locale is not in the supportedLocales array.');
}
$this->setDefaultLocale($this->configRepository->get('app.locale'));
}

/**
Expand Down Expand Up @@ -414,6 +409,22 @@ public function getDefaultLocale()
return $this->defaultLocale;
}

/**
* Sets the default locale.
*
* @param $defaultLocale
* @throws UnsupportedLocaleException
* @throws SupportedLocalesNotDefined
*/
public function setDefaultLocale($defaultLocale)
{
$supportedLocales = $this->getSupportedLocales();
if (empty($supportedLocales[$defaultLocale])) {
throw new UnsupportedLocaleException('Laravel default locale is not in the supportedLocales array.');
}
$this->defaultLocale = $defaultLocale;
}

/**
* Return locales mapping.
*
Expand Down
42 changes: 42 additions & 0 deletions tests/LocalizerTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -880,4 +880,46 @@ public function testSetLocaleWithMapping()
$this->assertEquals('http://localhost/custom/some-route', app('laravellocalization')->localizeURL('some-route', 'custom'));
$this->assertEquals('http://localhost/custom', app('laravellocalization')->localizeURL('http://localhost/custom', 'en'));
}

public function testSetDefaultLocale()
{
$this->assertEquals(
app('laravellocalization')->getDefaultLocale(), $this->defaultLocale
);

app('laravellocalization')->setDefaultLocale('es');

$this->assertEquals(
app('laravellocalization')->getDefaultLocale(), 'es'
);
}

public function testSetInvalidDefaultLocale()
{
$this->expectException(\Mcamara\LaravelLocalization\Exceptions\UnsupportedLocaleException::class);
app('laravellocalization')->setDefaultLocale('sv');
}

public function testSetDefaultLocaleHiddenInUrl()
{
app('config')->set('laravellocalization.hideDefaultLocaleInURL', true);

$this->assertEquals(
$this->test_url,
app('laravellocalization')->localizeURL('', $this->defaultLocale)
);

app('laravellocalization')->setDefaultLocale('es');

$this->assertNotEquals(
$this->test_url,
app('laravellocalization')->localizeURL('', $this->defaultLocale)
);

$this->assertEquals(
$this->test_url,
app('laravellocalization')->localizeURL('', 'es')
);

}
}