carga la clase POMO */ var $textos; // languages: idiomas de la aplicación. Por cada código de idioma ('es','en', etc...) se establece el nombre del archivo con las traducciones definido // en el directorio "application\language\locales" ('es_ES', 'en_EN', etc...). // Se establece como idioma por defecto el primer idioma indicado en este array. var $languages = array( 'eu' => 'eu_ES', 'es' => 'es_ES' ); // special URIs (not localized) --> A special URI is not prefixed by a language. The root URI (/) is by default a special URI. // You might need other special URIs, like for an admin section in just one language. // si establecemos site_url('admin');, podemos acceder a la pagina de administración sin dato de idioma: http://xxx/index.php/admin var $special = array ( "" ); // where to redirect if no language in URI var $default_uri = ''; /**************************************************/ /** * Constructor * * @access public */ function __construct() { log_message('debug', "Language Class Initialized"); // --- Nuevo: Control de idiomas por url --- global $CFG; global $URI; global $RTR; //Comprobamos si se encuentra el dato "index.php" en la URL actual. Si no se encuentra lo añadimos $current_url= $_SERVER['REQUEST_URI']; $position_index = strpos($current_url, "index.php"); if ($position_index <= 0) { // redirect a la url con index.php $idioma_por_defecto = $this->default_lang(); if ($_SERVER["HTTP_HOST"] == "boliche") { $raiz_sitio = $CFG->item('cfg_raiz_sitio'); $new_url = str_replace($raiz_sitio, $raiz_sitio . "/index.php/" . $idioma_por_defecto, $current_url); } else { if($current_url != "/") { $new_url = str_replace($CFG->item('base_url'), $CFG->item('base_url') . "index.php/" . $idioma_por_defecto, $current_url); } else{ $new_url = $CFG->item('base_url') . "index.php/" . $idioma_por_defecto; } } if(substr($new_url, -1) != "/") { $new_url = $new_url . "/"; } header("Location: " . $new_url, TRUE, 302); exit; } //Obtener el idioma de la URL $segment = $URI->segment(1); if($segment != "") { //Comprobamos si dicho idioma está definido en el array de idiomas if (isset($this->languages[$segment])) { // URI with language -> ok //Set "language" config item to received language $language = $this->languages[$segment]; $CFG->set_item('language', $language); } else if(strlen($segment) == 2) { //Consideramos que viene en la URI un código de idioma, que no existe (sino hubiese entrado por la primera condición) //Establecer el idioma por defecto $idioma_por_defecto = $this->default_lang(); //set default language $CFG->set_item('language', $this->languages[$idioma_por_defecto]); //redirect $new_url = str_replace($segment . "/", $idioma_por_defecto . "/", $URI->uri_string); if(substr($new_url, -1) != "/") { $new_url = $new_url . "/"; } header("Location: " . $CFG->site_url($this->localized($new_url)), TRUE, 302); exit; } else if ($this->is_special($segment)) { // special URI -> no redirect return; } else { // URI without language -> redirect to default_uri // set default language $idioma_por_defecto = $this->default_lang(); $CFG->set_item('language', $this->languages[$idioma_por_defecto]); // redirect $url_redirigir = $CFG->site_url($this->localized($URI->uri_string)); if(substr($url_redirigir, -1) != "/"){ $url_redirigir = $url_redirigir . "/"; } header("Location: " . $url_redirigir, TRUE, 302); exit; } } else { //URL sin idioma $idioma_por_defecto = $this->default_lang(); $CFG->set_item('language', $this->languages[$idioma_por_defecto]); // redirect $url_redirigir = $CFG->site_url($this->localized($URI->uri_string) . "/" . $this->default_lang()); if(substr($url_redirigir, -1) != "/"){ $url_redirigir = $url_redirigir . "/"; } header("Location: " . $url_redirigir, TRUE, 302); exit; } } //+++++++++++++++++++++++++++++++ FIN NUEVO: Control de idiomas +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // -------------------------------------------------------------------- // ++++ Clases definidas en "system/core/Lang.php", redefinidas aquí para leer de los archivos de lenguaje correspondientes /** * Load a language file * * @access public * @param mixed the name of the language file to be loaded. Can be an array * @param string the language (en_EN, es_ES, etc.) * @param bool return loaded array of translations * @param bool add suffix to $langfile --> En los archivos a utilizar el nombre del archivo no lleva la extensión "lang", directamente se denominan: "lang.mo" * @param string alternative path to look for language file * @return mixed */ function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '') { //Los archivos de traducciones siempre se llaman "xx_XX_lang" donde xx_XX es el código del idioma correspondiente $langfile = ''; $langfile = str_replace('.mo', '', $langfile); if ($add_suffix == TRUE) { $langfile = str_replace('_lang.', '', $langfile).'_lang'; } $langfile .= '.mo'; //Si llegamos hasta aquí es porque todavía no están cargados los textos de archivo $config =& get_config(); if ($idiom == '') { $deft_lang = ( ! isset($config['language'])) ? 'eu_ES' : $config['language']; $idiom = ($deft_lang == '') ? 'eu_ES' : $deft_lang; } //Añadir el código al nombre del archivo, que sigue el siguiente formato "xx_XX_lang.mo", por ejemplo "es_ES_lang.mo" o "eu_ES_lang.mo" $langfile = $idiom . $langfile; // Determine where the language file is and load it $path_archivo = ""; $path_ruta = ""; if ($alt_path != '' && file_exists($alt_path.'language/locales/'.$idiom.'/LC_MESSAGES/'.$langfile)) { $path_archivo = $alt_path.'language/locales/'.$idiom.'/LC_MESSAGES/'.$langfile; $path_ruta = $alt_path.'language/locales'; } else { $found = FALSE; foreach (get_instance()->load->get_package_paths(TRUE) as $package_path) { if (file_exists($package_path.'language/locales/'.$idiom.'/LC_MESSAGES/'.$langfile)) { $path_archivo = $package_path.'language/locales/'.$idiom.'/LC_MESSAGES/'.$langfile; $path_ruta = $package_path.'language/locales'; $found = TRUE; break; } } if ($found !== TRUE) { show_error('Unable to load the requested language file: language/locales/'.$idiom.'/LC_MESSAGES/'.$langfile); } } //---- Especial para el Gettex ---- //Change the languages setting and load the mo file log_message('debug', '[application/core/Lang.php - load]: valor de idiom: ' . $idiom); $lang_path = $path_ruta; //Establece el valor de una variable de entorno putenv('LANG='.$idiom.'.UTF-8'); //Establecer la información de la configuración regional setlocale(LC_ALL, $idiom.'.UTF-8'); //Establece la ruta del dominio bind_textdomain_codeset('lang',"UTF-8"); bindtextdomain('lang', $lang_path); // Establece el dominio actual textdomain('lang'); log_message('debug', 'Language file loaded: ' . $path_archivo); //Cargar los textos usando la clase POMO $varCI = get_instance(); $varCI->load->library('POMO'); $this->textos = new POMO(); $this->textos->cargarTextos($idiom, $path_archivo, 'lang'); return TRUE; } // -------------------------------------------------------------------- /** * Fetch a single line of text from the language array * * @access public * @param string $line the language line * @return string */ function line($line = '') { if($line == '') { $value = FALSE; } else { $value = $this->textos->gettext($line,'lang'); } if ($value === FALSE) { log_message('error', 'Could not find the language line "'.$line.'"'); } return $value; } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // NUEVO: Control de idiomas por url // http://maestric.com/en/doc/php/codeigniter_i18n //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // get current language // ex: return 'en' if language in CI config is 'english' function current_lang() { global $CFG; $language = $CFG->item('language'); $lang = array_search($language, $this->languages); if ($lang) { return $lang; } return NULL; } function is_special($uri) { $exploded = explode('/', $uri); if (in_array($exploded[0], $this->special)) { return TRUE; } if(isset($this->languages[$uri])) { return TRUE; } return FALSE; } function switch_uri($lang) { $CI =& get_instance(); $uri = $CI->uri->uri_string(); if ($uri != "") { //Cambia el idioma actual por el recibido $exploded = explode('/', $uri); if($exploded[0] == $this->current_lang()) { //URI with a lang $exploded[0] = $lang; } $uri = implode('/',$exploded); //Si la ruta no termina en "/", se lo añadimos if(substr($uri, -1) != "/") { $uri = $uri . "/"; } } return $uri; } // is there a language segment in this $uri? function has_language($uri) { $first_segment = NULL; $exploded = explode('/', $uri); if(isset($exploded[0])) { if($exploded[0] != '') { $first_segment = $exploded[0]; } else if(isset($exploded[1]) && $exploded[1] != '') { $first_segment = $exploded[1]; } } if($first_segment != NULL) { return isset($this->languages[$first_segment]); } return FALSE; } // default language: first element of $this->languages function default_lang() { //Comprobar si hay idioma guardado en cookie $idioma_por_defecto = ""; global $IN; $idioma_en_cookie = $IN->cookie('idioma_usuario'); if(isset($idioma_en_cookie) && $idioma_en_cookie != "") { //log_message('debug', 'Core/Lang.php - Idioma por defecto, idioma en cookie: ' . $IN->cookie('idioma_usuario')); $idioma_por_defecto = $IN->cookie('idioma_usuario'); } else { //No hay idioma en cookie. Devolvemos como idioma por defecto, el primero definido en el array $languages, definido en esta página arriba. foreach ($this->languages as $lang => $language) { $idioma_por_defecto = $lang; break; } } return $idioma_por_defecto; } // add language segment to $uri (if appropriate) function localized($uri) { if($this->has_language($uri) || $this->is_special($uri) || preg_match('/(.+)\.[a-zA-Z0-9]{2,4}$/', $uri)) { // we don't need a language segment because: // - there's already one or // - it's a special uri (set in $special) or // - that's a link to a file } else { $uri = $this->current_lang() . '/' . $uri; } return $uri; } // Check if a language code is supported in the website function is_valid_lang($langCode) { if(array_key_exists($langCode, $this->languages)) { return TRUE; } else { return FALSE; } } //+++++++++++++++++++++++++++++++ FIN NUEVO +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ }