0

I've followed Codeigniter language and all seems to be setup as a hook.

function pick_language() {

    require_once(APPPATH.'/config/language.php');

    session_start();

    // Lang set in URL via ?lang=something
    if(!empty($_GET['lang']))
    {
        // Turn en-gb into en
        $lang = substr($_GET['lang'], 0, 2);
        $_SESSION['lang_code'] = $lang;
    }

    // Lang has already been set and is stored in a session
    elseif( !empty($_SESSION['lang_code']) )
    {
        $lang = $_SESSION['lang_code'];
    }

    // Lang has is picked by a user.
    // Set it to a session variable so we are only checking one place most of the time
    elseif( !empty($_COOKIE['lang_code']) )
    {
        $lang = $_SESSION['lang_code'] = $_COOKIE['lang_code'];
    }

    // Still no Lang. Lets try some browser detection then
    else if (!empty( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ))
    {
        // explode languages into array
        $accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);

        log_message('debug', 'Checking browser languages: '.implode(', ', $accept_langs));

        // Check them all, until we find a match
        foreach ($accept_langs as $lang)
        {
            // Turn en-gb into en
            $lang = substr($lang, 0, 2);

            // Check its in the array. If so, break the loop, we have one!
            if(in_array($lang, array_keys($config['supported_languages'])))
            {
                break;
            }
        }
    }

    // If no language has been worked out - or it is not supported - use the default
    if(empty($lang) or !in_array($lang, array_keys($config['supported_languages'])))
    {
        $lang = $config['default_language'];
    }

    // Whatever we decided the lang was, save it for next time to avoid working it out again
    $_SESSION['lang_code'] = $lang;

    // Load CI config class
    $CI_config =& load_class('Config');

    // Set the language config. Selects the folder name from its key of 'en'
    $CI_config->set_item('language', $config['supported_languages'][$lang]['folder']);

    // Sets a constant to use throughout ALL of CI.
    define('CURRENT_LANGUAGE', $lang);
}

Now when I try to access $config['supported_languages'] it returns null or errors. Why?

2
  • you have not defined $config['supported_languages'] anywhere that I can see? Commented Sep 26, 2012 at 4:48
  • TheShiftExchange: Yes it's defined in the /config/language.php 'require_once' include statement at the top of the page... The issue is when I try to refer to $config['supported_languages']. it won't load. this function pick_language is configured as a hook. So I was thinking it would make $config['supported_languages'] available in all my controllers... Commented Sep 26, 2012 at 16:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.