-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de828f6
commit 824be87
Showing
8 changed files
with
359 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace CodeZero\UriTranslator\Macros\Lang; | ||
|
||
use CodeZero\UriTranslator\UriTranslator; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\Facades\Lang; | ||
|
||
class UriMacro | ||
{ | ||
/** | ||
* Register the macro. | ||
* | ||
* @return void | ||
*/ | ||
public static function register() | ||
{ | ||
Lang::macro('uri', function ($uri, $locale = null, $namespace = null) { | ||
return App::make(UriTranslator::class)->translate($uri, $locale, $namespace); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
namespace CodeZero\UriTranslator; | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Lang; | ||
use Illuminate\Support\Str; | ||
|
||
class UriTranslator | ||
{ | ||
/** | ||
* Translate a URI. | ||
* | ||
* @param string $uri | ||
* @param string|null $locale | ||
* @param string|null $namespace | ||
* | ||
* @return string | ||
*/ | ||
public function translate($uri, $locale = null, $namespace = null) | ||
{ | ||
$fullUriKey = $this->buildTranslationKey($uri, $namespace); | ||
|
||
// Attempt to translate the full URI. | ||
if (Lang::has($fullUriKey, $locale)) { | ||
return Lang::get($fullUriKey, [], $locale); | ||
} | ||
|
||
$segments = $this->splitUriIntoSegments($uri); | ||
|
||
// Attempt to translate each segment individually. If there is no translation | ||
// for a specific segment, then its original value will be used. | ||
$translations = $segments->map(function ($segment) use ($locale, $namespace) { | ||
$segmentKey = $this->buildTranslationKey($segment, $namespace); | ||
|
||
// If the segment is not a placeholder and the segment | ||
// has a translation, then update the segment. | ||
if ( ! Str::startsWith($segment, '{') && Lang::has($segmentKey, $locale)) { | ||
$segment = Lang::get($segmentKey, [], $locale); | ||
} | ||
|
||
return $segment; | ||
}); | ||
|
||
// Rebuild the URI from the translated segments. | ||
return $translations->implode('/'); | ||
} | ||
|
||
/** | ||
* Split the URI into a Collection of segments. | ||
* | ||
* @param string $uri | ||
* | ||
* @return \Illuminate\Support\Collection | ||
*/ | ||
protected function splitUriIntoSegments($uri) | ||
{ | ||
$uri = trim($uri, '/'); | ||
$segments = explode('/', $uri); | ||
|
||
return Collection::make($segments); | ||
} | ||
|
||
/** | ||
* Build a translation key, including the namespace and file name. | ||
* | ||
* @param string $key | ||
* @param string|null $namespace | ||
* | ||
* @return string | ||
*/ | ||
protected function buildTranslationKey($key, $namespace) | ||
{ | ||
$namespace = $namespace ? "{$namespace}::" : ''; | ||
$file = $this->getTranslationFileName(); | ||
|
||
return "{$namespace}{$file}.{$key}"; | ||
} | ||
|
||
/** | ||
* Get the file name that holds the URI translations. | ||
* | ||
* @return string | ||
*/ | ||
protected function getTranslationFileName() | ||
{ | ||
return 'routes'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
namespace CodeZero\UriTranslator\Tests\Feature; | ||
|
||
use CodeZero\UriTranslator\Tests\TestCase; | ||
use Illuminate\Support\Facades\Lang; | ||
|
||
class UriTranslatorTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_translates_every_segment_in_a_uri_to_the_current_locale() | ||
{ | ||
$this->setTranslations([ | ||
'nl' => [ | ||
'my' => 'mijn', | ||
'new' => 'nieuwe', | ||
'page' => 'pagina', | ||
] | ||
]); | ||
|
||
$this->setAppLocale('en'); | ||
$this->assertEquals('my/new/page', Lang::uri('my/new/page')); | ||
|
||
$this->setAppLocale('nl'); | ||
$this->assertEquals('mijn/nieuwe/pagina', Lang::uri('my/new/page')); | ||
$this->assertEquals('mijn/nieuwe/pagina', trans()->uri('my/new/page')); | ||
} | ||
|
||
/** @test */ | ||
public function it_translates_every_segment_in_a_uri_to_the_given_locale() | ||
{ | ||
$this->setTranslations([ | ||
'nl' => [ | ||
'my' => 'mijn', | ||
'new' => 'nieuwe', | ||
'page' => 'pagina', | ||
] | ||
]); | ||
|
||
$this->assertEquals('mijn/nieuwe/pagina', Lang::uri('my/new/page', 'nl')); | ||
} | ||
|
||
/** @test */ | ||
public function it_uses_the_original_values_if_a_translation_does_not_exist() | ||
{ | ||
$this->setTranslations([ | ||
'nl' => [ | ||
'my' => 'mijn', | ||
'new' => 'nieuwe', | ||
] | ||
]); | ||
|
||
$this->assertEquals('mijn/nieuwe/page', Lang::uri('my/new/page', 'nl')); | ||
$this->assertEquals('my/new/page', Lang::uri('my/new/page', 'fr')); | ||
} | ||
|
||
/** @test */ | ||
public function it_ignores_trailing_slashes() | ||
{ | ||
$this->setTranslations([ | ||
'nl' => [ | ||
'my' => 'mijn', | ||
'new' => 'nieuwe', | ||
'page' => 'pagina', | ||
] | ||
]); | ||
|
||
$this->assertEquals('mijn/nieuwe/pagina', Lang::uri('/my/new/page/', 'nl')); | ||
} | ||
|
||
/** @test */ | ||
public function it_skips_placeholders_in_a_uri() | ||
{ | ||
$this->setTranslations([ | ||
'nl' => [ | ||
'articles' => 'artikels', | ||
] | ||
]); | ||
|
||
$this->assertEquals('artikels/{articles}', Lang::uri('articles/{articles}', 'nl')); | ||
} | ||
|
||
/** @test */ | ||
public function you_can_translate_a_full_uri() | ||
{ | ||
$this->setTranslations([ | ||
'nl' => [ | ||
'glass' => 'glas', | ||
'products' => 'producten', | ||
'products/glass' => 'producten/glazen' | ||
] | ||
]); | ||
|
||
$this->assertEquals('producten/glazen', Lang::uri('products/glass', 'nl')); | ||
} | ||
|
||
/** @test */ | ||
public function you_can_translate_a_full_uri_with_placeholder() | ||
{ | ||
$this->setTranslations([ | ||
'nl' => [ | ||
'glass' => 'glas', | ||
'products' => 'producten', | ||
'products/glass/{type}' => 'producten/glazen/{type}' | ||
] | ||
]); | ||
|
||
$this->assertEquals('producten/glazen/{type}', Lang::uri('products/glass/{type}', 'nl')); | ||
} | ||
|
||
/** @test */ | ||
public function you_can_specify_a_namespace() | ||
{ | ||
$this->setTranslations([ | ||
'nl' => [ | ||
'articles' => 'artikels', | ||
] | ||
], 'blog'); | ||
|
||
$this->assertEquals('artikels/{article}', Lang::uri('articles/{article}', 'nl', 'blog')); | ||
} | ||
|
||
/** @test */ | ||
public function the_uri_macro_is_available_via_the_trans_helper() | ||
{ | ||
$this->setTranslations([ | ||
'nl' => [ | ||
'my' => 'mijn', | ||
'new' => 'nieuwe', | ||
'page' => 'pagina', | ||
] | ||
]); | ||
|
||
$this->setAppLocale('en'); | ||
$this->assertEquals('my/new/page', trans()->uri('my/new/page')); | ||
|
||
$this->setAppLocale('nl'); | ||
$this->assertEquals('mijn/nieuwe/pagina', trans()->uri('my/new/page')); | ||
} | ||
} |
Oops, something went wrong.