controller = $controller; $Code = $this->_getCode(); $this->_setCode($Code); $this->_populateMessages ($Code); $this->Session->write("Lang.Messages.$Code", $this->_messages[$Code]); if ( //($this->controller->multiLingualFiles)&& ($Code<>$this->acceptedLanguages[0]) ) { if (is_file(VIEWS.$this->controller->name.DS.$this->controller->action."_".$Code.".thtml")) { $this->controller->render($this->controller->action."_".$Code); die; } else { /* Optional, display flash message */ /* $msg = __("Language specific version not available for this page", Array("$Code")); $this->Session->setFlash($msg); */ } } } /** * Returns or creates an instance of the LocaleComponent. * * @return LocaleComponent instance */ function getInstance() { /* static $instance; if (!isset($instance)) { $instance =& new LocaleComponent(); } return $instance; */ return new LocaleComponent(); } /** * Change the language code for all messages. Only permits changes to values which * are in the acceptedLanguages parameter. This message is not intended to receive * user defined input. * * @param string $langcode 5 character language code * @return true if code changed, false if not. */ function changeCode ($langcode=NULL) { if (!in_array($langcode,$this->acceptedLanguages)) { $msg = __("Invalid language code",Array($langcode)); $this->Session->setFlash($msg); return false; } else { $this->Session->del("Lang.Messages"); $this->Session->write("Lang.Code", $langcode); return true; } } /** * returns a translated string. This method will return the requested message in the * language requested if it exists; if not the message in the default language if it exists * and finally the message $id if no message is defined. * * @param string $id the message or message id * @param array $MessageArgs variables to be substitued into the message defenition * @param int $capitalize how should the message be capitalized * 0 = no change * 1 = first letter of first word * 2 = fist character of all words * Assumed that the message is stored lowercase except when upper case is required * Such as with objects e.g. in German "der Mann". * @param int $punctuate how should the message be punctuated * 0 = "" * 1 = . * 2 = ! * 3 = ? * @param string $Code override for the language to be used. * @return string output message. */ function getString( $id, $MessageArgs=NULL, $capitalize=1, $punctuate=0 ,$Code=NULL) { $Code = (!$Code)?$this->_getCode():$Code; $this->_populateMessages ($Code); $this->_populateMessages ($this->acceptedLanguages[0]); if( isset($id, $this->_messages[$Code][$id])) { if ($MessageArgs) { $string = vsprintf ($this->_messages[$Code][ $id ], $MessageArgs); } else { $string = $this->_messages[$Code][ $id ]; } } elseif ( isset($id, $this->_messages[$this->acceptedLanguages[0]][$id])) { if ($MessageArgs) { $string = vsprintf ($this->_messages[$this->acceptedLanguages[0]][ $id ], $MessageArgs); } else { $string = $this->_messages[$this->acceptedLanguages[0]][ $id ]; } } else { $string = $id; } switch ($capitalize) { case 0: break; case 1: $string = ucfirst($string); break; case 2: $string = ucwords($string); break; } switch ($punctuate) { case 0: break; case 1: if ($Code=='es_es') { $string = "".$string."."; } else { $string = $string."."; } break; case 2: if ($Code=='es_es') { $string = "¡".$string."!"; } else { $string = $string."!"; } break; case 3: if ($Code=='es_es') { $string = "¿".$string."?"; } else { $string = $string."?"; } break; } return $string; } /** * returns the language code for the session. If a language code has not been defined * the code determined from the ethod _checkBrowserLanguage is returned * @return string 5 character language code */ function _getCode () { if (isset($this->Session)) { $return = $this->Session->read("Lang.Code"); if ($return) { return $return; } else { return $this->_checkBrowserLanguage(); } } else { $return = $_SESSION['Lang']['Code']; if ($return) { return $return; } else { return $this->_checkBrowserLanguage(); } } } /** * stores the passed code in the session. */ function _setCode ($langcode=NULL) { $this->Session->write("Lang.Code", $langcode); } /** * This method will return the most appropriate language defined in the acceptedLanguages list * based upon the browser language settings. If no language matches the acceptedLanguages * list, the first item of the acceptedLanguages list (the default language)is returned. * This method isn't optimised and needs improving. * * @return string 5 character language code */ function _checkBrowserLanguage () { if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $Languages = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']); $SLanguages = array(); foreach ($Languages as $Key=> $Language) { $Language = ereg_replace("-","_",$Language); $Language = explode(";", $Language); if (isset($Language[1])) { $Priority = explode("q=", $Language[1]); $Priority=$Priority[1]; } else { $Priority ="1.0"; } $SLanguages[] = array('priority' => $Priority, 'language' => $Language[0]); } // Obtain a list of columns foreach ($SLanguages as $key => $row) { $priority[$key] = $row['priority']; $language[$key] = $row['language']; } // Sort the Slanguges with priority descending, languagecode ascending // Add $Languages as the last parameter, to sort by the common key array_multisort($priority, SORT_DESC, $language, SORT_ASC, $SLanguages); $ALangString = implode(";",$this->acceptedLanguages); foreach ($SLanguages as $A) { $key = array_search($A['language'], $this->acceptedLanguages); if ($key===FALSE) { $GenericLanguage = explode("_", $A['language']); $pos1 = strpos($ALangString, $GenericLanguage[0]); if (is_numeric($pos1)) { $key = $pos1/6; } } if (is_numeric($key)&&(!isset($Code))) { $Code = $this->acceptedLanguages[$key]; } } if (!isset($Code)) { $Code = $this->acceptedLanguages[0]; } } else { $Code = $this->acceptedLanguages[0]; } return $Code; } /** * set the _message variable for the specified language code */ function _populateMessages($langcode) { if (isset($this->Session)) { if ($this->Session->check("Lang.Messages.$langcode")) { $sessionMessages = $this->Session->read("Lang.Messages.$langcode"); } else { $sessionMessages = NULL; } } else { if (isset($_SESSION['Lang']["Messages"][$langcode])) { $sessionMessages = $_SESSION['Lang']["Messages"][$langcode]; } else { $sessionMessages = NULL; } } if ($sessionMessages) { $this->_messages[$langcode] = $sessionMessages; } else { $this->_loadLocaleFile($langcode); if ($langcode<>$this->acceptedLanguages[0]) { $this->_loadLocaleFile($this->acceptedLanguages[0]); } } } /** * Load the language file for the specified language code * * @return true if file loaded, false if not. */ function _loadLocaleFile($langcode) { $fileName = dirname(__FILE__).DS.$this->_messageFolder.DS.$langcode . ".php"; if (file_exists($fileName)) { include( $fileName ); $this->_messages[$langcode] = $messages; return true; } else { $this->_messages[$langcode] = NULL; return false; //$msg = __("not found", Array("$fileName")); //$this->log($msg); } } } ?>