[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/locale/ -> locale.class.php (source)

   1  <?php
   2  
   3      /**
   4       * \defgroup Locale
   5       *
   6       * The Locale module is used for localization purposes. Its main class is the Locale class
   7       * that is capable of loading very simple php files containing a big array of string identifiers
   8       * and their translations. The main method of the class is the Locale::tr() that given a string
   9       * identifier, will return the translated version.
  10       *
  11       * Locale files can also specify default encodings and very basic date and time formats (this will
  12       * be improved in next version) At the moment it is also possible to use right-to-left languages
  13       * even though there is none available yet.
  14       *
  15       * The Locales class is the preferred way to load translations from disk since it has
  16       * caching mechanisms so that we don't have to load the data everytime from disk.
  17       *
  18       * Please see http://wiki.lifetype.net/index.php/Translating_LifeType for more information regading
  19       * how to work with locale files in LifeType.
  20       *
  21       * @see Locale
  22       * @see Locales
  23       */
  24  
  25      
  26      lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
  27  
  28      define( "DEFAULT_LOCALE_FOLDER", PLOG_CLASS_PATH . "locale" );
  29      
  30      /**
  31       * default locale format that will be used if none available
  32       */
  33      define( "DEFAULT_LOCALE_FORMAT", "%d/%m/%Y %H:%M" );
  34      
  35      /**
  36       * default encoding if the locale does not specify one
  37       */
  38      define( "DEFAULT_ENCODING", "iso-8859-1" );
  39      
  40      /**
  41       * default direction for texts, if the locale does not specify one
  42       */
  43      define( "DEFAULT_DIRECTION", "ltr" );
  44      
  45      /**
  46       * default first day fot calendar
  47       */
  48      define( "DEFAULT_FIRST_DAY_OF_WEEK", 1 );
  49  
  50      /**
  51       * \ingroup Locale
  52       *
  53       * Class used to localize messages and things such as dates and numbers.
  54       *
  55       * To use this class, we will have to provide a file containing an array
  56       * of the form:
  57       *
  58       * <pre>
  59       * $messages["identifier"] = "Translated text"
  60       * </pre>
  61       *
  62       * The file will be loaded when creating this object and must be called following
  63       * the same scheme: locale_lang_COUNTRY (see constructor on locales namig schemes)
  64       *
  65       * When we want to translate a string, we will have to use its identifier, that will
  66       * be looked up in the array containing all the messages. If there is a message for that
  67       * identifier, it will be returned or a empty string otherwise.
  68       *
  69       * This class is extensively used throughout the templates to localize texts, dates
  70       * and numbers, being the formatDate function one of the most importants of this class.
  71       *
  72       * <b>IMPORTANT:</b> For performance reasons, it is recommended to use the Locales::getLocale
  73       * method instead of creating new Locale objects every time we need one. The getLocale methods
  74       * offers caching capabilities so that the file with the messages will not need to be fetched
  75       * every time from disk.
  76       * @see Locales::getLocale()
  77       */
  78      class Locale  
  79      {
  80  
  81          var $_defaultFolder;
  82          var $_code;    // our ISO locale code, eg. es_ES, en_UK, etc
  83          var $_cache;
  84          var $_messages;
  85          var $_charset;
  86          var $_description;
  87          var $_dateFormat;
  88          var $_firstDayOfWeek;
  89          var $_dataLoaded;
  90  
  91          /**
  92           * Constructor.
  93           *
  94           * @param $code Code follows the Java naming scheme: language_COUNTRY, so
  95           * for example if we want to have the texts translated in the English spoken
  96           * in the UK, we'd have to use en_UK as the code. The two digits country
  97           * code and language code are ISO standards and can be found in
  98           * http://userpage.chemie.fu-berlin.de/diverse/doc/ISO_3166.html (country codes) and
  99           * http://userpage.chemie.fu-berlin.de/diverse/doc/ISO_639.html (language codes)
 100           */
 101  		function Locale( $code )
 102          {    
 103              $this->_code = $code;
 104  
 105              $this->_loadLocaleInfo();
 106  
 107              if( $this->_charset == "" )
 108                  $this->_charset = DEFAULT_ENCODING;
 109  
 110              $this->_dataLoaded = false;
 111          }
 112  
 113          /**
 114           * @private
 115           */
 116  		function _loadLocaleFile()
 117          {            
 118              $this->_defaultFolder = $this->getLocaleFolder();
 119    
 120              // load the blog locale
 121              $fileName = $this->_defaultFolder."/locale_".$this->_code.".php";
 122              if( File::isReadable( $fileName )){
 123                      // TODO: this function is only called once, right?
 124                  include( $fileName );
 125              }
 126              if ( !isset($messages) || !is_array( $messages ) ) {
 127                  $messages = array();
 128              }
 129              $this->_messages = $messages;
 130              
 131              // load the admin locale
 132              $fileName = $this->_defaultFolder."/admin/locale_".$this->_code.".php";
 133              if( File::isReadable( $fileName )){
 134                      // TODO: this function is only called once, right?
 135                  include( $fileName );
 136              }
 137              if ( !isset($messages) || !is_array( $messages ) ) {
 138                  $messages = array();
 139              }
 140              $this->_messages = array_merge( $this->_messages, $messages );
 141              
 142              $this->_dataLoaded = true;
 143  
 144              unset($messages);
 145          }
 146          
 147          /**
 148           * @private
 149           * Loads the locale file, extracts the information needed and frees the used memory
 150           */
 151  		function _loadLocaleInfo()
 152          {
 153  
 154              if( !is_array($this->_messages) ) {
 155                  // load the locale into $this->_messages
 156                  $this->_loadLocaleFile();
 157              }
 158              
 159              // get the info that we need
 160              $this->_description = isset($this->_messages["locale_description"]) ? $this->_messages["locale_description"] : "no description for " . $this->_code;
 161              $this->_charset = isset($this->_messages["encoding"]) ? $this->_messages["encoding"] : DEFAULT_ENCODING;
 162              $this->_direction = isset($this->_messages["direction"]) ? $this->_messages["direction"] : DEFAULT_DIRECTION;
 163              $this->_dateFormat = isset($this->_messages["date_format"]) ? $this->_messages["date_format"] : DEFAULT_LOCALE_FORMAT;
 164              $this->_firstDayOfWeek = isset($this->_messages["first_day_of_week"]) ? $this->_messages["first_day_of_week"] : DEFAULT_FIRST_DAY_OF_WEEK;
 165              
 166              unset( $this->_messages );
 167              
 168              $this->_messages = NULL;
 169              
 170              return( true );
 171          }
 172  
 173          /**
 174           * Returns the character encoding method used by the current locale file. It has to be a valid
 175           * character encoding, since it will be used in the header of the html file to tell the browser
 176           * which is the most suitable encoding that should be used.
 177           *
 178           * @return A valid character encoding method.
 179           */
 180          function getCharset()
 181          {
 182              return $this->_charset;
 183          }
 184          
 185          /** 
 186           * returns the direction in which this language is written.
 187           * Possible values are, as with the html standard, "rtl" or "ltr"
 188           *
 189           */
 190  		function getDirection()
 191          {
 192              $direction = $this->_direction;
 193              if( $direction != "rtl" )
 194                  $direction = "ltr";
 195                  
 196              return $direction;
 197          }
 198  
 199          /**
 200           * Returns an optional locale description string that can be included in the
 201           * locale file with the other texts.
 202           *
 203           * @return A string describing the locale file.
 204           */
 205          function getDescription()
 206          {
 207              return( $this->_description );
 208          }
 209  
 210          /**
 211           * @static
 212           */
 213          function getLocaleFolder()
 214          {
 215              $config =& Config::getConfig();
 216  
 217              $localeFolder = $config->getValue( "locale_folder" );
 218              if( $localeFolder == "" || $localeFolder == "./locale")
 219                  $localeFolder = DEFAULT_LOCALE_FOLDER;
 220  
 221              return $localeFolder;
 222          }
 223  
 224          /**
 225           * Changes the locale to something else than what we chose in the first place when
 226           * creating the object.
 227           *
 228           * @param code follows the same format as in the constructor.
 229           */
 230  		function setLocale( $code )
 231          {
 232              $this->_code = $code;
 233  
 234              $this->_loadLocaleFile();
 235          }
 236  
 237          /**
 238           * returns all the strings
 239           *
 240           * @return An array containing all the strings that this locale supports
 241           */
 242  		function getStrings()
 243          {
 244              // load the file if it hadn't been loaded yet        
 245              if( !is_array($this->_messages))
 246                  $this->_loadLocaleFile();
 247          
 248              return $this->_messages;
 249          }
 250  
 251          /**
 252           * Translates a string
 253           *
 254           * @param id Identifier of the message we would like to translate
 255           */
 256          function tr( $id )
 257          {
 258              // load the file if it hasn't been loaded yet
 259              if( !$this->_dataLoaded ) {
 260                  $this->_loadLocaleFile();
 261              }
 262  
 263              isset( $this->_messages[$id] ) ? $string = $this->_messages[$id] : $string = $id;
 264              if( $string == "" ) $string = $id;
 265  
 266              return $string;
 267          }
 268  
 269          /**
 270           * Alias for getString
 271           * @see getString
 272           */
 273  		function i18n( $id )
 274          {
 275              return $this->getString( $id );
 276          }
 277  
 278          /**
 279           * calls printf on the translated string.
 280           *
 281           * Crappy Crappy! Since it only accepts three arguments... ;) Well, if we
 282           * ever need more than three, I'll change it!
 283           * @private
 284           */
 285          function pr( $id, $arg1 = null, $arg2 = null, $arg3 = null )
 286          {
 287              // first of all, we translate the string
 288              $str = $this->tr( $id );
 289              if( $arg1 == null )
 290                  $result = $str;
 291              else if( $arg2 == null )
 292                  $result = sprintf( $str, $arg1 );
 293              else if( $arg3 == null )
 294                  $result = sprintf( $str, $arg1, $arg2 );
 295              else
 296                  $result = sprintf( $str, $arg1, $arg2, $arg3 );
 297  
 298              return $result;
 299          }
 300  
 301          /**
 302           * Returns the complete code
 303           *
 304           * @return The Locale code
 305           */
 306          function getLocaleCode()
 307          {
 308              return $this->_code;
 309          }
 310  
 311          /**
 312           * Returns the two-character language code
 313           *
 314           * @return The two-character language code
 315           */
 316  		function getLanguageId()
 317          {
 318              $countryId = substr( $this->_code, 3, 5 ); 
 319              if ( strcmp( $countryId, "TW" ) == 0 || strcmp( $countryId, "CN") ==0 ) { 
 320                  $langId = substr( $this->_code, 0, 2 ); 
 321                  return $langId."-".$countryId;
 322              } 
 323              else { 
 324                  return substr( $this->_code, 0, 2 ); 
 325              } 
 326          }
 327  
 328          /**
 329           * Returns the two-character country code
 330           *
 331           * @return The two-character country code.
 332           */
 333  		function getCountryId()
 334          {
 335              return substr( $this->_code, 3, 5 );
 336          }
 337  
 338          /**
 339           * Returns the first day of the week, which also depends on the country
 340           *
 341           * @return Returns 0 for Sunday, 1 for Monday and so on...
 342           */
 343          function firstDayOfWeek()
 344          {
 345              return $this->_firstDayOfWeek;
 346          }
 347  
 348          /**
 349           * Returns all the months of the year
 350           *
 351           * @return Returns an array containing the names of the months, where the
 352           * first one is January.
 353           */
 354          function getMonthNames()
 355          {
 356              // load the file if it hadn't been loaded yet        
 357              if( !is_array($this->_messages))
 358                  $this->_loadLocaleFile();
 359          
 360              return $this->_messages["months"];
 361          }
 362  
 363          /**
 364           * Returns the days of the week
 365           *
 366           * @return Returns the names of the days of the week, where the first one is
 367           * Sunday.
 368           */
 369          function getDayNames()
 370          {
 371              // load the file if it hadn't been loaded yet        
 372              if( !is_array($this->_messages))
 373                  $this->_loadLocaleFile();
 374          
 375              return $this->_messages["days"];
 376          }
 377  
 378          /**
 379           * Returns the shorter version of the days of the week
 380           *
 381           * @return Returns an array with the days of the week abbreviated, where the first
 382           * one is Sunday.
 383           */
 384          function getDayNamesShort()
 385          {
 386              // load the file if it hadn't been loaded yet        
 387              if( !is_array($this->_messages))
 388                  $this->_loadLocaleFile();        
 389          
 390              return $this->_messages["daysshort"];
 391          }
 392          
 393  		function _getOrdinal( $num )
 394          {
 395              // first we check the last two digits
 396              $last_two_digits = substr( $num, -2 );
 397              if( $last_two_digits == "11" )
 398                  $value = $num."th";
 399              elseif( $last_two_digits == "12" )
 400                  $value = $num."th";
 401              elseif( $last_two_digits == "13" )
 402                  $value = $num."th";
 403              else {
 404                  // we get the last digit
 405                  $last_digit = substr( $num, -1 );
 406                  
 407                  if( $num < 10 )
 408                      $num = $last_digit;
 409                  
 410                  if( $last_digit == "1" )
 411                      $value = $num."st";
 412                  elseif( $last_digit == "2" )
 413                      $value = $num."nd";
 414                  elseif( $last_digit == "3" )
 415                      $value = $num."rd";
 416                  else
 417                      $value = $num."th";
 418              }
 419              
 420              return $value;
 421          }        
 422          
 423          /**
 424           * Returns the day in an ordinal format, i.e. 1st, 2nd, 3rd, etc (in English)
 425           *
 426           * @return A string with the ordinal representation of the day.
 427           */
 428  		function getDateOrdinal( $date )
 429          {
 430              $dayOrdinal = $date;
 431              $last_digit = substr( $dayOrdinal, -1 );
 432              if( $dayOrdinal < 10 )
 433                  $dayOrdinal = $last_digit;
 434              
 435              switch( $this->getLanguageId()) {
 436                  case "es":
 437                  case "ca":
 438                      break;
 439                  case "de":
 440                  case "fi":
 441                      $dayOrdinal .= "."; break;
 442                  case "en":
 443                  default:
 444                      $dayOrdinal = $this->_getOrdinal( $date); break;
 445              }
 446              
 447              return $dayOrdinal;
 448          }    
 449  
 450  		function getDayOrdinal( $t )
 451          {
 452              $dayOrdinal = $t->getDay();
 453              return $this->getDateOrdinal( $dayOrdinal );
 454          }    
 455  
 456  
 457          /**
 458           * Formats the date of a Timestamp object according to the given format:
 459           *
 460           * (compatible with PHP):<ul>
 461           * <li>%a abbreviated weekday</li>
 462           * <li>%A    complete weekday</li>
 463           * <li>%b    abbreviated month</li>
 464           * <li>%B    long month</li>
 465           * <li>%d    day of the month, 2 digits with leading zero</li>
 466           * <li>%j   day of the month, numeric (without leading zero)</li>
 467           * <li>%H    hours, in 24-h format</li>
 468           * <li>%I    hours, in 12-h format (without leading zero)</li>
 469           * <li>%p   returns 'am' or 'pm'</li>
 470           * <li>%P   returns 'AM' or 'PM'</li>
 471           * <li>%M    minutes</li>
 472           * <li>%m    month number, from 00 to 12</li>
 473           * <li>%S    seconds</li>
 474           * <li>%y    2-digit year representation</li>
 475           * <li>%Y    4-digit year representation</li>
 476           * <li>%O   Difference to Greenwich time (GMT) in hours</li>
 477           * <li>%%    the '%' character
 478           * </ul>
 479           * (these have been added by myself and are therefore incompatible with php)<ul>
 480           * <li>%T    "_day_ of _month_", where the day is in ordinal form and 'month' is the name of the month</li>
 481           * <li>%D    cardinal representation of the day</li>
 482           * </ul>
 483           */
 484  		function formatDate( $timeStamp, $format = null, $blog = null )
 485          {
 486              // load the file if it hadn't been loaded yet        
 487              if( !is_array($this->_messages))
 488                  $this->_loadLocaleFile();    
 489          
 490              // if the user did not specify a format, let's use the default one
 491              if( $format == null )
 492                  $format = $this->_dateFormat;
 493                  
 494              // Get the unix time stamp 
 495              $time = $timeStamp->getTimestamp(DATE_FORMAT_UNIXTIME);
 496              $timeZoneSec = date("Z", $time);
 497              if ( $blog ) {
 498                  //
 499                  // The blog was specified.  Use it to get the time offset
 500                  //
 501                  $timeDiff = 0;
 502                  $blogSettings = $blog->getSettings();
 503                  $timeDiff = $blogSettings->getValue( 'time_offset' );
 504  
 505                  // The following line relies on the fact that the result will
 506                  // be an int.
 507                  $timeZoneSec += ( $timeDiff * 3600 );
 508              }                
 509                  
 510              $text = $format;
 511                  
 512              if( strpos( $text, "%a" ) !== FALSE ) {
 513                  $weekdayId = $timeStamp->getWeekdayId();
 514                  $weekday = $this->_messages["days"][$weekdayId];
 515                  if( !empty( $this->_messages["weekdaysshort"] ) )
 516                      $shortWeekday = $this->_messages["weekdaysshort"][$weekdayId];
 517                  else
 518                      $shortWeekday = function_exists('html_entity_decode') ? htmlentities(substr(html_entity_decode($weekday), 0, 3 )) : substr($weekday, 0, 3);    
 519                  $text = str_replace( "%a", $shortWeekday, $text );
 520              }
 521              if( strpos( $text, "%A" ) !== FALSE ) {
 522                  $weekdayId = $timeStamp->getWeekdayId();    
 523                  $text = str_replace( "%A", $this->_messages["days"][$weekdayId], $text );    
 524              }
 525              if( strpos( $text, "%b" ) !== FALSE ) {
 526                  $monthId    = (int)$timeStamp->getMonth();
 527                  $monthStr   = $this->_messages["months"][$monthId-1];
 528                  if( !empty( $this->_messages["monthsshort"] ) )
 529                      $shortMonthStr = $this->_messages["monthsshort"][$monthId-1];
 530                  else
 531                      $shortMonthStr = function_exists('html_entity_decode') ? htmlentities(substr(html_entity_decode($monthStr), 0, 3 )) : substr($monthStr, 0, 3);
 532  
 533                  $text = str_replace( "%b", $shortMonthStr, $text );                
 534              }
 535              if( strpos( $text, "%B" ) !== FALSE ) {
 536                  $monthId = (int)$timeStamp->getMonth();
 537                  $text = str_replace( "%B", $this->_messages["months"][$monthId-1], $text );                
 538              }    
 539              if( strpos( $text, "%d" ) !== FALSE ) {
 540                  $text = str_replace( "%d", ($timeStamp->getDay() < 10) ? "0".$timeStamp->getDay() : $timeStamp->getDay(), $text );
 541              }
 542              if( strpos( $text, "%e" ) !== FALSE ) {
 543                  $text = str_replace( "%e", intval($timeStamp->getDay()), $text );
 544              }
 545              if( strpos( $text, "%j" ) !== FALSE ) {
 546                  $text = str_replace( "%j", $timeStamp->getDay(), $text );
 547              }
 548              if( strpos( $text, "%H" ) !== FALSE ) {
 549                  $text = str_replace( "%H", $timeStamp->getHour(), $text );                
 550              }            
 551              if( strpos( $text, "%I" ) !== FALSE ) {
 552                  $text = str_replace( "%I", ($timeStamp->getHour() != 0) ? ($timeStamp->getHour() > 12) ? $timeStamp->getHour()-12 : $timeStamp->getHour()+0 : 12, $text );    
 553              }
 554              if( strpos( $text, "%p" ) !== FALSE ) {
 555                  $text = str_replace( "%p", $timeStamp->getHour() >= 12 ? "pm" : "am", $text );                    
 556              }
 557              if( strpos( $text, "%P" ) !== FALSE ) {
 558                  $text = str_replace( "%P", $timeStamp->getHour() >= 12 ? "PM" : "AM", $text );                
 559              }
 560              if( strpos( $text, "%M" ) !== FALSE ) {
 561                  $text = str_replace( "%M",  $timeStamp->getMinutes(), $text );                    
 562              }
 563              if( strpos( $text, "%m" ) !== FALSE ) {
 564                  $text = str_replace( "%m", $timeStamp->getMonth(), $text );                
 565              }
 566              if( strpos( $text, "%S" ) !== FALSE ) {
 567                  $text = str_replace( "%S", $timeStamp->getSeconds(), $text );                    
 568              }
 569              if( strpos( $text, "%y" ) !== FALSE ) {
 570                  $text = str_replace( "%y", substr($timeStamp->getYear(), 2, 4 ), $text );                                    
 571              }
 572              if( strpos( $text, "%Y" ) !== FALSE ) {
 573                  $text = str_replace( "%Y", $timeStamp->getYear(), $text );                
 574              }
 575              if( strpos( $text, "%O" ) !== FALSE ) {
 576                  // Now convert the time zone seconds to hours and minutes
 577                  $timeZoneHours = intval( abs($timeZoneSec) / 3600 );
 578                  $timeZoneMins = intval(( abs($timeZoneSec) % 3600 ) / 60 );
 579                  $timeZoneDirection = ($timeZoneSec < 0 ) ? "-" : "+";                
 580                  
 581                  $text = str_replace( "%O", sprintf( "%s%02d%02d", $timeZoneDirection, $timeZoneHours, $timeZoneMins ), $text );                
 582              }                    
 583              if( strpos( $text, "%%" ) !== FALSE ) {
 584                  $text = str_replace( "%%", "%", $text );                
 585              }
 586              if( strpos( $text, "%T" ) !== FALSE ) {
 587                  $monthId    = (int)$timeStamp->getMonth();
 588                  $monthStr   = $this->_messages["months"][$monthId-1];                
 589      
 590                  $text = str_replace( "%T", $this->getDayOrdinal( $timeStamp )." ".$this->tr("of")." ".$monthStr, $text );                
 591              }            
 592              if( strpos( $text, "%D" ) !== FALSE ) {
 593                  $text = str_replace( "%D", $this->getDayOrdinal( $timeStamp ), $text );                
 594              }
 595              
 596              if ( $this->_code == 'fa_IR' )
 597              {            
 598                  lt_include( PLOG_CLASS_PATH."class/data/jalalicalendar.class.php" );
 599                  list( $jyear, $jmonth, $jday ) = JalaliCalendar::gregorian_to_jalali(gmdate( "Y", $time ), gmdate( "m", $time ), gmdate( "d", $time ));
 600      
 601                  if( strpos( $text, "%q" ) !== FALSE ) {
 602                        $text = str_replace( "%q", JalaliCalendar::Convertnumber2farsi($jyear), $text );                
 603                  }            
 604                  if( strpos( $text, "%w" ) !== FALSE ) {
 605                        $text = str_replace( "%w", JalaliCalendar::Convertnumber2farsi($jmonth), $text );
 606                  }            
 607                  if( strpos( $text, "%o" ) !== FALSE ) {
 608                        $text = str_replace( "%o", JalaliCalendar::Convertnumber2farsi($jday), $text );
 609                  }            
 610                  if( strpos( $text, "%R" ) !== FALSE ) {
 611                        $text = str_replace( "%R", JalaliCalendar::monthname($jmonth), $text );
 612                  }            
 613                  if( strpos( $text, "%T" ) !== FALSE ) {
 614                        $text = str_replace( "%T", JalaliCalendar::Convertnumber2farsi($timeStamp->getHour()), $text );                    
 615                  }            
 616                  if( strpos( $text, "%U" ) !== FALSE ) {
 617                        $text = str_replace( "%U", JalaliCalendar::Convertnumber2farsi($timeStamp->getMinutes()), $text );
 618                  }
 619              }            
 620  
 621              return $text;
 622          }
 623          
 624          /**
 625           * Formats the date of a Timestamp object according to the given format:
 626           * This function assumes that the timestamp is local and it converts it
 627           * to GMT before formatting.
 628           *
 629           * (compatible with PHP):<ul>
 630           * <li>%a abbreviated weekday</li>
 631           * <li>%A    complete weekday</li>
 632           * <li>%b    abbreviated month</li>
 633           * <li>%B    long month</li>
 634           * <li>%d    day of the month, 2 digits with leading zero</li>
 635           * <li>%j   day of the month, numeric (without leading zero)</li>
 636           * <li>%H    hours, in 24-h format</li>
 637           * <li>%I    hours, in 12-h format (without leading zero)</li>
 638           * <li>%p   returns 'am' or 'pm'</li>
 639           * <li>%P   returns 'AM' or 'PM'</li>
 640           * <li>%M    minutes</li>
 641           * <li>%m    month number, from 00 to 12</li>
 642           * <li>%S    seconds</li>
 643           * <li>%y    2-digit year representation</li>
 644           * <li>%Y    4-digit year representation</li>
 645           * <li>%O   Difference to Greenwich time (GMT) in hours (Will always be +0000)</li>
 646           * <li>%%    the '%' character
 647           * </ul>
 648           * (these have been added by myself and are therefore incompatible with php)<ul>
 649           * <li>%T    "_day_ of _month_", where the day is in ordinal form and 'month' is the name of the month</li>
 650           * <li>%D    cardinal representation of the day</li>
 651           * </ul>
 652           */
 653          function formatDateGMT( $timeStamp, $format = null, $blog = null )
 654          {
 655      
 656  
 657              // load the file if it hadn't been loaded yet        
 658              if( !is_array($this->_messages))
 659                  $this->_loadLocaleFile();    
 660          
 661              // if the user did not specify a format, let's use the default one
 662              if( $format == null )
 663                  $format = $this->_dateFormat;
 664                  
 665              // Get the unix time stamp 
 666              $time = $timeStamp->getTimestamp(DATE_FORMAT_UNIXTIME);
 667              $timeZoneSec = date("Z", $time);
 668              if ( $blog ) {
 669                  //
 670                  // The blog was specified.  Use it to get the time offset
 671                  //
 672                  $timeDiff = 0;
 673                  $blogSettings = $blog->getSettings();
 674                  $timeDiff = $blogSettings->getValue( 'time_offset' );
 675                  $timeDiff *= -1;
 676                  
 677                  if( $timeDiff > 0 )
 678                      $timeStamp->addSeconds( $timeDiff * 3600 );
 679                  else
 680                      $timeStamp->subtractSeconds( $timeDiff * (-3600));
 681              }                
 682                  
 683              $text = $format;                
 684                  
 685              if( strpos( $text, "%a" )  !== FALSE ) {
 686                  $weekdayId = gmdate( "w", $time );
 687                  $weekday = $this->_messages["days"][$weekdayId];
 688                  if( !empty( $this->_messages["weekdaysshort"] ) )
 689                      $shortWeekday = $this->_messages["weekdaysshort"][$weekdayId];
 690                  else
 691                      $shortWeekday = function_exists('html_entity_decode') ? htmlentities(substr(html_entity_decode($weekday), 0, 3 )) : substr($weekday, 0, 3);
 692      
 693                  $text = str_replace( "%a", $shortWeekday, $text );
 694              }
 695              if( strpos( $text, "%A" ) !== FALSE ) {
 696                  $weekdayId = gmdate( "w", $time );
 697                  $text = str_replace( "%A", $this->_messages["days"][$weekdayId], $text );    
 698              }
 699              if( strpos( $text, "%b" ) !== FALSE ) {
 700                  $monthId = gmdate( "n", $time );
 701                  $monthStr   = $this->_messages["months"][$monthId-1];
 702                  if( !empty( $this->_messages["monthsshort"] ) )
 703                      $shortMonthStr = $this->_messages["monthsshort"][$monthId-1];
 704                  else
 705                      $shortMonthStr = function_exists('html_entity_decode') ? htmlentities(substr(html_entity_decode($monthStr), 0, 3 )) : substr($monthStr, 0, 3);
 706  
 707                  $text = str_replace( "%b", $shortMonthStr, $text );                
 708              }
 709              if( strpos( $text, "%B" ) !== FALSE ) {
 710                  $monthId = gmdate( "n", $time );
 711                  $text = str_replace( "%B", $this->_messages["months"][$monthId-1], $text );                
 712              }    
 713              if( strpos( $text, "%d" ) !== FALSE ) {
 714                  $text = str_replace( "%d", gmdate( "d", $time ), $text );
 715              }
 716              if( strpos( $text, "%e" ) !== FALSE ) {
 717                  $text = str_replace( "%e", intval(gmdate( "d", $time )), $text );
 718              }
 719              if( strpos( $text, "%j" ) !== FALSE ) {
 720                  $text = str_replace( "%j", gmdate( "j", $time ), $text );
 721              }
 722              if( strpos( $text, "%H" ) !== FALSE ) {
 723                  $text = str_replace( "%H", gmdate( "H", $time ), $text );                
 724              }            
 725              if( strpos( $text, "%I" ) !== FALSE ) {
 726                  $text = str_replace( "%I", gmdate( "g", $time ), $text );    
 727              }
 728              if( strpos( $text, "%p" ) !== FALSE ) {
 729                  $text = str_replace( "%p", gmdate( "a", $time ), $text );                    
 730              }
 731              if( strpos( $text, "%P" ) !== FALSE ) {
 732                  $text = str_replace( "%P", gmdate( "A", $time ), $text );                
 733              }
 734              if( strpos( $text, "%M" ) !== FALSE ) {
 735                  $text = str_replace( "%M",  gmdate( "i", $time ), $text );                    
 736              }
 737              if( strpos( $text, "%m") !== FALSE ) {
 738                  $text = str_replace( "%m", gmdate( "m", $time ), $text );                
 739              }
 740              if( strpos( $text, "%S" ) !== FALSE ) {
 741                  $text = str_replace( "%S", gmdate( "s", $time ), $text );                    
 742              }
 743              if( strpos( $text, "%y" ) !== FALSE ) {
 744                  $text = str_replace( "%y", gmdate( "y", $time ), $text );                                    
 745              }
 746              if( strpos( $text, "%Y" ) !== FALSE ) {
 747                  $text = str_replace( "%Y", gmdate( "Y", $time ), $text );                
 748              }
 749              if( strpos( $text, "%O" ) !== FALSE ) {
 750                  $text = str_replace( "%O", "+0000", $text );                
 751              }                    
 752              if( strpos( $text, "%%" ) !== FALSE ) {
 753                  $text = str_replace( "%%", "%", $text );
 754              }
 755              if( strpos( $text, "%T" ) !== FALSE ) {
 756                  $monthId    = (int)$timeStamp->getMonth();
 757                  $monthStr   = $this->_messages["months"][$monthId-1];                
 758      
 759                  $text = str_replace( "%T", $this->getDateOrdinal( gmdate( "d", $time ))." ".$this->tr("of")." ".$monthStr, $text );                
 760              }            
 761              if( strpos( $text, "%D" ) !== FALSE ) {
 762                  $text = str_replace( "%D", $this->getDateOrdinal( gmdate( "d", $time )), $text );
 763              }
 764              
 765              if ( $this->_code == 'fa_IR' )
 766              {            
 767                  lt_include( PLOG_CLASS_PATH."class/data/jalalicalendar.class.php" );
 768                  list( $jyear, $jmonth, $jday ) = JalaliCalendar::gregorian_to_jalali(gmdate( "Y", $time ), gmdate( "m", $time ), gmdate( "d", $time ));
 769      
 770                  if( strpos( $text, "%q" ) !== FALSE ) {
 771                        $text = str_replace( "%q", JalaliCalendar::Convertnumber2farsi($jyear), $text );                
 772                  }            
 773                  if( strpos( $text, "%w" ) !== FALSE ) {
 774                        $text = str_replace( "%w", JalaliCalendar::Convertnumber2farsi($jmonth), $text );
 775                  }            
 776                  if( strpos( $text, "%o" ) !== FALSE ) {
 777                        $text = str_replace( "%o", JalaliCalendar::Convertnumber2farsi($jday), $text );
 778                  }            
 779                  if( strpos( $text, "%R" ) !== FALSE ) {
 780                        $text = str_replace( "%R", JalaliCalendar::monthname($jmonth), $text );
 781                  }            
 782                  if( strpos( $text, "%T" ) !== FALSE ) {
 783                        $text = str_replace( "%T", JalaliCalendar::Convertnumber2farsi(gmdate( "H", $time )), $text );
 784                  }            
 785                  if( strpos( $text, "%U" ) !== FALSE ) {
 786                        $text = str_replace( "%U", JalaliCalendar::Convertnumber2farsi(gmdate( "i", $time )), $text );
 787                  }
 788              }            
 789  
 790              return $text;
 791          }
 792          
 793          /**
 794           * Formats a date as an RFC 822 date timestamp.
 795           * @see formatDate
 796           */
 797          function formatDateAsRFC822( $timestamp, $blog = null )
 798          {
 799              $locale = $this->getLocaleCode();
 800              $this->setLocale( "en_UK" );
 801              $rfc822Date = $this->formatDate( $timestamp, "%a, %d %b %Y %H:%M:%S %O", $blog );
 802              $this->setLocale( $locale );
 803              
 804              return( $rfc822Date );
 805          }
 806          
 807          /**
 808           * merges two locales
 809           */
 810  		function mergeLocale( $locale )
 811          {
 812              if(isset($locale)){
 813                  // load the file if it hadn't been loaded yet        
 814                  if( !is_array($this->_messages))
 815                      $this->_loadLocaleFile();        
 816          
 817                  $this->_messages = array_merge( $this->_messages, $locale->getStrings());
 818                  return true;
 819              }
 820              else{
 821                  return false;
 822              }
 823          }
 824      }
 825  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics