language support in url
Closed this issue · 6 comments
I'm trying to incorporate this guide, getting 404
https://github.com/bcit-ci/CodeIgniter/wiki/URI-Language-Identifier
Is it even possible?
i have the same question.
Hi, this isn't possible yet. However, I restarted the development of the project so you'll see updates soon.
Cheers.
This is an example of language managment using route parameters and middleware:
Routing:
# application/routes/web.php
Route::get('/', function(){
// "Default" route. This is a good place to ask for a cookie, session variable or something
// that let us to restore the previous user language, or showing a language
// select page if no information provided
redirect(route('homepage', ['_locale' => 'en']));
});
Route::group('{_locale}', ['middleware' => ['LangMiddleware']], function(){
Route::get('home', function(){
var_dump( ci()->lang->line('test') );
})->name('homepage');
Route::get('about', function(){
var_dump( ci()->lang->line('test') );
})->name('about');
});
Middleware:
# application/middleware/LangMiddleware.php
class LangMiddleware
{
public function run()
{
// Getting the current locale from the route parameter
$locale = ci()->route->param('_locale');
$langs = [
'es' => 'spanish',
'en' => 'english',
'it' => 'italian',
'br' => 'portuguese-brazilian',
'ge' => 'german',
];
// With the current locale, we can perform some deterministic operations
// in order to set the current framework language
if(isset($langs[$locale]))
{
ci()->lang->load('test', $langs[$locale]);
}
}
}
Thank you for Middleware solution. One question: How can I make "/" with default locale without having in url segments?
Or can we do something like that:
Route::group('{_locale}', ['middleware' => ['LangMiddleware']], function(){
if( {_locale} === NULL)
{
{_locale} = "en"
}
Route::get('/', "WelcomeController@index")->name('homepage');
Route::get('/about', "WelcomeController@about")->name('about');
});
What I want to achieve, if there is no language identifer in URI, it means locale is my default locale. If it is defined in URI, locale is uri-driven locale. I don't want to mention default locale in url.
Or should I create a new issue?
Or can we do something like that:
Route::group('{_locale}', ['middleware' => ['LangMiddleware']], function(){if( {_locale} === NULL) { {_locale} = "en" } Route::get('/', "WelcomeController@index")->name('homepage'); Route::get('/about', "WelcomeController@about")->name('about');
});
What I want to achieve, if there is no language identifer in URI, it means locale is my default locale. If it is defined in URI, locale is uri-driven locale. I don't want to mention default locale in url.
Or should I create a new issue?
Howdy @yahyaerturan!
This is a old issue, please create a new one in order to address your requirement 😄