[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-forums/bbpress/bb-includes/ -> functions.bb-l10n.php (source)

   1  <?php
   2  /**
   3   * bbPress Translation API
   4   *
   5   * @package bbPress
   6   * @subpackage i18n
   7   */
   8  
   9  /**
  10   * Gets the current locale.
  11   *
  12   * If the locale is set, then it will filter the locale in the 'locale' filter
  13   * hook and return the value.
  14   *
  15   * If the locale is not set already, then the BB_LANG constant is used if it is
  16   * defined. Then it is filtered through the 'locale' filter hook and the value
  17   * for the locale global set and the locale is returned.
  18   *
  19   * The process to get the locale should only be done once but the locale will
  20   * always be filtered using the 'locale' hook.
  21   *
  22   * @since 0.7.2
  23   * @uses apply_filters() Calls 'locale' hook on locale value.
  24   * @uses $locale Gets the locale stored in the global.
  25   *
  26   * @return string The locale of the blog or from the 'locale' hook.
  27   */
  28  function bb_get_locale() {
  29      global $locale;
  30  
  31      if (isset($locale))
  32          return apply_filters( 'locale', $locale );
  33  
  34      // BB_LANG is defined in bb-config.php
  35      if (defined('BB_LANG'))
  36          $locale = BB_LANG;
  37  
  38      if (empty($locale))
  39          $locale = 'en_US';
  40  
  41      return apply_filters('locale', $locale);
  42  }
  43  
  44  if ( !function_exists( 'get_locale' ) ) :
  45  function get_locale() {
  46      return bb_get_locale();
  47  }
  48  endif;
  49  
  50  if ( !function_exists( 'translate' ) ) :
  51  /**
  52   * Retrieves the translation of $text. If there is no translation, or
  53   * the domain isn't loaded the original text is returned.
  54   *
  55   * @see __() Don't use translate() directly, use __()
  56   * @since 1.0
  57   * @uses apply_filters() Calls 'gettext' on domain translated text
  58   *        with the untranslated text as second parameter.
  59   *
  60   * @param string $text Text to translate.
  61   * @param string $domain Domain to retrieve the translated text.
  62   * @return string Translated text
  63   */
  64  function translate( $text, $domain = 'default' ) {
  65      $translations = &get_translations_for_domain( $domain );
  66      return apply_filters('gettext', $translations->translate($text), $text, $domain);
  67  }
  68  endif;
  69  
  70  if ( !function_exists( 'before_last_bar' ) ) :
  71  /**
  72   * @since 1.0
  73   */
  74  function before_last_bar( $string ) {
  75      $last_bar = strrpos( $string, '|' );
  76      if ( false == $last_bar )
  77          return $string;
  78      else
  79          return substr( $string, 0, $last_bar );
  80  }
  81  endif;
  82  
  83  if ( !function_exists( 'translate_with_context' ) ) :
  84  /**
  85   * Translate $text like translate(), but assumes that the text
  86   * contains a context after its last vertical bar.
  87   *
  88   * @since 1.0
  89   * @uses translate()
  90   *
  91   * @param string $text Text to translate
  92   * @param string $domain Domain to retrieve the translated text
  93   * @return string Translated text
  94   */
  95  function translate_with_context( $text, $domain = 'default' ) {
  96      return before_last_bar( translate( $text, $domain ) );
  97  
  98  }
  99  endif;
 100  
 101  if ( !function_exists( 'translate_with_gettext_context' ) ) :
 102  /**
 103   * @since 1.0
 104   */
 105  function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
 106      $translations = &get_translations_for_domain( $domain );
 107      return apply_filters( 'gettext_with_context', $translations->translate( $text, $context ), $text, $context, $domain);
 108  }
 109  endif;
 110  
 111  if ( !function_exists( '__' ) ) :
 112  /**
 113   * Retrieves the translation of $text. If there is no translation, or
 114   * the domain isn't loaded the original text is returned.
 115   *
 116   * @see translate() An alias of translate()
 117   * @since 0.7.2
 118   *
 119   * @param string $text Text to translate
 120   * @param string $domain Optional. Domain to retrieve the translated text
 121   * @return string Translated text
 122   */
 123  function __( $text, $domain = 'default' ) {
 124      return translate( $text, $domain );
 125  }
 126  endif;
 127  
 128  if ( !function_exists( 'esc_attr__' ) ) :
 129  /**
 130   * Retrieves the translation of $text and escapes it for safe use in an attribute.
 131   * If there is no translation, or the domain isn't loaded the original text is returned.
 132   *
 133   * @see translate() An alias of translate()
 134   * @see esc_attr()
 135   * @since 1.0
 136   *
 137   * @param string $text Text to translate
 138   * @param string $domain Optional. Domain to retrieve the translated text
 139   * @return string Translated text
 140   */
 141  function esc_attr__( $text, $domain = 'default' ) {
 142      return esc_attr( translate( $text, $domain ) );
 143  }
 144  endif;
 145  
 146  if ( !function_exists( 'esc_html__' ) ) :
 147  /**
 148   * Retrieves the translation of $text and escapes it for safe use in HTML output.
 149   * If there is no translation, or the domain isn't loaded the original text is returned.
 150   *
 151   * @see translate() An alias of translate()
 152   * @see esc_html()
 153   * @since 1.0
 154   *
 155   * @param string $text Text to translate
 156   * @param string $domain Optional. Domain to retrieve the translated text
 157   * @return string Translated text
 158   */
 159  function esc_html__( $text, $domain = 'default' ) {
 160      return esc_html( translate( $text, $domain ) );
 161  }
 162  endif;
 163  
 164  if ( !function_exists( '_e' ) ) :
 165  /**
 166   * Displays the returned translated text from translate().
 167   *
 168   * @see translate() Echos returned translate() string
 169   * @since 0.7.2
 170   *
 171   * @param string $text Text to translate
 172   * @param string $domain Optional. Domain to retrieve the translated text
 173   */
 174  function _e( $text, $domain = 'default' ) {
 175      echo translate( $text, $domain );
 176  }
 177  endif;
 178  
 179  if ( !function_exists( 'esc_attr_e' ) ) :
 180  /**
 181   * Displays translated text that has been escaped for safe use in an attribute.
 182   *
 183   * @see translate() Echoes returned translate() string
 184   * @see esc_attr()
 185   * @since 1.0
 186   *
 187   * @param string $text Text to translate
 188   * @param string $domain Optional. Domain to retrieve the translated text
 189   */
 190  function esc_attr_e( $text, $domain = 'default' ) {
 191      echo esc_attr( translate( $text, $domain ) );
 192  }
 193  endif;
 194  
 195  if ( !function_exists( 'esc_html_e' ) ) :
 196  /**
 197   * Displays translated text that has been escaped for safe use in HTML output.
 198   *
 199   * @see translate() Echoes returned translate() string
 200   * @see esc_html()
 201   * @since 1.0
 202   *
 203   * @param string $text Text to translate
 204   * @param string $domain Optional. Domain to retrieve the translated text
 205   */
 206  function esc_html_e( $text, $domain = 'default' ) {
 207      echo esc_html( translate( $text, $domain ) );
 208  }
 209  endif;
 210  
 211  if ( !function_exists( '_c' ) ) :
 212  /**
 213   * Retrieve translated string with vertical bar context
 214   *
 215   * Quite a few times, there will be collisions with similar translatable text
 216   * found in more than two places but with different translated context.
 217   *
 218   * In order to use the separate contexts, the _c() function is used and the
 219   * translatable string uses a pipe ('|') which has the context the string is in.
 220   *
 221   * When the translated string is returned, it is everything before the pipe, not
 222   * including the pipe character. If there is no pipe in the translated text then
 223   * everything is returned.
 224   *
 225   * @since 1.0
 226   *
 227   * @param string $text Text to translate
 228   * @param string $domain Optional. Domain to retrieve the translated text
 229   * @return string Translated context string without pipe
 230   */
 231  function _c($text, $domain = 'default') {
 232      return translate_with_context($text, $domain);
 233  }
 234  endif;
 235  
 236  if ( !function_exists( '_x' ) ) :
 237  /**
 238   * @since 1.0
 239   */
 240  function _x( $single, $context, $domain = 'default' ) {
 241      return translate_with_gettext_context( $single, $context, $domain );
 242  }
 243  endif;
 244  
 245  if ( !function_exists( 'esc_attr_x' ) ) :
 246  function esc_attr_x( $single, $context, $domain = 'default' ) {
 247      return esc_attr( translate_with_gettext_context( $single, $context, $domain ) );
 248  }
 249  endif;
 250  
 251  if ( !function_exists( '__ngettext' ) ) :
 252  /**
 253   * @deprecated Use _n()
 254   */
 255  function __ngettext() {
 256      //_deprecated_function( __FUNCTION__, '2.8', '_n()' );
 257      $args = func_get_args();
 258      return call_user_func_array('_n', $args);
 259  }
 260  endif;
 261  
 262  if ( !function_exists( '_n' ) ) :
 263  /**
 264   * Retrieve the plural or single form based on the amount.
 265   *
 266   * If the domain is not set in the $l10n list, then a comparsion will be made
 267   * and either $plural or $single parameters returned.
 268   *
 269   * If the domain does exist, then the parameters $single, $plural, and $number
 270   * will first be passed to the domain's ngettext method. Then it will be passed
 271   * to the 'ngettext' filter hook along with the same parameters. The expected
 272   * type will be a string.
 273   *
 274   * @since 1.0
 275   * @uses $l10n Gets list of domain translated string (gettext_reader) objects
 276   * @uses apply_filters() Calls 'ngettext' hook on domains text returned,
 277   *        along with $single, $plural, and $number parameters. Expected to return string.
 278   *
 279   * @param string $single The text that will be used if $number is 1
 280   * @param string $plural The text that will be used if $number is not 1
 281   * @param int $number The number to compare against to use either $single or $plural
 282   * @param string $domain Optional. The domain identifier the text should be retrieved in
 283   * @return string Either $single or $plural translated text
 284   */
 285  function _n($single, $plural, $number, $domain = 'default') {
 286      $translations = &get_translations_for_domain( $domain );
 287      $translation = $translations->translate_plural( $single, $plural, $number );
 288      return apply_filters( 'ngettext', $translation, $single, $plural, $number );
 289  }
 290  endif;
 291  
 292  if ( !function_exists( '_nc' ) ) :
 293  /**
 294   * @see _n() A version of _n(), which supports contexts --
 295   * strips everything from the translation after the last bar
 296   * @since 1.0
 297   */
 298  function _nc( $single, $plural, $number, $domain = 'default' ) {
 299      return before_last_bar( _n( $single, $plural, $number, $domain ) );
 300  }
 301  endif;
 302  
 303  if ( !function_exists( '_nx' ) ) :
 304  /**
 305   * @since 1.0
 306   */
 307  function _nx($single, $plural, $number, $context, $domain = 'default') {
 308      $translations = &get_translations_for_domain( $domain );
 309      $translation = $translations->translate_plural( $single, $plural, $number, $context );
 310      return apply_filters( 'ngettext_with_context ', $translation, $single, $plural, $number, $context );
 311  }
 312  endif;
 313  
 314  if ( !function_exists( '__ngettext_noop' ) ) :
 315  /**
 316   * @deprecated Use _n_noop()
 317   */
 318  function __ngettext_noop() {
 319      //_deprecated_function( __FUNCTION__, '2.8', '_n_noop()' );
 320      $args = func_get_args();
 321      return call_user_func_array('_n_noop', $args);
 322  }
 323  endif;
 324  
 325  if ( !function_exists( '_n_noop' ) ) :
 326  /**
 327   * Register plural strings in POT file, but don't translate them.
 328   *
 329   * Used when you want do keep structures with translatable plural strings and
 330   * use them later.
 331   *
 332   * Example:
 333   *  $messages = array(
 334   *      'post' => _n_noop('%s post', '%s posts'),
 335   *      'page' => _n_noop('%s pages', '%s pages')
 336   *  );
 337   *  ...
 338   *  $message = $messages[$type];
 339   *  $usable_text = sprintf(_n($message[0], $message[1], $count), $count);
 340   *
 341   * @since 1.0
 342   * @param $single Single form to be i18ned
 343   * @param $plural Plural form to be i18ned
 344   * @return array array($single, $plural)
 345   */
 346  function _n_noop( $single, $plural ) {
 347      return array( $single, $plural );
 348  }
 349  endif;
 350  
 351  if ( !function_exists( '_nx_noop' ) ) :
 352  /**
 353   * Register plural strings with context in POT file, but don't translate them.
 354   *
 355   * @see _n_noop()
 356   */
 357  function _nx_noop( $single, $plural, $context ) {
 358      return array( $single, $plural, $context );
 359  }
 360  endif;
 361  
 362  if ( !function_exists( 'load_textdomain' ) ) :
 363  /**
 364   * Loads MO file into the list of domains.
 365   *
 366   * If the domain already exists, the inclusion will fail. If the MO file is not
 367   * readable, the inclusion will fail.
 368   *
 369   * On success, the mofile will be placed in the $l10n global by $domain and will
 370   * be an gettext_reader object.
 371   *
 372   * @since 0.7.2
 373   * @uses $l10n Gets list of domain translated string (gettext_reader) objects
 374   * @uses CacheFileReader Reads the MO file
 375   * @uses gettext_reader Allows for retrieving translated strings
 376   *
 377   * @param string $domain Unique identifier for retrieving translated strings
 378   * @param string $mofile Path to the .mo file
 379   * @return null On failure returns null and also on success returns nothing.
 380   */
 381  function load_textdomain($domain, $mofile) {
 382      global $l10n;
 383  
 384      if ( !is_readable($mofile)) return;
 385      
 386      $mo = new MO();
 387      $mo->import_from_file( $mofile );
 388  
 389      if (isset($l10n[$domain]))
 390          $mo->merge_with( $l10n[$domain] );
 391          
 392      $l10n[$domain] = &$mo;
 393  }
 394  endif;
 395  
 396  /**
 397   * Loads default translated strings based on locale.
 398   *
 399   * Loads the .mo file in WP_LANG_DIR constant path from WordPress root. The
 400   * translated (.mo) file is named based off of the locale.
 401   *
 402   * @since 1.5.0
 403   */
 404  function bb_load_default_textdomain() {
 405      $locale = bb_get_locale();
 406  
 407      $mofile = BB_LANG_DIR . "/$locale.mo";
 408  
 409      load_textdomain('default', $mofile);
 410  }
 411  
 412  if ( !function_exists( 'load_default_textdomain' ) ) :
 413  function load_default_textdomain() {
 414      bb_load_default_textdomain();
 415  }
 416  endif;
 417  
 418  /**
 419   * Loads the plugin's translated strings.
 420   *
 421   * If the path is not given then it will be the root of the plugin directory.
 422   * The .mo file should be named based on the domain with a dash followed by a
 423   * dash, and then the locale exactly.
 424   *
 425   * @since 0.7.2
 426   *
 427   * @param string $domain Unique identifier for retrieving translated strings
 428   * @param string $path Optional. Absolute path to folder where the .mo file resides
 429   */
 430  function bb_load_plugin_textdomain($domain, $path = false) {
 431      $locale = bb_get_locale();
 432  
 433      if ( false === $path ) {
 434          global $bb;
 435          $path = $bb->plugin_locations['core']['dir'];
 436      }
 437  
 438      $mofile = rtrim( trim( $path ), " \t\n\r\0\x0B/" ) . '/'. $domain . '-' . $locale . '.mo';
 439      load_textdomain($domain, $mofile);
 440  }
 441  
 442  if ( !function_exists( 'load_plugin_textdomain' ) ) :
 443  function load_plugin_textdomain( $domain, $path = false ) {
 444      bb_load_plugin_textdomain( $domain, $path );
 445  }
 446  endif;
 447  
 448  /**
 449   * Loads the theme's translated strings.
 450   *
 451   * If the current locale exists as a .mo file in the theme's root directory, it
 452   * will be included in the translated strings by the $domain.
 453   *
 454   * The .mo files must be named based on the locale exactly.
 455   *
 456   * @since 0.7.2
 457   *
 458   * @param string $domain Unique identifier for retrieving translated strings
 459   */
 460  function bb_load_theme_textdomain($domain, $path = false) {
 461      $locale = bb_get_locale();
 462  
 463      $mofile = ( empty( $path ) ) ? bb_get_template( $locale . '.mo' ) : "$path/$locale.mo";
 464      
 465      load_textdomain($domain, $mofile);
 466  }
 467  
 468  if ( !function_exists( 'load_theme_textdomain' ) ) :
 469  function load_theme_textdomain( $domain, $path = false ) {
 470      bb_load_theme_textdomain( $domain, $path );
 471  }
 472  endif;
 473  
 474  if ( !function_exists( 'get_translations_for_domain' ) ) :
 475  /**
 476   * Returns the Translations instance for a domain. If there isn't one,
 477   * returns empty Translations instance.
 478   *
 479   * @param string $domain
 480   * @return object A Translation instance
 481   */
 482  function &get_translations_for_domain( $domain ) {
 483      global $l10n;
 484      $empty = new Translations;
 485      if ( isset($l10n[$domain]) )
 486          return $l10n[$domain];
 487      else
 488          return $empty;
 489  }
 490  endif;


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