Advertisement
Guest User

Untitled

a guest
Jul 25th, 2010
374
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class translateException extends baseException{
  2.     /**
  3.      * @var mixed
  4.      */
  5.     protected $translateVars = null;
  6.  
  7.     /**
  8.      * @param string $message
  9.      * @param mixed $variables
  10.      * @param Exception $previous
  11.      */
  12.     public function  __construct($message = '', $variables = null, Exception $previous = null) {
  13.         parent::__construct($message, 0, $previous);
  14.         $this->translateVars = $variables;
  15.     }
  16.  
  17.     /**
  18.      * @return string
  19.      */
  20.     public function getTranslateMessage(){
  21.         $st = translate::getTranslateString($this->getMessage());
  22.         if(empty($this->translateVars)){
  23.             return $st;
  24.         }
  25.         if(!is_array($this->translateVars)){
  26.             $vars = array($st, $this->translateVars);
  27.         } else{
  28.             $vars = $this->translateVars;
  29.             array_unshift($vars, $st);
  30.         }
  31.         return call_user_func_array('sprintf', $vars);
  32.     }
  33. }
  34.  
  35. class translate{
  36.     const localeRu = 1;
  37.     const localeDe = 2;
  38.     public static $translateRu = array(
  39.         'Number:%d' => 'Номер:%d',
  40.         'Error'     => 'Ошибка'
  41.     );
  42.     public static $translateDe = array(
  43.         'Number:%d' => 'Zahl:%d',
  44.         'Error'     => 'Fehler'
  45.         );
  46.     public static $currentLocale;
  47.     public static function getTranslateString($s){
  48.         $st = '';
  49.         switch( self::$currentLocale ){
  50.             case self::localeRu: $st = self::$translateRu[$s];
  51.                 break;
  52.             case self::localeDe: $st = self::$translateDe[$s];
  53.                 break;
  54.         }
  55.         if(!strlen($st)){
  56.             $st = $s;
  57.         }
  58.         return $st;
  59.     }
  60. }
  61.  
  62. translate::$currentLocale = translate::localeDe;
  63.  
  64. try {
  65.     throw new translateException('Number:%d', 1);
  66.     throw new translateException('Number:%d, String:%s', array(1, 'abc'));
  67.     throw new translateException('Error');
  68. } catch (translateException $e) {
  69.     echo $e->getTranslateMessage();
  70. }
Advertisement
RAW Paste Data Copied
Advertisement