<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/**
 * Modular Extensions - HMVC
 * Adapted from wiredesignz codeigniter-modular-extensions-hmvc 5.5
 */
#[\AllowDynamicProperties]
class MX_Lang extends CI_Lang
{
	/** Fetch a language array **/
	public function language($langfile, $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
	{
		if (is_array($langfile))
		{
			foreach($langfile as $_lang) $this->language($_lang, $idiom, $return, $add_suffix, $alt_path);
			return $this->language;
		}

		$deft_lang = CI::$APP->config->item('language');
		$idiom = ($idiom != '') ? $idiom : $deft_lang;

		if (in_array($langfile.'_lang'.EXT, $this->is_loaded, TRUE))
			return $this->language;

		$_module = CI::$APP->router->fetch_module();

		list($path, $_langfile) = Modules::find($langfile.'_lang', $_module, 'language/'.$idiom.'/');

		if ($path === FALSE)
		{
			if ($lang = parent::load($langfile, $idiom, $return, $add_suffix, $alt_path))
				return $lang;
		}
		else
		{
			if ($lang = Modules::load_file($_langfile, $path, 'lang'))
			{
				if ($return) return $lang;
				$this->language = array_merge($this->language, $lang);
				$this->is_loaded[] = $langfile.'_lang'.EXT;
				unset($lang);
			}
		}

		return $this->language;
	}
}
