Skip to content

Commit 8209af3

Browse files
committed
Extract Lang::uri() macro to separate package
1 parent e4f20e4 commit 8209af3

File tree

6 files changed

+12
-234
lines changed

6 files changed

+12
-234
lines changed

README.md

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ I know you can get punished for this, but you are one of the hopes of those inno
3232
- [Automatically register](#-register-routes) a route for each locale.
3333
- Use [URL slugs or custom domains](#-supported-locales) (or subdomains).
3434
- Optionally [omit the locale slug from the URL for your main locale](#%EF%B8%8F-omit-slug-for-main-locale).
35-
- Optionally [translate each segment](#-translate-routes) in your URI's.
35+
- Optionally [translate each segment](#-uri-translations) in your URI's.
3636
- [Generate localized route URL's](#-generate-route-urls) using the `route()` helper.
3737
- [Redirect to localized routes](#-redirect-to-routes) using the `redirect()->route()` helper.
3838
- [Generate localized signed route URL's](#-generate-signed-route-urls).
@@ -441,76 +441,18 @@ $signedUrl = URL::signedRoute($name, $parameters, $expiration, true, 'nl');
441441

442442
Check out the [Laravel docs](https://laravel.com/docs/urls#signed-urls) for more info on signed routes.
443443

444-
### 🌎 Translate Routes
444+
### 🌎 URI Translations
445445

446-
If you want to translate the segments of your URI's, create a `routes.php` language file for each locale you [configured](#%EF%B8%8F-supported-locales):
446+
This package includes [codezero/laravel-uri-translator](https://github.com/codezero-be/laravel-uri-translator), which registers a `Lang::uri()` macro that enables you to translate individual, hard-coded URI slugs.
447+
Route parameters will not be translated by this macro.
447448

448-
```
449-
resources
450-
└── lang
451-
├── en
452-
│ └── routes.php
453-
└── nl
454-
└── routes.php
455-
```
456-
457-
In these files, add a translation for each segment, or for the full URI.
458-
459-
If an exact match is found, that will be returned.
460-
Otherwise, each segment will be translated separately.
461-
If a translation is not found, the original segment is used.
462-
463-
```php
464-
// lang/nl/routes.php
465-
return [
466-
'glass' => 'glas',
467-
'products' => 'producten',
468-
'materials' => 'materiaal',
469-
'materials/glass' => 'materiaal/glazen'
470-
];
471-
```
472-
473-
Now you can use our `Lang::uri()` macro during route registration:
474-
475-
```php
476-
Route::localized(function () {
477-
478-
Route::get(Lang::uri('products/glass'), ProductsController::class.'@index')
479-
->name('products.glass');
480-
481-
Route::get(Lang::uri('materials/glass'), MaterialsController::class.'@index')
482-
->name('materials.glass');
449+
Routes with translated URIs need to have a name in order to generate localized versions of it using the `route()` helper or the `Route::localizedUrl()` macro.
450+
Because these routes have different slugs depending on the locale, the route name is the only thing that links them together.
483451

484-
});
485-
```
486-
487-
The above will generate:
488-
489-
- /en/products/glass
490-
- /nl/producten/glass
491-
- /en/materials/glass
492-
- /nl/materiaal/glazen
493-
494-
If you need to get a translation from a package,
495-
you can pass an optional translation namespace as a third parameter to `Lang::uri()`:
496-
497-
```php
498-
Route::localized(function () {
499-
500-
Route::get(Lang::uri('products/glass', null, 'shop'), ProductsController::class.'@index')
501-
->name('products.glass');
502-
503-
});
504-
```
505-
506-
> Note that in order to find a translated version of a route, you will need to give your routes a name.
507-
If you don't name your routes, only the parameters (model route keys) will be translated, not the "hard-coded" slugs.
452+
Refer to [the GitHub page](https://github.com/codezero-be/laravel-uri-translator) to learn how to use the `Lang::uri()` macro.
508453

509454
## 🚏 Route Parameters
510455

511-
Parameter placeholders are not translated via language files. These are values you would provide via the `route()` function.
512-
The `Lang::uri()` macro will skip any parameter placeholder segment.
513-
514456
If you have a model that uses a route key that is translated in the current locale,
515457
then you can still simply pass the model to the `route()` function to get translated URL's.
516458

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"php": "^7.2.5|^8.0",
2424
"0.0.0/composer-include-files": "^1.5",
2525
"codezero/laravel-localizer": "dev-master",
26+
"codezero/laravel-uri-translator": "^1.0",
2627
"illuminate/support": "^7.0|^8.0|^9.0|^10.0"
2728
},
2829
"require-dev": {

src/LocalizedRoutesServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
namespace CodeZero\LocalizedRoutes;
44

55
use CodeZero\LocalizedRoutes\Illuminate\Routing\UrlGenerator;
6-
use CodeZero\LocalizedRoutes\Macros\Lang\UriMacro;
76
use CodeZero\LocalizedRoutes\Macros\Route\HasLocalizedMacro;
87
use CodeZero\LocalizedRoutes\Macros\Route\IsLocalizedMacro;
98
use CodeZero\LocalizedRoutes\Macros\Route\LocalizedMacro;
109
use CodeZero\LocalizedRoutes\Macros\Route\LocalizedUrlMacro;
1110
use CodeZero\Localizer\LocalizerServiceProvider;
11+
use CodeZero\UriTranslator\UriTranslatorServiceProvider;
1212
use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract;
1313
use Illuminate\Support\ServiceProvider;
1414

@@ -55,7 +55,6 @@ protected function registerMacros()
5555
IsLocalizedMacro::register();
5656
LocalizedMacro::register();
5757
LocalizedUrlMacro::register();
58-
UriMacro::register();
5958
}
6059

6160
/**
@@ -89,6 +88,7 @@ protected function mergeConfig()
8988
protected function registerProviders()
9089
{
9190
$this->app->register(LocalizerServiceProvider::class);
91+
$this->app->register(UriTranslatorServiceProvider::class);
9292
}
9393

9494
/**

src/Macros/Lang/UriMacro.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/TestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use CodeZero\BrowserLocale\BrowserLocale;
66
use CodeZero\LocalizedRoutes\LocalizedRoutesServiceProvider;
7+
use CodeZero\UriTranslator\UriTranslatorServiceProvider;
78
use Illuminate\Contracts\View\View;
89
use Illuminate\Support\Collection;
910
use Illuminate\Support\Facades\App;
@@ -167,6 +168,7 @@ protected function getPackageProviders($app)
167168
{
168169
return [
169170
LocalizedRoutesServiceProvider::class,
171+
UriTranslatorServiceProvider::class,
170172
];
171173
}
172174
}

tests/Unit/Macros/Lang/UriMacroTest.php

Lines changed: 0 additions & 121 deletions
This file was deleted.

0 commit comments

Comments
 (0)