Skip to content

Instantly share code, notes, and snippets.

@jgmuchiri
Last active October 19, 2018 02:06
Show Gist options
  • Save jgmuchiri/92bb8b72b4d5a5cb02b7a88c1516edff to your computer and use it in GitHub Desktop.
Save jgmuchiri/92bb8b72b4d5a5cb02b7a88c1516edff to your computer and use it in GitHub Desktop.
CodeIgniter load language files dynamically. User can select from dropdown menu their preferred language. Autoload this model.
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle waves-effect waves-dark"
href="#"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false">
<i class="fa fa-flag"></i> <?php echo ucwords($this->My_conf->getLanguage()); ?>
</a>
<div class="dropdown-menu dropdown-menu-right user-dd animated">
<?php foreach($this->My_conf->getLanguages() as $language){
echo anchor(uri_string().'?language='.$language,icon('flag').' '.$language,'class="dropdown-item"');
} ?>
</div>
</li>
<?php if(!defined('BASEPATH')) {
exit('No direct script access allowed');
}
//autoload this model
class My_config extends CI_Model
{
public function __construct()
{
$this->setLanguage();
}
/**
* @param $item
*
* @return bool
*/
function session($item)
{
if(is_array($item)) { //means we are requesting setting session
$this->session->set_userdata($item);
return TRUE;
}
return $this->session->userdata($item);
}
/**
* set language to session
*/
function setLanguage()
{
$langFiles = $this->loadLanguageFiles();
if(isset($_GET['language'])) {
$lang = $_GET['language'];
if(!in_array($lang, $this->getLanguages()))
$this->session(['language' => config_item('language')]);
$this->session(['language' => $lang]);
}
$this->lang->load($langFiles, $this->session('language'));
}
/**
* @return mixed
*/
function getLanguage()
{
if($this->session('language'))
return $this->session('language');
return config_item('language');
}
function getLanguages()
{
$dir = new DirectoryIterator(APPPATH.'language');
$languages = [];
foreach ($dir as $fileinfo) {
if($fileinfo->isDir() && !$fileinfo->isDot()) {
$languages[] = $fileinfo->getFilename();
}
}
return $languages;
}
/**
* @param array $replace
*
* @return mixed
*/
function loadLanguageFiles(Array $replace = [])
{
static $autoload;
if(empty($autoload)) {
$file_path = APPPATH.'config/autoload.php';
$found = FALSE;
if(file_exists($file_path)) {
$found = TRUE;
require($file_path);
}
if(file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/autoload.php')) {
require($file_path);
} elseif(!$found) {
set_status_header(503);
echo 'The configuration file does not exist.';
exit(3); // EXIT_CONFIG
}
// Does the $config array exist in the file?
if(!isset($autoload) OR !is_array($autoload)) {
set_status_header(503);
echo 'Your config file does not appear to be formatted correctly.';
exit(3); // EXIT_CONFIG
}
}
// Are any values being dynamically added or replaced?
foreach ($replace as $key => $val) {
$autoload[$key] = $val;
}
return $autoload['language'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment