PHP code example of ofat / laravel-translatable-routes
1. Go to this page and download the library: Download ofat/laravel-translatable-routes library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ofat / laravel-translatable-routes example snippets
return [
'locales' => ['en', 'de']
];
Route::localeGroup(function() {
Route::get('[country]/{country}', function($country) {
});
Route::get('[about-us]', function(){
return 'hi!';
});
});
Route::localeGroup(function(){
Route::get('[country]/{country}', function($country){
})->name('country')
});
route('country', ['country' => $country]);
url()->route('country', ['country' => $country])
routeInLocale('de', 'country', ['country' => $country]);
url()->routeInLocale('de', 'country', ['country' => $country]);
route('de.country', ['country' => $country]);
url('[country]/belgium')
url()->to('[country]/belgium')
url()->withLocale('[country]/belgium'); // generates `en/country/belgium`
urlWithLocale('[country]/belgium');
urlWithLocale('[country]/belgium', $params, 'en'); // specify needed locale. generates `de/land/belgium`
route('switch-locale', 'fr')
namespace App\Service\UrlTranslator;
use App\Models\Country;
use Ofat\LaravelTranslatableRoutes\UrlTranslator\Abstracts\BaseUrlTranslation;
class CountryUrlTranslation extends BaseUrlTranslation
{
/**
* Get current route translated url
*
* @param string $locale
* @return string
*/
public function getTranslatedUrl(string $locale): string
{
$country = Country::query()
->where('url->'.$this->route->getLocale(), $this->route->parameter('country'))
->firstOrFail();
return $this->urlGenerator->route('country', $country->url);
}
/**
* Check if current route is applicable to this strategy
*
* @return bool
*/
public function isApplicable(): bool
{
return strpos($this->route->getName(), '.country') > 0;
}
}