ingeniasoftware / luthier-ci

Improved routing, middleware support, authentication tools and more for CodeIgniter 3 framework
https://luthier.ingenia.me/ci/en/
MIT License
150 stars 38 forks source link

language support in url #5

Closed FLasH3r closed 6 years ago

FLasH3r commented 6 years ago

I'm trying to incorporate this guide, getting 404

https://github.com/bcit-ci/CodeIgniter/wiki/URI-Language-Identifier

Is it even possible?

mateusmcordeiro commented 6 years ago

i have the same question.

andersonsalas commented 6 years ago

Hi, this isn't possible yet. However, I restarted the development of the project so you'll see updates soon.

Cheers.

andersonsalas commented 6 years ago

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]);
        }
    }
}
yahyaerturan commented 5 years ago

Thank you for Middleware solution. One question: How can I make "/" with default locale without having in url segments?

yahyaerturan commented 5 years ago

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?

andersonsalas commented 5 years ago

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 😄