Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7510099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:07:43+00:00 2026-05-29T23:07:43+00:00

I am running CI version 2.1 and I have recently installed the CodeIgniter 2.1

  • 0

I am running CI version 2.1 and I have recently installed the CodeIgniter 2.1 internationalization i18n (Original Author: Jérôme Jaglale). The code instructions are here: http://codeigniter.com/wiki/URI_Language_Identifier/

It runs fine but I would like to override the default language if the user set his language preference in a previous visit to the website (this would be stored in a table in the database).

My question is, how do you do this?

I believe MY_Lang.php (where default language is loaded) is actually loaded BEFORE the CodeIgniter MY_Controller so I am unable to load the database to find if a user has a preferred language.

The default language is configured in MY_Lang.php here:

function default_lang()
{
    $browser_lang = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strtok(strip_tags($_SERVER['HTTP_ACCEPT_LANGUAGE']), ',') : '';
    $browser_lang = substr($browser_lang, 0,2);
    $default_lang = array_splice(array_keys($this->languages), 0,1); 
    return (array_key_exists($browser_lang, $this->languages)) ? $browser_lang : $default_lang[0]; 
}

However, I am unable to override it because here I have no access to my $this->session->userdata('language')

Thanks in advance!


ANSWER HERE:


Found out that you can use native PHP cookies to read the cookie inside MY_Lang.php file. If you try to use the cookie helper it won’t work, so make sure you use $_COOKIE instead and run the appropriate filters before getting the content.

Then, in MY_Lang.php just change this:

return (array_key_exists($browser_lang, $this->languages)) ? $browser_lang : $default_lang[0]; 

To this:

if($_COOKIE['int_lang']) {
$preferred_lang = filter_var($_COOKIE['int_lang'], FILTER_SANITIZE_STRING);
return (array_key_exists($preferred_lang, $this->languages)) ? $preferred_lang : $default_lang[0];
}
else
{
return (array_key_exists($browser_lang, $this->languages)) ? $browser_lang : $default_lang[0]; 
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:07:44+00:00Added an answer on May 29, 2026 at 11:07 pm

    If it was me, I’d use cookies for that instead of storing this in a database. It’ll be faster and easier to manage.

    Most users don’t erase cookies, at least not all that often. It would solve your problem: “I am unable to load the database to find if a user has a preferred language”.

    If you need the database storage, you could try using a Hook:
    http://codeigniter.com/user_guide/general/hooks.html

    In a “post_controller_constructor” hook, load you Database Model used for language storage, get the language set by the user and load the language file that you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a reverse proxy here, running Apache in version 2.2.x Essentially I want
I am running Mac OSX 10.5.8. I recently installed a version of macports that
I have recently installed a LAMP server running ubuntu server 11.04. I am trying
I have a Firebird database (running on server version 2.1.3) and am connecting to
I am running the free version of Helicon ISAPI Rewrite on IIS and have
I am running Cygwin Python version 2.5.2. I have a three-line source file, called
I've recently installed mongodb on my CentOS 6 VM running on Vagrant. I added
I have recently installed and activated APC cache on a web server (Centos 5.7,
We have recently installed Orbeon Forms stable 3.8.0 CE's orbeon.war and that works out
I currently have a web server running PHP 5.2.13 and have recently been told:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.