Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

language support in url #5

Closed
FLasH3r opened this issue Jan 9, 2018 · 6 comments
Closed

language support in url #5

FLasH3r opened this issue Jan 9, 2018 · 6 comments
Labels

Comments

@FLasH3r
Copy link

FLasH3r commented Jan 9, 2018

I'm trying to incorporate this guide, getting 404

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

Is it even possible?

@mateusmcordeiro
Copy link

i have the same question.

@andersonsalas
Copy link
Contributor

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

Cheers.

@andersonsalas
Copy link
Contributor

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
Copy link

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

@yahyaerturan
Copy link

yahyaerturan commented Dec 27, 2018

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
Copy link
Contributor

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 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants