[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-forums/bbpress/bb-includes/ -> class.bb-locale.php (source)

   1  <?php
   2  // Last sync [WP11537]
   3  
   4  /**
   5   * Date and Time Locale object
   6   *
   7   * @package bbPress
   8   * @subpackage i18n
   9   */
  10  
  11  /**
  12   * Class that loads the calendar locale.
  13   */
  14  class BB_Locale {
  15      /**
  16       * Stores the translated strings for the full weekday names.
  17       *
  18       * @var array
  19       * @access private
  20       */
  21      var $weekday;
  22  
  23      /**
  24       * Stores the translated strings for the one character weekday names.
  25       *
  26       * There is a hack to make sure that Tuesday and Thursday, as well
  27       * as Sunday and Saturday don't conflict. See init() method for more.
  28       *
  29       * @see BB_Locale::init() for how to handle the hack.
  30       *
  31       * @var array
  32       * @access private
  33       */
  34      var $weekday_initial;
  35  
  36      /**
  37       * Stores the translated strings for the abbreviated weekday names.
  38       *
  39       * @var array
  40       * @access private
  41       */
  42      var $weekday_abbrev;
  43  
  44      /**
  45       * Stores the translated strings for the full month names.
  46       *
  47       * @var array
  48       * @access private
  49       */
  50      var $month;
  51  
  52      /**
  53       * Stores the translated strings for the abbreviated month names.
  54       *
  55       * @var array
  56       * @access private
  57       */
  58      var $month_abbrev;
  59  
  60      /**
  61       * Stores the translated strings for 'am' and 'pm'.
  62       *
  63       * Also the capitalized versions.
  64       *
  65       * @var array
  66       * @access private
  67       */
  68      var $meridiem;
  69  
  70      /**
  71       * Stores number formatting rules.
  72       *
  73       * @var array
  74       * @access public
  75       */
  76      var $number_format;
  77  
  78      /**
  79       * Stores date and time formatting strings.
  80       *
  81       * @var array
  82       * @access public
  83       */
  84      var $datetime_formatstring;
  85  
  86      /**
  87       * The text direction of the locale language.
  88       *
  89       * Default is left to right 'ltr'.
  90       *
  91       * @var string
  92       * @access private
  93       */
  94      var $text_direction = '';
  95  
  96      /**
  97       * Imports the global version to the class property.
  98       *
  99       * @var array
 100       * @access private
 101       */
 102      var $locale_vars = array('text_direction');
 103  
 104      /**
 105       * Sets up the translated strings and object properties.
 106       *
 107       * The method creates the translatable strings for various
 108       * calendar elements. Which allows for specifying locale
 109       * specific calendar names and text direction.
 110       *
 111       * @access private
 112       */
 113  	function init() {
 114          // The Weekdays
 115          $this->weekday[0] = __('Sunday');
 116          $this->weekday[1] = __('Monday');
 117          $this->weekday[2] = __('Tuesday');
 118          $this->weekday[3] = __('Wednesday');
 119          $this->weekday[4] = __('Thursday');
 120          $this->weekday[5] = __('Friday');
 121          $this->weekday[6] = __('Saturday');
 122  
 123          // The first letter of each day.  The _%day%_initial suffix is a hack to make
 124          // sure the day initials are unique.
 125          $this->weekday_initial[__('Sunday')]    = __('S_Sunday_initial');
 126          $this->weekday_initial[__('Monday')]    = __('M_Monday_initial');
 127          $this->weekday_initial[__('Tuesday')]   = __('T_Tuesday_initial');
 128          $this->weekday_initial[__('Wednesday')] = __('W_Wednesday_initial');
 129          $this->weekday_initial[__('Thursday')]  = __('T_Thursday_initial');
 130          $this->weekday_initial[__('Friday')]    = __('F_Friday_initial');
 131          $this->weekday_initial[__('Saturday')]  = __('S_Saturday_initial');
 132  
 133          foreach ($this->weekday_initial as $weekday_ => $weekday_initial_) {
 134              $this->weekday_initial[$weekday_] = preg_replace('/_.+_initial$/', '', $weekday_initial_);
 135          }
 136  
 137          // Abbreviations for each day.
 138          $this->weekday_abbrev[__('Sunday')]    = __('Sun');
 139          $this->weekday_abbrev[__('Monday')]    = __('Mon');
 140          $this->weekday_abbrev[__('Tuesday')]   = __('Tue');
 141          $this->weekday_abbrev[__('Wednesday')] = __('Wed');
 142          $this->weekday_abbrev[__('Thursday')]  = __('Thu');
 143          $this->weekday_abbrev[__('Friday')]    = __('Fri');
 144          $this->weekday_abbrev[__('Saturday')]  = __('Sat');
 145  
 146          // The Months
 147          $this->month['01'] = __('January');
 148          $this->month['02'] = __('February');
 149          $this->month['03'] = __('March');
 150          $this->month['04'] = __('April');
 151          $this->month['05'] = __('May');
 152          $this->month['06'] = __('June');
 153          $this->month['07'] = __('July');
 154          $this->month['08'] = __('August');
 155          $this->month['09'] = __('September');
 156          $this->month['10'] = __('October');
 157          $this->month['11'] = __('November');
 158          $this->month['12'] = __('December');
 159  
 160          // Abbreviations for each month. Uses the same hack as above to get around the
 161          // 'May' duplication.
 162          $this->month_abbrev[__('January')] = __('Jan_January_abbreviation');
 163          $this->month_abbrev[__('February')] = __('Feb_February_abbreviation');
 164          $this->month_abbrev[__('March')] = __('Mar_March_abbreviation');
 165          $this->month_abbrev[__('April')] = __('Apr_April_abbreviation');
 166          $this->month_abbrev[__('May')] = __('May_May_abbreviation');
 167          $this->month_abbrev[__('June')] = __('Jun_June_abbreviation');
 168          $this->month_abbrev[__('July')] = __('Jul_July_abbreviation');
 169          $this->month_abbrev[__('August')] = __('Aug_August_abbreviation');
 170          $this->month_abbrev[__('September')] = __('Sep_September_abbreviation');
 171          $this->month_abbrev[__('October')] = __('Oct_October_abbreviation');
 172          $this->month_abbrev[__('November')] = __('Nov_November_abbreviation');
 173          $this->month_abbrev[__('December')] = __('Dec_December_abbreviation');
 174  
 175          foreach ($this->month_abbrev as $month_ => $month_abbrev_) {
 176              $this->month_abbrev[$month_] = preg_replace('/_.+_abbreviation$/', '', $month_abbrev_);
 177          }
 178  
 179          // The Meridiems
 180          $this->meridiem['am'] = __('am');
 181          $this->meridiem['pm'] = __('pm');
 182          $this->meridiem['AM'] = __('AM');
 183          $this->meridiem['PM'] = __('PM');
 184  
 185          // Numbers formatting
 186          // See http://php.net/number_format
 187  
 188          /* translators: $decimals argument for http://php.net/number_format, default is 0 */
 189          $trans = __('number_format_decimals');
 190          $this->number_format['decimals'] = ('number_format_decimals' == $trans) ? 0 : $trans;
 191  
 192          /* translators: $dec_point argument for http://php.net/number_format, default is . */
 193          $trans = __('number_format_decimal_point');
 194          $this->number_format['decimal_point'] = ('number_format_decimal_point' == $trans) ? '.' : $trans;
 195  
 196          /* translators: $thousands_sep argument for http://php.net/number_format, default is , */
 197          $trans = __('number_format_thousands_sep');
 198          $this->number_format['thousands_sep'] = ('number_format_thousands_sep' == $trans) ? ',' : $trans;
 199          
 200          // Date/Time formatting
 201          $this->datetime_formatstring['datetime'] = __('F j, Y - h:i A');
 202          $this->datetime_formatstring['date'] = __('F j, Y');
 203          $this->datetime_formatstring['time'] = __('h:i A');
 204          
 205          $this->_load_locale_data();
 206      }
 207  
 208      /**
 209       * Imports global locale vars set during inclusion of $locale.php.
 210       *
 211       * @access private
 212       */
 213  	function _load_locale_data() {
 214          $locale = bb_get_locale();
 215          $locale_file = BB_LANG_DIR . $locale . '.php';
 216          if ( !is_file( $locale_file ) ) {
 217              return;
 218          }
 219  
 220          include( $locale_file );
 221  
 222          foreach ( $this->locale_vars as $var ) {
 223              $this->$var = $$var;
 224          }
 225      }
 226  
 227      /**
 228       * Retrieve the full translated weekday word.
 229       *
 230       * Week starts on translated Sunday and can be fetched
 231       * by using 0 (zero). So the week starts with 0 (zero)
 232       * and ends on Saturday with is fetched by using 6 (six).
 233       *
 234       * @access public
 235       *
 236       * @param int $weekday_number 0 for Sunday through 6 Saturday
 237       * @return string Full translated weekday
 238       */
 239  	function get_weekday($weekday_number) {
 240          return $this->weekday[$weekday_number];
 241      }
 242  
 243      /**
 244       * Retrieve the translated weekday initial.
 245       *
 246       * The weekday initial is retrieved by the translated
 247       * full weekday word. When translating the weekday initial
 248       * pay attention to make sure that the starting letter does
 249       * not conflict.
 250       *
 251       * @access public
 252       *
 253       * @param string $weekday_name
 254       * @return string
 255       */
 256  	function get_weekday_initial($weekday_name) {
 257          return $this->weekday_initial[$weekday_name];
 258      }
 259  
 260      /**
 261       * Retrieve the translated weekday abbreviation.
 262       *
 263       * The weekday abbreviation is retrieved by the translated
 264       * full weekday word.
 265       *
 266       * @access public
 267       *
 268       * @param string $weekday_name Full translated weekday word
 269       * @return string Translated weekday abbreviation
 270       */
 271  	function get_weekday_abbrev($weekday_name) {
 272          return $this->weekday_abbrev[$weekday_name];
 273      }
 274  
 275      /**
 276       * Retrieve the full translated month by month number.
 277       *
 278       * The $month_number parameter has to be a string
 279       * because it must have the '0' in front of any number
 280       * that is less than 10. Starts from '01' and ends at
 281       * '12'.
 282       *
 283       * You can use an integer instead and it will add the
 284       * '0' before the numbers less than 10 for you.
 285       *
 286       * @access public
 287       *
 288       * @param string|int $month_number '01' through '12'
 289       * @return string Translated full month name
 290       */
 291  	function get_month($month_number) {
 292          return $this->month[zeroise($month_number, 2)];
 293      }
 294  
 295  	function get_month_initial($month_name) {
 296          return $this->month_initial[$month_name];
 297      }
 298  
 299      /**
 300       * Retrieve translated version of month abbreviation string.
 301       *
 302       * The $month_name parameter is expected to be the translated or
 303       * translatable version of the month.
 304       *
 305       * @access public
 306       *
 307       * @param string $month_name Translated month to get abbreviated version
 308       * @return string Translated abbreviated month
 309       */
 310  	function get_month_abbrev($month_name) {
 311          return $this->month_abbrev[$month_name];
 312      }
 313  
 314      /**
 315       * Retrieve translated version of meridiem string.
 316       *
 317       * The $meridiem parameter is expected to not be translated.
 318       *
 319       * @access public
 320       *
 321       * @param string $meridiem Either 'am', 'pm', 'AM', or 'PM'. Not translated version.
 322       * @return string Translated version
 323       */
 324  	function get_meridiem($meridiem) {
 325          return $this->meridiem[$meridiem];
 326      }
 327  
 328  	function get_datetime_formatstring($type = 'datetime') {
 329          return $this->datetime_formatstring[$type];
 330      }
 331  
 332      /**
 333       * Global variables are deprecated. For backwards compatibility only.
 334       *
 335       * @deprecated For backwards compatibility only.
 336       * @access private
 337       */
 338  	function register_globals() {
 339          $GLOBALS['weekday']         = $this->weekday;
 340          $GLOBALS['weekday_initial'] = $this->weekday_initial;
 341          $GLOBALS['weekday_abbrev']  = $this->weekday_abbrev;
 342          $GLOBALS['month']           = $this->month;
 343          $GLOBALS['month_abbrev']    = $this->month_abbrev;
 344      }
 345  
 346      /**
 347       * PHP4 style constructor which calls helper methods to set up object variables
 348       *
 349       * @uses BB_Locale::init()
 350       * @uses BB_Locale::register_globals()
 351       *
 352       * @return BB_Locale
 353       */
 354  	function __construct() {
 355          $this->init();
 356          $this->register_globals();
 357      }
 358  
 359  	function BB_Locale() {
 360          $this->__construct();
 361      }
 362  }
 363  
 364  /**
 365   * Retrieve the date in localized format, based on timestamp.
 366   *
 367   * If the locale specifies the locale month and weekday, then the locale will
 368   * take over the format for the date. If it isn't, then the date format string
 369   * will be used instead.
 370   *
 371   * @param string $dateformatstring Format to display the date.
 372   * @param int $unixtimestamp Optional. Unix timestamp.
 373   * @param bool $gmt Optional, default is true. Whether to convert to GMT for time.
 374   * @return string The date, translated if locale specifies it.
 375   */
 376  function bb_gmdate_i18n( $dateformatstring, $unixtimestamp = false, $gmt = true ) {
 377      global $bb_locale;
 378      $i = $unixtimestamp;
 379      // Sanity check for PHP 5.1.0-
 380      if ( false === $i || intval($i) < 0 ) {
 381          if ( ! $gmt )
 382              $i = current_time( 'timestamp' );
 383          else
 384              $i = time();
 385          // we should not let date() interfere with our
 386          // specially computed timestamp
 387          $gmt = true;
 388      }
 389  
 390      // store original value for language with untypical grammars
 391      // see http://core.trac.wordpress.org/ticket/9396
 392      $req_format = $dateformatstring;
 393  
 394      $datefunc = $gmt? 'gmdate' : 'date';
 395  
 396      if ( ( !empty( $bb_locale->month ) ) && ( !empty( $bb_locale->weekday ) ) ) {
 397          $datemonth = $bb_locale->get_month( $datefunc( 'm', $i ) );
 398          $datemonth_abbrev = $bb_locale->get_month_abbrev( $datemonth );
 399          $dateweekday = $bb_locale->get_weekday( $datefunc( 'w', $i ) );
 400          $dateweekday_abbrev = $bb_locale->get_weekday_abbrev( $dateweekday );
 401          $datemeridiem = $bb_locale->get_meridiem( $datefunc( 'a', $i ) );
 402          $datemeridiem_capital = $bb_locale->get_meridiem( $datefunc( 'A', $i ) );
 403          $dateformatstring = ' '.$dateformatstring;
 404          $dateformatstring = preg_replace( "/([^\\\])D/", "\\1" . backslashit( $dateweekday_abbrev ), $dateformatstring );
 405          $dateformatstring = preg_replace( "/([^\\\])F/", "\\1" . backslashit( $datemonth ), $dateformatstring );
 406          $dateformatstring = preg_replace( "/([^\\\])l/", "\\1" . backslashit( $dateweekday ), $dateformatstring );
 407          $dateformatstring = preg_replace( "/([^\\\])M/", "\\1" . backslashit( $datemonth_abbrev ), $dateformatstring );
 408          $dateformatstring = preg_replace( "/([^\\\])a/", "\\1" . backslashit( $datemeridiem ), $dateformatstring );
 409          $dateformatstring = preg_replace( "/([^\\\])A/", "\\1" . backslashit( $datemeridiem_capital ), $dateformatstring );
 410  
 411          $dateformatstring = substr( $dateformatstring, 1, strlen( $dateformatstring ) -1 );
 412      }
 413      $j = @$datefunc( $dateformatstring, $i );
 414      // allow plugins to redo this entirely for languages with untypical grammars
 415      $j = apply_filters('bb_gmdate_i18n', $j, $req_format, $i, $gmt);
 416      return $j;
 417  }
 418  
 419  function bb_get_datetime_formatstring_i18n( $type = 'datetime' ) {
 420      $formatstring = bb_get_option( $type . '_format' );
 421      if ( empty($formatstring) ) {
 422          global $bb_locale;
 423          $formatstring = $bb_locale->get_datetime_formatstring( $type );
 424      }
 425      return $formatstring;
 426  }
 427  
 428  function bb_datetime_format_i18n( $unixtimestamp, $type = 'datetime', $formatstring = '', $gmt = true ) {
 429      if ( empty($formatstring) ) {
 430          $formatstring = bb_get_datetime_formatstring_i18n( $type );
 431      }
 432      return bb_gmdate_i18n( $formatstring, bb_offset_time( $unixtimestamp ), $gmt );
 433  }
 434  
 435  /**
 436   * Convert number to format based on the locale.
 437   *
 438   * @since 2.3.0
 439   *
 440   * @param mixed $number The number to convert based on locale.
 441   * @param int $decimals Precision of the number of decimal places.
 442   * @return string Converted number in string format.
 443   */
 444  function bb_number_format_i18n( $number, $decimals = null ) {
 445      global $bb_locale;
 446      // let the user override the precision only
 447      $decimals = ( is_null( $decimals ) ) ? $bb_locale->number_format['decimals'] : intval( $decimals );
 448  
 449      $num = number_format( $number, $decimals, $bb_locale->number_format['decimal_point'], $bb_locale->number_format['thousands_sep'] );
 450  
 451      // let the user translate digits from latin to localized language
 452      return apply_filters( 'bb_number_format_i18n', $num );
 453  }


Generated: Thu Dec 7 01:01:35 2017 Cross-referenced by PHPXref 0.7.1