Skip to content

Instantly share code, notes, and snippets.

@rbarros
Created June 8, 2012 13:13
Show Gist options
  • Save rbarros/2895542 to your computer and use it in GitHub Desktop.
Save rbarros/2895542 to your computer and use it in GitHub Desktop.
Classe para carregamento de arquivos de idioma.
<?php
/**
* Class Lang
*
* An open source application
*
* @package Neogas
* @category Classes
* @author Ramon Barros
* @copyright Copyright (c) 2011, Ezoom Ltda.
* @license http://www.ramon-barros.com/
* @link http://public.ramon-barros.com/
* @since Version 1.6.12
* @filesource Lang.class.php
*/
include('Parser.class.php');
/**
* Lang extends CI_Parser
*
* @package Lang Application
* @subpackage Libraries
* @category Parser
* @author Ramon Barros
* @link http://public.ramon-barros.com/name
*/
class Lang extends CI_Parser
{
private $language;
private $default;
var $languages = array();
var $is_loaded = array();
var $values;
var $path;
var $modules_path;
/**
* Construct Function
*
* Constroi e salvas as configurações da linguagem do site
*
* @access public
*/
public function __construct()
{
$this->language = &$_SESSION['lang'];
$this->path = 'language/'.$this->language."/";
$this->is_loaded["flash"]["banner_base"] = 'flash/banner_base_'.$this->language.'.swf?industrial=pt/Solucoes_Aplicacoes/Gas_Natural_Industrial&residencial=pt/Solucoes_Aplicacoes/Gas_Natural_residencial&veicular=pt/Solucoes_Aplicacoes/Gas_Natural_veicularTeste&comercial=pt/Solucoes_Aplicacoes/Gas_Natural_Comercial&especiais=pt/Solucoes_Aplicacoes/Gas_Natural_Especial';
$this->is_loaded["flash"]["player-neogas-home"] = 'flash/player-neogas-home.swf';
$this->is_loaded["flash"]["neogas_mundo"] = 'language/'.$this->language.'/flash/neogas_mundo.swf';
$this->is_loaded["usercontrols"]["topo"] = 'modules/usercontrols/topo.php';
$this->is_loaded["usercontrols"]["rodape"] = 'modules/usercontrols/rodape.php';
$this->is_loaded["usercontrols"]["menuLateral"] = 'modules/usercontrols/menuLateral.php';
$this->is_loaded["usercontrols"]["menuLateralBusca"] = 'modules/usercontrols/menuLateralBusca.php';
$this->is_loaded["usercontrols"]["menuLateralAssistenciaTecnica"] = 'modules/usercontrols/menuLateralAssistenciaTecnica.php';
$this->is_loaded["usercontrols"]["menuLateralNoticias"] = 'modules/usercontrols/menuLateralNoticias.php';
$this->is_loaded["usercontrols"]["menuLateralConsultoriaTecnica"] = 'modules/usercontrols/menuLateralConsultoriaTecnica.php';
$this->is_loaded["usercontrols"]["menuLateralEmpresa"] = 'modules/usercontrols/menuLateralEmpresa.php';
$this->is_loaded["usercontrols"]["menuLateralSolucoesAplicacoes"] = 'modules/usercontrols/menuLateralSolucoesAplicacoes.php';
$this->is_loaded["usercontrols"]["menuLateralProdutosServicos"] = 'modules/usercontrols/menuLateralProdutosServicos.php';
$this->is_loaded["usercontrols"]["menuLateralFaleConosco"] = 'modules/usercontrols/menuLateralFaleConosco.php';
$this->is_loaded["usercontrols"]["bread"] = 'modules/usercontrols/bread.php';
$this->is_loaded["usercontrols"]["compartilhe"] = 'modules/usercontrols/compartilhe.php';
}
/**
* Set the accepted languages
*
* @access private
* @return void
*/
private function _set_languages()
{
if ((count($this->languages) == 0) AND isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) AND $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
{
$languages = preg_replace('/(;q=[0-9\.]+)/i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
$this->languages = explode(',', $languages);
}
if (count($this->languages) == 0)
{
$this->languages = array('Undefined');
}
}
/**
* Get the accepted languages
*
* @access public
* @return array
*/
public function languages()
{
if (count($this->languages) == 0)
{
$this->_set_languages();
}
return $this->languages;
}
/**
* Load a language field
*
* @access public
* @param converts the fields in the selected language
* @param string the language (pt, en, es, etc)
* @return mixed
*/
public function fields($fields)
{
if(is_array($fields)){
foreach($fields as $key=>$field)
{
if(preg_match('/('.$this->language.')/',$key))
{
$this->values[str_replace('_'.$this->language,"",$key)]= $field;
}else{
$this->values[$key]= $field;
}
}
return $this->values;
}
}
/**
* Parse a String
*
* Parses pseudo-variables contained in the specified string,
* replacing them with the data in the second param
*
* @access public
* @param string
* @param array
* @return string
*/
public function parserText($text,$data)
{
return $this->parse($text,$data);
}
/**
* Returns the current language
*
* @access public
* @return string
*/
public function get_lang()
{
return $this->language;
}
/**
* Converts url into an array
*
* @access public
* @param remove caracter
* @return array
*/
public function queryString($remove='')
{
$result ='';
$remove=array_flip(explode(',',$remove));
parse_str($_SERVER['QUERY_STRING'],$queryStr);
$queryStr=array_diff_key($queryStr,$remove);
foreach($queryStr as $key=>$value){
if(is_array($value)){
unset($temp);
foreach($value as $value2){$temp[]=$key."[]=".$value2;}
$result[]=implode('/',$temp);
}else{
if(!preg_match('/(_view)/',$value))
{
$result[]=ucfirst($value); //str_replace('-','_',$value)
}
}//$result[]="$key=".$value;
}
if($result!='') { return implode('/',$result); }
}
/**
* Loads the language file for the language reported
*
* @access public
* @param string
* @param string
* @param bool
* @param bool
* @param string
* @return mixed
*/
public function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
{
$langfile = str_replace('.php', '', $langfile);
if ($add_suffix == TRUE)
{
$langfile = str_replace('_lang.', '', $langfile).'_lang';
}
$langfile .= '.php';
if (in_array($langfile, $this->is_loaded, TRUE))
{
return;
}
$config['language'] = $this->get_lang();
if ($idiom == '')
{
$deft_lang = ( ! isset($config['language'])) ? 'en' : $config['language'];
$idiom = ($deft_lang == '') ? 'en' : $deft_lang;
}
// Determine where the language file is and load it
if ($alt_path != '' && file_exists($alt_path.'language/'.$idiom.'/'.$langfile))
{
include($alt_path.'language/'.$idiom.'/'.$langfile);
}else{
$found = FALSE;
//foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
//{
$package_path = '';
if (file_exists($package_path.'language/'.$idiom.'/'.$langfile))
{
include($package_path.'language/'.$idiom.'/'.$langfile);
$found = TRUE;
//break;
}
//}
if ($found !== TRUE)
{
//show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
}
}
if ( ! isset($lang))
{
//log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
return;
}
if ($return == TRUE)
{
return $lang;
}
$this->is_loaded['langfile'] = $langfile;
$this->languages = array_merge($this->languages, $lang);
unset($lang);
//log_message('debug', 'Language file loaded: language/'.$idiom.'/'.$langfile);
return TRUE;
}
}
// END Lang Class
/* End of file Lang.class.php */
/* Location: ./path/subpackage/Lang.class.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment