Laravel 4 multilingual website

I am trying to implement a laravel 4 multilingual site with language code in the url (mywebsite.com/en/home and mywebsite.com/de/home).

I've seen a couple of options like filtering all requests and checking if the first parameter is one of the language code.

I also check the package but didn't find one that already does the type work.

Is there a better way to implement it?

thank

+3


source to share


2 answers


Finally, I created a config variable in config / app.php

'available_language' => array('en', 'fr', 'es'),

      

In filters.php I am detecting the browser language:

Route::filter('detectLang', function($lang = "auto")
{
    if($lang != "auto" && in_array($lang , Config::get('app.available_language')))
    {
        Config::set('app.locale', $lang);
    }else{
        $browser_lang = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ',') : '';
        $browser_lang = substr($browser_lang, 0,2);
        $userLang = (in_array($browser_lang, Config::get('app.available_language'))) ? $browser_lang : Config::get('app.locale');
        Config::set('app.locale', $userLang);
    }
});

      



and then in routes.php I can either define the language or force it:

Route::get('/', array(
    'before' => 'detectLang()', // auto-detect language
    function(){
        ...
    })
);

      

or

Route::get('/', array(
    'before' => 'detectLang("fr")', // force language to "fe"
    function(){
        ...
    })
);

      

+6


source


You can set the language variable in the user session.

Then use filter ' before

' and look at that variable and write the correct language file.



If there is no set of variables, use the default (possibly based on their IP address).

+1


source







All Articles