Codeigniter different DB for each language Codeigniter different DB for each language codeigniter codeigniter

Codeigniter different DB for each language


For the best result I would create a pre_system hook that checks whether a $_SESSION['lang'] variable exists and if it does set a constant:

define('LANG', isset($_SESSION['lang']) ? $_SESSION['lang'] : 'EN');

Then in your database config file do this:

$db['default']['hostname'] = "localhost";$db['default']['username'] = "root";$db['default']['password'] = "";$db['default']['database'] = "database_".LANG;$db['default']['dbdriver'] = "mysql";$db['default']['dbprefix'] = "";$db['default']['pconnect'] = TRUE;$db['default']['db_debug'] = FALSE;$db['default']['cache_on'] = FALSE;$db['default']['cachedir'] = "";$db['default']['char_set'] = "utf8";$db['default']['dbcollat'] = "utf8_general_ci";

Should do the trick with minimum fuss.


A variation of Phil Sturgeaon answer:

$active_group = LANG; // switch the DB based on the LANG constant$db['EN']['hostname'] = "localhost";$db['EN']['username'] = "root";$db['EN']['password'] = "";$db['EN']['database'] = "databasename_EN";// etc$db['FR']['hostname'] = "localhost";$db['FR']['username'] = "root";$db['FR']['password'] = "";$db['FR']['database'] = "database_FR";//etc


i guess you could create a MyModel class extending Model and handling this gymnastic. than all you'd need to do is use MyModel as the parent class of your models.