Skip to content

Commit

Permalink
Ft: convert locale to application specific locales
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCavens committed Apr 24, 2018
1 parent 6f986ea commit 824acf1
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 22 deletions.
26 changes: 25 additions & 1 deletion src/Detect.php
Expand Up @@ -67,7 +67,7 @@ public function detectLocale(): self

$this->locale = $locale;

app()->setLocale($locale->get());
$this->setApplicationLocale();

return $this;
}
Expand Down Expand Up @@ -111,4 +111,28 @@ private function detectScope()
{
$this->scope = ScopeCollection::fromConfig($this->config)->findByRoot($this->request->root());
}

private function setApplicationLocale()
{
$locale = $this->locale;

$conversions = $this->config->get('convert_locales_to', []);

if('auto' === $this->config->get('convert_locales'))
{
if(isset($conversions[$locale->get()])){
$locale = Locale::from($conversions[$locale->get()]);
}else{
$locale = $locale->withoutRegion();
}
}
else if(true === $this->config->get('convert_locales'))
{
if(isset($conversions[$locale->get()])){
$locale = Locale::from($conversions[$locale->get()]);
}
}

app()->setLocale($locale->get());
}
}
11 changes: 11 additions & 0 deletions src/Values/Locale.php
Expand Up @@ -19,6 +19,17 @@ public static function from(string $value)
return new static($value);
}

public function withoutRegion()
{
if(false !== strpos($this->value, '-')){
$value = substr($this->value,0,strpos($this->value, '-'));
}else if(false !== strpos($this->value, '_')){
$value = substr($this->value,0,strpos($this->value, '_'));
}

return new static($value);
}

public function get(): string
{
return $this->value;
Expand Down
39 changes: 18 additions & 21 deletions src/config/locale.php
Expand Up @@ -12,29 +12,26 @@
'*' => 'en',
],

// 'locales' => [
// 'fr.example.com' => 'fr',
// 'example.com' => [
// '/fr' => 'fr',
// '/' => 'en',
// ],
// 'mijnkindishetallerliefste.be' => 'nl',
// 'monenfantestleplusadorable.be' => [
// '/nl' => 'nl',
// '/' => 'fr-BE',
// ],
// 'monenfantestleplusadorable.fr' => 'fr',
//
// '*' => [
// 'en' => 'en-GB',
// 'us' => 'en-US',
// 'fr' => 'fr',
// '/' => 'nl',
// ],
// ],
/**
* In case your locales contains both language as region references and this locale differs
* from the application's locale, you can opt to point this locale to the one as expected
* by the application.
*
* The value can be either: auto, true or false.
* - false default value. Keeps the locale values as set as above.
* - true locales will be converted to the ones defined below in the 'convert_locales_to'
* - auto if the format is in the RFC 4646 format with hyphen (en-US) or underscore (en_US), we will automatically convert the locale
* to the language portion. e.g. en-US
*/
'convert_locales' => false,

'convert_locales_to' => [
// e.g. 'nl-BE' => 'nl',
],

/*
* Define your canonical domains here.
* Define specific canonical domains here.
*
* Specify unique domains for each locale to avoid crawler duplicate content warnings.
* By default the current scope is used to determine the canonical domain. Here
* you can specify a domain for a certain locale if the same locale is present in multiple domains.
Expand Down
63 changes: 63 additions & 0 deletions tests/ConvertLocale/ConvertLocaleTest.php
@@ -0,0 +1,63 @@
<?php

namespace Thinktomorrow\Locale\Tests\ConverLocale;

use Thinktomorrow\Locale\Detect;
use Thinktomorrow\Locale\Tests\TestCase;

class ConvertLocaleTest extends TestCase
{
/** @test */
public function it_can_convert_locale_to_application_one()
{
$this->detectLocaleAfterVisiting('http://convert.example.com/', [
'locales' => [
'convert.example.com' => [
'/' => 'locale-ten',
],
],
'convert_locales' => true,
'convert_locales_to' => [
'locale-ten' => 'converted-ten'
],
]);

$this->assertEquals('locale-ten', app(Detect::class)->getLocale()->get());
$this->assertEquals('converted-ten', app()->getLocale());
}

/** @test */
function it_can_automatically_convert_locale_to_application_one()
{
$this->detectLocaleAfterVisiting('http://convert.example.com/', [
'locales' => [
'convert.example.com' => [
'/' => 'locale-ten',
],
],
'convert_locales' => 'auto',
]);

$this->assertEquals('locale-ten', app(Detect::class)->getLocale()->get());
$this->assertEquals('locale', app()->getLocale());
}

/** @test */
public function explicit_conversion_has_priority_over_automatic()
{
$this->detectLocaleAfterVisiting('http://convert.example.com/', [
'locales' => [
'convert.example.com' => [
'/' => 'locale-ten',
],
],
'convert_locales' => 'auto',
'convert_locales_to' => [
'locale-ten' => 'converted-ten'
],
]);

$this->assertEquals('locale-ten', app(Detect::class)->getLocale()->get());
$this->assertEquals('converted-ten', app()->getLocale());
}
}
8 changes: 8 additions & 0 deletions tests/Values/LocaleTest.php
Expand Up @@ -19,6 +19,14 @@ public function it_can_compare()
$this->assertTrue(Locale::from('nl')->equals(Locale::from('nl')));
$this->assertFalse(Locale::from('nl')->equals(Locale::from('fr')));
}

/** @test */
function it_can_auto_convert_to_language_locale_without_region()
{
$this->assertEquals(Locale::from('nl'), Locale::from('nl-BE')->withoutRegion());
$this->assertEquals(Locale::from('nl'), Locale::from('nl_BE')->withoutRegion());
$this->assertEquals(Locale::from('NL'), Locale::from('NL-NL')->withoutRegion());
}

/** @test */
public function it_prints_out_as_string()
Expand Down

0 comments on commit 824acf1

Please sign in to comment.