Created
January 30, 2017 19:42
-
-
Save bkader/b39ecc25f95ae5f4f9c08f878e3bdb85 to your computer and use it in GitHub Desktop.
This extension load the english version of a language then it will replace lines by the existing ones on the new requested langauge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
/** | |
* MY_Lang | |
* | |
* This extension of CodeIgniter CI_Lang allows you to load any file in any | |
* language even if the language file does not exists BUT the english version | |
* must exist in order to proceed. | |
* | |
* So to make short: ALWAYS have the english version of the language file then | |
* create as many languages versions as you wish. If the line does not exist | |
* in the requested language, it will user the english one. | |
* | |
* @package CodeIgniter | |
* @category Core | |
* @author Kader Bouyakoub <[email protected]> | |
* @link http://www.bkader.com/ | |
*/ | |
class MY_Lang extends CI_Lang | |
{ | |
/** | |
* Holds Config class object | |
* @var array | |
*/ | |
protected $config; | |
/** | |
* Constructor | |
* @param none | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
// ------------------------------------------------------------------------ | |
/** | |
* Does the same job as parent's load method except that it will load the | |
* english version first, then it will replace values with those found | |
* inside requested language file. | |
* | |
* @param mixed $langfile Language file name | |
* @param string $idiom Language name (english, etc.) | |
* @param bool $return Whether to return the loaded array of translations | |
* @param bool $add_suffix Whether to add suffix to $langfile | |
* @param string $alt_path Alternative path to look for the language file | |
* | |
* @return void|string[] Array containing translations, if $return is set to TRUE | |
*/ | |
public function load($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') | |
{ | |
// In case of multiple files | |
if (is_array($langfile)) | |
{ | |
foreach ($langfile as $value) | |
{ | |
$this->load($value, $idiom, $return, $add_suffix, $alt_path); | |
} | |
return; | |
} | |
// Remove .php extension if set | |
$langfile = str_replace('.php', '', $langfile); | |
// Remove the _lang suffix | |
if ($add_suffix === TRUE) { | |
$langfile = preg_replace('/_lang$/', '', $langfile).'_lang'; | |
} | |
$langfile .= '.php'; | |
if (empty($idiom) OR ! preg_match('/^[a-z_-]+$/i', $idiom)) | |
{ | |
$idiom = $this->config->item('language'); | |
} | |
if ($return === FALSE && isset($this->is_loaded[$langfile]) && $this->is_loaded[$langfile] === $idiom) | |
{ | |
return; | |
} | |
// Load the english version first, then we load the the requested one | |
$full_lang = array(); | |
// Load the base file, so any others found can override it | |
$basepath = BASEPATH.'language/english/'.$langfile; | |
if (($found = file_exists($basepath)) === TRUE) | |
{ | |
include($basepath); | |
} | |
// Do we have an alternative path to look in? | |
if ($alt_path !== '') | |
{ | |
$alt_path .= 'language/english/'.$langfile; | |
if (file_exists($alt_path)) | |
{ | |
include($alt_path); | |
$found = TRUE; | |
} | |
} | |
else | |
{ | |
foreach (get_instance()->load->get_package_paths(TRUE) as $package_path) | |
{ | |
$package_path .= 'language/english/'.$langfile; | |
if ($basepath !== $package_path && file_exists($package_path)) | |
{ | |
include($package_path); | |
$found = TRUE; | |
break; | |
} | |
} | |
} | |
if ($found !== TRUE) | |
{ | |
show_error('Unable to load the requested language file: language/english/'.$langfile); | |
} | |
$full_lang = isset($lang) ? $lang : array(); | |
$lang = array(); | |
// Load the base file, so any others found can override it | |
$basepath = BASEPATH.'language/'.$idiom.'/'.$langfile; | |
if (file_exists($basepath)) | |
{ | |
include($basepath); | |
} | |
// Do we have an alternative path to look in? | |
if ($alt_path !== '') | |
{ | |
$alt_path .= 'language/'.$idiom.'/'.$langfile; | |
if (file_exists($alt_path)) | |
{ | |
include($alt_path); | |
} | |
} | |
else | |
{ | |
foreach (get_instance()->load->get_package_paths(TRUE) as $package_path) | |
{ | |
$package_path .= 'language/'.$idiom.'/'.$langfile; | |
if ($basepath !== $package_path && file_exists($package_path)) | |
{ | |
include($package_path); | |
break; | |
} | |
} | |
} | |
if ($found !== TRUE) | |
{ | |
show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile); | |
} | |
isset($lang) OR $lang = array(); | |
$full_lang = array_replace_recursive($full_lang, $lang); | |
if ( ! isset($full_lang) OR ! is_array($full_lang)) | |
{ | |
log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile); | |
if ($return === TRUE) | |
{ | |
return array(); | |
} | |
return; | |
} | |
if ($return === TRUE) | |
{ | |
return $full_lang; | |
} | |
$this->is_loaded[$langfile] = $idiom; | |
$this->language = array_merge($this->language, $full_lang); | |
unset($full_lang); | |
log_message('info', 'Language file loaded: language/'.$idiom.'/'.$langfile); | |
return TRUE; | |
} | |
} | |
/* End of file MY_Lang.php */ | |
/* Location: ./application/core/MY_Lang.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment