Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Language detection
  • Hi, 

    I'm just thinking about something : I use the Lang class to get data from Lang files but how can I detect the user's language?

    I know that in the uri, you just have to add "en" or "fr" to get data in the good Lang file but how can language be detected?

    How can "en" or "fr" be added to URI automaticaly? Is there a function to do this?
    I saw the get_lang() function. Is this one the one to use? 
    This function is not in my lang class, i don't know why.

    Thank you so much for any help.
  • All our applications have a standard config file which is in the always_load section of the config, and contains this (and a lot of other app config stuff). We also allow overriding the language via the query string, so developers can easily switch languages, for example to test views with different language files.

    // language cookie available?
    if ($language = \Cookie::get('eaf_lang', false) and in_array($language, array('en', 'nl', 'fr', 'de')))
    {
        Config::set('language', $language);
    }

    // if not, perform browser language autodetection (for the languages we support)
    elseif(\Agent::accepts_language('nl'))
    {
        Config::set('language', 'nl');
    }
    elseif(\Agent::accepts_language('fr'))
    {
        Config::set('language', 'fr');
    }
    elseif(\Agent::accepts_language('de'))
    {
        Config::set('language', 'de');
    }
    else
    {
        Config::set('language', 'en');
    }

    /**
     * Custom application configuration, based on the current environment
     */
    if (Fuel::$env !== 'production')
    {
        /**
         * Make sure the URI is parsed, needed to populate $_GET properly for fastcgi installs
         */
        Config::set('security.output_filter', array('Security::htmlentities'));
        Input::uri();

        /**
         * Allow language overrides using the query string
         */
        if (isset($_GET['lang']) and in_array($_GET['lang'], array('en', 'nl', 'fr', 'de')))
        {
            $language = $_GET['lang'];
            Config::set('language', $language);
        }

        /**
         * Allow profiling override using the query string
         */
        Config::set('profiling', isset($_GET['profile']));
    }

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion