[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> l10n.php (source)

   1  <?php
   2  /**
   3   * Core Translation API
   4   *
   5   * @package WordPress
   6   * @subpackage i18n
   7   * @since 1.2.0
   8   */
   9  
  10  /**
  11   * Retrieves the current locale.
  12   *
  13   * If the locale is set, then it will filter the locale in the {@see 'locale'}
  14   * filter hook and return the value.
  15   *
  16   * If the locale is not set already, then the WPLANG constant is used if it is
  17   * defined. Then it is filtered through the {@see 'locale'} filter hook and
  18   * the value for the locale global set and the locale is returned.
  19   *
  20   * The process to get the locale should only be done once, but the locale will
  21   * always be filtered using the {@see 'locale'} hook.
  22   *
  23   * @since 1.5.0
  24   *
  25   * @global string $locale           The current locale.
  26   * @global string $wp_local_package Locale code of the package.
  27   *
  28   * @return string The locale of the blog or from the {@see 'locale'} hook.
  29   */
  30  function get_locale() {
  31      global $locale, $wp_local_package;
  32  
  33      if ( isset( $locale ) ) {
  34          /** This filter is documented in wp-includes/l10n.php */
  35          return apply_filters( 'locale', $locale );
  36      }
  37  
  38      if ( isset( $wp_local_package ) ) {
  39          $locale = $wp_local_package;
  40      }
  41  
  42      // WPLANG was defined in wp-config.
  43      if ( defined( 'WPLANG' ) ) {
  44          $locale = WPLANG;
  45      }
  46  
  47      // If multisite, check options.
  48      if ( is_multisite() ) {
  49          // Don't check blog option when installing.
  50          if ( wp_installing() ) {
  51              $ms_locale = get_site_option( 'WPLANG' );
  52          } else {
  53              $ms_locale = get_option( 'WPLANG' );
  54              if ( false === $ms_locale ) {
  55                  $ms_locale = get_site_option( 'WPLANG' );
  56              }
  57          }
  58  
  59          if ( false !== $ms_locale ) {
  60              $locale = $ms_locale;
  61          }
  62      } else {
  63          $db_locale = get_option( 'WPLANG' );
  64          if ( false !== $db_locale ) {
  65              $locale = $db_locale;
  66          }
  67      }
  68  
  69      if ( empty( $locale ) ) {
  70          $locale = 'en_US';
  71      }
  72  
  73      /**
  74       * Filters the locale ID of the WordPress installation.
  75       *
  76       * @since 1.5.0
  77       *
  78       * @param string $locale The locale ID.
  79       */
  80      return apply_filters( 'locale', $locale );
  81  }
  82  
  83  /**
  84   * Retrieves the locale of a user.
  85   *
  86   * If the user has a locale set to a non-empty string then it will be
  87   * returned. Otherwise it returns the locale of get_locale().
  88   *
  89   * @since 4.7.0
  90   *
  91   * @param int|WP_User $user_id User's ID or a WP_User object. Defaults to current user.
  92   * @return string The locale of the user.
  93   */
  94  function get_user_locale( $user_id = 0 ) {
  95      $user = false;
  96      if ( 0 === $user_id && function_exists( 'wp_get_current_user' ) ) {
  97          $user = wp_get_current_user();
  98      } elseif ( $user_id instanceof WP_User ) {
  99          $user = $user_id;
 100      } elseif ( $user_id && is_numeric( $user_id ) ) {
 101          $user = get_user_by( 'id', $user_id );
 102      }
 103  
 104      if ( ! $user ) {
 105          return get_locale();
 106      }
 107  
 108      $locale = $user->locale;
 109      return $locale ? $locale : get_locale();
 110  }
 111  
 112  /**
 113   * Determine the current locale desired for the request.
 114   *
 115   * @since 5.0.0
 116   *
 117   * @global string $pagenow
 118   *
 119   * @return string The determined locale.
 120   */
 121  function determine_locale() {
 122      /**
 123       * Filters the locale for the current request prior to the default determination process.
 124       *
 125       * Using this filter allows to override the default logic, effectively short-circuiting the function.
 126       *
 127       * @since 5.0.0
 128       *
 129       * @param string|null $locale The locale to return and short-circuit. Default null.
 130       */
 131      $determined_locale = apply_filters( 'pre_determine_locale', null );
 132  
 133      if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) {
 134          return $determined_locale;
 135      }
 136  
 137      $determined_locale = get_locale();
 138  
 139      if ( is_admin() ) {
 140          $determined_locale = get_user_locale();
 141      }
 142  
 143      if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) {
 144          $determined_locale = get_user_locale();
 145      }
 146  
 147      if ( ! empty( $_GET['wp_lang'] ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
 148          $determined_locale = sanitize_text_field( $_GET['wp_lang'] );
 149      }
 150  
 151      /**
 152       * Filters the locale for the current request.
 153       *
 154       * @since 5.0.0
 155       *
 156       * @param string $locale The locale.
 157       */
 158      return apply_filters( 'determine_locale', $determined_locale );
 159  }
 160  
 161  /**
 162   * Retrieve the translation of $text.
 163   *
 164   * If there is no translation, or the text domain isn't loaded, the original text is returned.
 165   *
 166   * *Note:* Don't use translate() directly, use __() or related functions.
 167   *
 168   * @since 2.2.0
 169   * @since 5.5.0 Introduced gettext-{$domain} filter.
 170   *
 171   * @param string $text   Text to translate.
 172   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 173   *                       Default 'default'.
 174   * @return string Translated text.
 175   */
 176  function translate( $text, $domain = 'default' ) {
 177      $translations = get_translations_for_domain( $domain );
 178      $translation  = $translations->translate( $text );
 179  
 180      /**
 181       * Filters text with its translation.
 182       *
 183       * @since 2.0.11
 184       *
 185       * @param string $translation Translated text.
 186       * @param string $text        Text to translate.
 187       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 188       */
 189      $translation = apply_filters( 'gettext', $translation, $text, $domain );
 190  
 191      /**
 192       * Filters text with its translation for a domain.
 193       *
 194       * The dynamic portion of the hook, `$domain`, refers to the text domain.
 195       *
 196       * @since 5.5.0
 197       *
 198       * @param string $translation Translated text.
 199       * @param string $text        Text to translate.
 200       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 201       */
 202      $translation = apply_filters( "gettext_{$domain}", $translation, $text, $domain );
 203  
 204      return $translation;
 205  }
 206  
 207  /**
 208   * Remove last item on a pipe-delimited string.
 209   *
 210   * Meant for removing the last item in a string, such as 'Role name|User role'. The original
 211   * string will be returned if no pipe '|' characters are found in the string.
 212   *
 213   * @since 2.8.0
 214   *
 215   * @param string $string A pipe-delimited string.
 216   * @return string Either $string or everything before the last pipe.
 217   */
 218  function before_last_bar( $string ) {
 219      $last_bar = strrpos( $string, '|' );
 220      if ( false === $last_bar ) {
 221          return $string;
 222      } else {
 223          return substr( $string, 0, $last_bar );
 224      }
 225  }
 226  
 227  /**
 228   * Retrieve the translation of $text in the context defined in $context.
 229   *
 230   * If there is no translation, or the text domain isn't loaded, the original text is returned.
 231   *
 232   * *Note:* Don't use translate_with_gettext_context() directly, use _x() or related functions.
 233   *
 234   * @since 2.8.0
 235   * @since 5.5.0 Introduced gettext_with_context-{$domain} filter.
 236   *
 237   * @param string $text    Text to translate.
 238   * @param string $context Context information for the translators.
 239   * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
 240   *                        Default 'default'.
 241   * @return string Translated text on success, original text on failure.
 242   */
 243  function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
 244      $translations = get_translations_for_domain( $domain );
 245      $translation  = $translations->translate( $text, $context );
 246  
 247      /**
 248       * Filters text with its translation based on context information.
 249       *
 250       * @since 2.8.0
 251       *
 252       * @param string $translation Translated text.
 253       * @param string $text        Text to translate.
 254       * @param string $context     Context information for the translators.
 255       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 256       */
 257      $translation = apply_filters( 'gettext_with_context', $translation, $text, $context, $domain );
 258  
 259      /**
 260       * Filters text with its translation based on context information for a domain.
 261       *
 262       * The dynamic portion of the hook, `$domain`, refers to the text domain.
 263       *
 264       * @since 5.5.0
 265       *
 266       * @param string $translation Translated text.
 267       * @param string $text        Text to translate.
 268       * @param string $context     Context information for the translators.
 269       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 270       */
 271      $translation = apply_filters( "gettext_with_context_{$domain}", $translation, $text, $context, $domain );
 272  
 273      return $translation;
 274  }
 275  
 276  /**
 277   * Retrieve the translation of $text.
 278   *
 279   * If there is no translation, or the text domain isn't loaded, the original text is returned.
 280   *
 281   * @since 2.1.0
 282   *
 283   * @param string $text   Text to translate.
 284   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 285   *                       Default 'default'.
 286   * @return string Translated text.
 287   */
 288  function __( $text, $domain = 'default' ) {
 289      return translate( $text, $domain );
 290  }
 291  
 292  /**
 293   * Retrieve the translation of $text and escapes it for safe use in an attribute.
 294   *
 295   * If there is no translation, or the text domain isn't loaded, the original text is returned.
 296   *
 297   * @since 2.8.0
 298   *
 299   * @param string $text   Text to translate.
 300   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 301   *                       Default 'default'.
 302   * @return string Translated text on success, original text on failure.
 303   */
 304  function esc_attr__( $text, $domain = 'default' ) {
 305      return esc_attr( translate( $text, $domain ) );
 306  }
 307  
 308  /**
 309   * Retrieve the translation of $text and escapes it for safe use in HTML output.
 310   *
 311   * If there is no translation, or the text domain isn't loaded, the original text
 312   * is escaped and returned.
 313   *
 314   * @since 2.8.0
 315   *
 316   * @param string $text   Text to translate.
 317   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 318   *                       Default 'default'.
 319   * @return string Translated text.
 320   */
 321  function esc_html__( $text, $domain = 'default' ) {
 322      return esc_html( translate( $text, $domain ) );
 323  }
 324  
 325  /**
 326   * Display translated text.
 327   *
 328   * @since 1.2.0
 329   *
 330   * @param string $text   Text to translate.
 331   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 332   *                       Default 'default'.
 333   */
 334  function _e( $text, $domain = 'default' ) {
 335      echo translate( $text, $domain );
 336  }
 337  
 338  /**
 339   * Display translated text that has been escaped for safe use in an attribute.
 340   *
 341   * Encodes `< > & " '` (less than, greater than, ampersand, double quote, single quote).
 342   * Will never double encode entities.
 343   *
 344   * If you need the value for use in PHP, use esc_attr__().
 345   *
 346   * @since 2.8.0
 347   *
 348   * @param string $text   Text to translate.
 349   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 350   *                       Default 'default'.
 351   */
 352  function esc_attr_e( $text, $domain = 'default' ) {
 353      echo esc_attr( translate( $text, $domain ) );
 354  }
 355  
 356  /**
 357   * Display translated text that has been escaped for safe use in HTML output.
 358   *
 359   * If there is no translation, or the text domain isn't loaded, the original text
 360   * is escaped and displayed.
 361   *
 362   * If you need the value for use in PHP, use esc_html__().
 363   *
 364   * @since 2.8.0
 365   *
 366   * @param string $text   Text to translate.
 367   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 368   *                       Default 'default'.
 369   */
 370  function esc_html_e( $text, $domain = 'default' ) {
 371      echo esc_html( translate( $text, $domain ) );
 372  }
 373  
 374  /**
 375   * Retrieve translated string with gettext context.
 376   *
 377   * Quite a few times, there will be collisions with similar translatable text
 378   * found in more than two places, but with different translated context.
 379   *
 380   * By including the context in the pot file, translators can translate the two
 381   * strings differently.
 382   *
 383   * @since 2.8.0
 384   *
 385   * @param string $text    Text to translate.
 386   * @param string $context Context information for the translators.
 387   * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
 388   *                        Default 'default'.
 389   * @return string Translated context string without pipe.
 390   */
 391  function _x( $text, $context, $domain = 'default' ) {
 392      return translate_with_gettext_context( $text, $context, $domain );
 393  }
 394  
 395  /**
 396   * Display translated string with gettext context.
 397   *
 398   * @since 3.0.0
 399   *
 400   * @param string $text    Text to translate.
 401   * @param string $context Context information for the translators.
 402   * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
 403   *                        Default 'default'.
 404   */
 405  function _ex( $text, $context, $domain = 'default' ) {
 406      echo _x( $text, $context, $domain );
 407  }
 408  
 409  /**
 410   * Translate string with gettext context, and escapes it for safe use in an attribute.
 411   *
 412   * If there is no translation, or the text domain isn't loaded, the original text
 413   * is escaped and returned.
 414   *
 415   * @since 2.8.0
 416   *
 417   * @param string $text    Text to translate.
 418   * @param string $context Context information for the translators.
 419   * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
 420   *                        Default 'default'.
 421   * @return string Translated text.
 422   */
 423  function esc_attr_x( $text, $context, $domain = 'default' ) {
 424      return esc_attr( translate_with_gettext_context( $text, $context, $domain ) );
 425  }
 426  
 427  /**
 428   * Translate string with gettext context, and escapes it for safe use in HTML output.
 429   *
 430   * If there is no translation, or the text domain isn't loaded, the original text
 431   * is escaped and returned.
 432   *
 433   * @since 2.9.0
 434   *
 435   * @param string $text    Text to translate.
 436   * @param string $context Context information for the translators.
 437   * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
 438   *                        Default 'default'.
 439   * @return string Translated text.
 440   */
 441  function esc_html_x( $text, $context, $domain = 'default' ) {
 442      return esc_html( translate_with_gettext_context( $text, $context, $domain ) );
 443  }
 444  
 445  /**
 446   * Translates and retrieves the singular or plural form based on the supplied number.
 447   *
 448   * Used when you want to use the appropriate form of a string based on whether a
 449   * number is singular or plural.
 450   *
 451   * Example:
 452   *
 453   *     printf( _n( '%s person', '%s people', $count, 'text-domain' ), number_format_i18n( $count ) );
 454   *
 455   * @since 2.8.0
 456   * @since 5.5.0 Introduced ngettext-{$domain} filter.
 457   *
 458   * @param string $single The text to be used if the number is singular.
 459   * @param string $plural The text to be used if the number is plural.
 460   * @param int    $number The number to compare against to use either the singular or plural form.
 461   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 462   *                       Default 'default'.
 463   * @return string The translated singular or plural form.
 464   */
 465  function _n( $single, $plural, $number, $domain = 'default' ) {
 466      $translations = get_translations_for_domain( $domain );
 467      $translation  = $translations->translate_plural( $single, $plural, $number );
 468  
 469      /**
 470       * Filters the singular or plural form of a string.
 471       *
 472       * @since 2.2.0
 473       *
 474       * @param string $translation Translated text.
 475       * @param string $single      The text to be used if the number is singular.
 476       * @param string $plural      The text to be used if the number is plural.
 477       * @param string $number      The number to compare against to use either the singular or plural form.
 478       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 479       */
 480      $translation = apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
 481  
 482      /**
 483       * Filters the singular or plural form of a string for a domain.
 484       *
 485       * The dynamic portion of the hook, `$domain`, refers to the text domain.
 486       *
 487       * @since 5.5.0
 488       *
 489       * @param string $translation Translated text.
 490       * @param string $single      The text to be used if the number is singular.
 491       * @param string $plural      The text to be used if the number is plural.
 492       * @param string $number      The number to compare against to use either the singular or plural form.
 493       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 494       */
 495      $translation = apply_filters( "ngettext_{$domain}", $translation, $single, $plural, $number, $domain );
 496  
 497      return $translation;
 498  }
 499  
 500  /**
 501   * Translates and retrieves the singular or plural form based on the supplied number, with gettext context.
 502   *
 503   * This is a hybrid of _n() and _x(). It supports context and plurals.
 504   *
 505   * Used when you want to use the appropriate form of a string with context based on whether a
 506   * number is singular or plural.
 507   *
 508   * Example of a generic phrase which is disambiguated via the context parameter:
 509   *
 510   *     printf( _nx( '%s group', '%s groups', $people, 'group of people', 'text-domain' ), number_format_i18n( $people ) );
 511   *     printf( _nx( '%s group', '%s groups', $animals, 'group of animals', 'text-domain' ), number_format_i18n( $animals ) );
 512   *
 513   * @since 2.8.0
 514   * @since 5.5.0 Introduced ngettext_with_context-{$domain} filter.
 515   *
 516   * @param string $single  The text to be used if the number is singular.
 517   * @param string $plural  The text to be used if the number is plural.
 518   * @param int    $number  The number to compare against to use either the singular or plural form.
 519   * @param string $context Context information for the translators.
 520   * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
 521   *                        Default 'default'.
 522   * @return string The translated singular or plural form.
 523   */
 524  function _nx( $single, $plural, $number, $context, $domain = 'default' ) {
 525      $translations = get_translations_for_domain( $domain );
 526      $translation  = $translations->translate_plural( $single, $plural, $number, $context );
 527  
 528      /**
 529       * Filters the singular or plural form of a string with gettext context.
 530       *
 531       * @since 2.8.0
 532       *
 533       * @param string $translation Translated text.
 534       * @param string $single      The text to be used if the number is singular.
 535       * @param string $plural      The text to be used if the number is plural.
 536       * @param string $number      The number to compare against to use either the singular or plural form.
 537       * @param string $context     Context information for the translators.
 538       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 539       */
 540      $translation = apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
 541  
 542      /**
 543       * Filters the singular or plural form of a string with gettext context for a domain.
 544       *
 545       * The dynamic portion of the hook, `$domain`, refers to the text domain.
 546       *
 547       * @since 5.5.0
 548       *
 549       * @param string $translation Translated text.
 550       * @param string $single      The text to be used if the number is singular.
 551       * @param string $plural      The text to be used if the number is plural.
 552       * @param string $number      The number to compare against to use either the singular or plural form.
 553       * @param string $context     Context information for the translators.
 554       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 555       */
 556      $translation = apply_filters( "ngettext_with_context_{$domain}", $translation, $single, $plural, $number, $context, $domain );
 557  
 558      return $translation;
 559  }
 560  
 561  /**
 562   * Registers plural strings in POT file, but does not translate them.
 563   *
 564   * Used when you want to keep structures with translatable plural
 565   * strings and use them later when the number is known.
 566   *
 567   * Example:
 568   *
 569   *     $message = _n_noop( '%s post', '%s posts', 'text-domain' );
 570   *     ...
 571   *     printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
 572   *
 573   * @since 2.5.0
 574   *
 575   * @param string $singular Singular form to be localized.
 576   * @param string $plural   Plural form to be localized.
 577   * @param string $domain   Optional. Text domain. Unique identifier for retrieving translated strings.
 578   *                         Default null.
 579   * @return array {
 580   *     Array of translation information for the strings.
 581   *
 582   *     @type string $0        Singular form to be localized. No longer used.
 583   *     @type string $1        Plural form to be localized. No longer used.
 584   *     @type string $singular Singular form to be localized.
 585   *     @type string $plural   Plural form to be localized.
 586   *     @type null   $context  Context information for the translators.
 587   *     @type string $domain   Text domain.
 588   * }
 589   */
 590  function _n_noop( $singular, $plural, $domain = null ) {
 591      return array(
 592          0          => $singular,
 593          1          => $plural,
 594          'singular' => $singular,
 595          'plural'   => $plural,
 596          'context'  => null,
 597          'domain'   => $domain,
 598      );
 599  }
 600  
 601  /**
 602   * Registers plural strings with gettext context in POT file, but does not translate them.
 603   *
 604   * Used when you want to keep structures with translatable plural
 605   * strings and use them later when the number is known.
 606   *
 607   * Example of a generic phrase which is disambiguated via the context parameter:
 608   *
 609   *     $messages = array(
 610   *          'people'  => _nx_noop( '%s group', '%s groups', 'people', 'text-domain' ),
 611   *          'animals' => _nx_noop( '%s group', '%s groups', 'animals', 'text-domain' ),
 612   *     );
 613   *     ...
 614   *     $message = $messages[ $type ];
 615   *     printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
 616   *
 617   * @since 2.8.0
 618   *
 619   * @param string $singular Singular form to be localized.
 620   * @param string $plural   Plural form to be localized.
 621   * @param string $context  Context information for the translators.
 622   * @param string $domain   Optional. Text domain. Unique identifier for retrieving translated strings.
 623   *                         Default null.
 624   * @return array {
 625   *     Array of translation information for the strings.
 626   *
 627   *     @type string $0        Singular form to be localized. No longer used.
 628   *     @type string $1        Plural form to be localized. No longer used.
 629   *     @type string $2        Context information for the translators. No longer used.
 630   *     @type string $singular Singular form to be localized.
 631   *     @type string $plural   Plural form to be localized.
 632   *     @type string $context  Context information for the translators.
 633   *     @type string $domain   Text domain.
 634   * }
 635   */
 636  function _nx_noop( $singular, $plural, $context, $domain = null ) {
 637      return array(
 638          0          => $singular,
 639          1          => $plural,
 640          2          => $context,
 641          'singular' => $singular,
 642          'plural'   => $plural,
 643          'context'  => $context,
 644          'domain'   => $domain,
 645      );
 646  }
 647  
 648  /**
 649   * Translates and retrieves the singular or plural form of a string that's been registered
 650   * with _n_noop() or _nx_noop().
 651   *
 652   * Used when you want to use a translatable plural string once the number is known.
 653   *
 654   * Example:
 655   *
 656   *     $message = _n_noop( '%s post', '%s posts', 'text-domain' );
 657   *     ...
 658   *     printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
 659   *
 660   * @since 3.1.0
 661   *
 662   * @param array  $nooped_plural Array with singular, plural, and context keys, usually the result of _n_noop() or _nx_noop().
 663   * @param int    $count         Number of objects.
 664   * @param string $domain        Optional. Text domain. Unique identifier for retrieving translated strings. If $nooped_plural contains
 665   *                              a text domain passed to _n_noop() or _nx_noop(), it will override this value. Default 'default'.
 666   * @return string Either $single or $plural translated text.
 667   */
 668  function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) {
 669      if ( $nooped_plural['domain'] ) {
 670          $domain = $nooped_plural['domain'];
 671      }
 672  
 673      if ( $nooped_plural['context'] ) {
 674          return _nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain );
 675      } else {
 676          return _n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain );
 677      }
 678  }
 679  
 680  /**
 681   * Load a .mo file into the text domain $domain.
 682   *
 683   * If the text domain already exists, the translations will be merged. If both
 684   * sets have the same string, the translation from the original value will be taken.
 685   *
 686   * On success, the .mo file will be placed in the $l10n global by $domain
 687   * and will be a MO object.
 688   *
 689   * @since 1.5.0
 690   *
 691   * @global MO[] $l10n          An array of all currently loaded text domains.
 692   * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
 693   *
 694   * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 695   * @param string $mofile Path to the .mo file.
 696   * @return bool True on success, false on failure.
 697   */
 698  function load_textdomain( $domain, $mofile ) {
 699      global $l10n, $l10n_unloaded;
 700  
 701      $l10n_unloaded = (array) $l10n_unloaded;
 702  
 703      /**
 704       * Filters whether to override the .mo file loading.
 705       *
 706       * @since 2.9.0
 707       *
 708       * @param bool   $override Whether to override the .mo file loading. Default false.
 709       * @param string $domain   Text domain. Unique identifier for retrieving translated strings.
 710       * @param string $mofile   Path to the MO file.
 711       */
 712      $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile );
 713  
 714      if ( true === (bool) $plugin_override ) {
 715          unset( $l10n_unloaded[ $domain ] );
 716  
 717          return true;
 718      }
 719  
 720      /**
 721       * Fires before the MO translation file is loaded.
 722       *
 723       * @since 2.9.0
 724       *
 725       * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 726       * @param string $mofile Path to the .mo file.
 727       */
 728      do_action( 'load_textdomain', $domain, $mofile );
 729  
 730      /**
 731       * Filters MO file path for loading translations for a specific text domain.
 732       *
 733       * @since 2.9.0
 734       *
 735       * @param string $mofile Path to the MO file.
 736       * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 737       */
 738      $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
 739  
 740      if ( ! is_readable( $mofile ) ) {
 741          return false;
 742      }
 743  
 744      $mo = new MO();
 745      if ( ! $mo->import_from_file( $mofile ) ) {
 746          return false;
 747      }
 748  
 749      if ( isset( $l10n[ $domain ] ) ) {
 750          $mo->merge_with( $l10n[ $domain ] );
 751      }
 752  
 753      unset( $l10n_unloaded[ $domain ] );
 754  
 755      $l10n[ $domain ] = &$mo;
 756  
 757      return true;
 758  }
 759  
 760  /**
 761   * Unload translations for a text domain.
 762   *
 763   * @since 3.0.0
 764   *
 765   * @global MO[] $l10n          An array of all currently loaded text domains.
 766   * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
 767   *
 768   * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 769   * @return bool Whether textdomain was unloaded.
 770   */
 771  function unload_textdomain( $domain ) {
 772      global $l10n, $l10n_unloaded;
 773  
 774      $l10n_unloaded = (array) $l10n_unloaded;
 775  
 776      /**
 777       * Filters whether to override the text domain unloading.
 778       *
 779       * @since 3.0.0
 780       *
 781       * @param bool   $override Whether to override the text domain unloading. Default false.
 782       * @param string $domain   Text domain. Unique identifier for retrieving translated strings.
 783       */
 784      $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain );
 785  
 786      if ( $plugin_override ) {
 787          $l10n_unloaded[ $domain ] = true;
 788  
 789          return true;
 790      }
 791  
 792      /**
 793       * Fires before the text domain is unloaded.
 794       *
 795       * @since 3.0.0
 796       *
 797       * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 798       */
 799      do_action( 'unload_textdomain', $domain );
 800  
 801      if ( isset( $l10n[ $domain ] ) ) {
 802          unset( $l10n[ $domain ] );
 803  
 804          $l10n_unloaded[ $domain ] = true;
 805  
 806          return true;
 807      }
 808  
 809      return false;
 810  }
 811  
 812  /**
 813   * Load default translated strings based on locale.
 814   *
 815   * Loads the .mo file in WP_LANG_DIR constant path from WordPress root.
 816   * The translated (.mo) file is named based on the locale.
 817   *
 818   * @see load_textdomain()
 819   *
 820   * @since 1.5.0
 821   *
 822   * @param string $locale Optional. Locale to load. Default is the value of get_locale().
 823   * @return bool Whether the textdomain was loaded.
 824   */
 825  function load_default_textdomain( $locale = null ) {
 826      if ( null === $locale ) {
 827          $locale = determine_locale();
 828      }
 829  
 830      // Unload previously loaded strings so we can switch translations.
 831      unload_textdomain( 'default' );
 832  
 833      $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo" );
 834  
 835      if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists( WP_LANG_DIR . "/admin-$locale.mo" ) ) {
 836          load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo" );
 837          return $return;
 838      }
 839  
 840      if ( is_admin() || wp_installing() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) {
 841          load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo" );
 842      }
 843  
 844      if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) {
 845          load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo" );
 846      }
 847  
 848      return $return;
 849  }
 850  
 851  /**
 852   * Loads a plugin's translated strings.
 853   *
 854   * If the path is not given then it will be the root of the plugin directory.
 855   *
 856   * The .mo file should be named based on the text domain with a dash, and then the locale exactly.
 857   *
 858   * @since 1.5.0
 859   * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
 860   *
 861   * @param string       $domain          Unique identifier for retrieving translated strings
 862   * @param string|false $deprecated      Optional. Deprecated. Use the $plugin_rel_path parameter instead.
 863   *                                      Default false.
 864   * @param string|false $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides.
 865   *                                      Default false.
 866   * @return bool True when textdomain is successfully loaded, false otherwise.
 867   */
 868  function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) {
 869      /**
 870       * Filters a plugin's locale.
 871       *
 872       * @since 3.0.0
 873       *
 874       * @param string $locale The plugin's current locale.
 875       * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 876       */
 877      $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
 878  
 879      $mofile = $domain . '-' . $locale . '.mo';
 880  
 881      // Try to load from the languages directory first.
 882      if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ) ) {
 883          return true;
 884      }
 885  
 886      if ( false !== $plugin_rel_path ) {
 887          $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
 888      } elseif ( false !== $deprecated ) {
 889          _deprecated_argument( __FUNCTION__, '2.7.0' );
 890          $path = ABSPATH . trim( $deprecated, '/' );
 891      } else {
 892          $path = WP_PLUGIN_DIR;
 893      }
 894  
 895      return load_textdomain( $domain, $path . '/' . $mofile );
 896  }
 897  
 898  /**
 899   * Load the translated strings for a plugin residing in the mu-plugins directory.
 900   *
 901   * @since 3.0.0
 902   * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
 903   *
 904   * @param string $domain             Text domain. Unique identifier for retrieving translated strings.
 905   * @param string $mu_plugin_rel_path Optional. Relative to `WPMU_PLUGIN_DIR` directory in which the .mo
 906   *                                   file resides. Default empty string.
 907   * @return bool True when textdomain is successfully loaded, false otherwise.
 908   */
 909  function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
 910      /** This filter is documented in wp-includes/l10n.php */
 911      $locale = apply_filters( 'plugin_locale', determine_locale(), $domain );
 912  
 913      $mofile = $domain . '-' . $locale . '.mo';
 914  
 915      // Try to load from the languages directory first.
 916      if ( load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile ) ) {
 917          return true;
 918      }
 919  
 920      $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' );
 921  
 922      return load_textdomain( $domain, $path . '/' . $mofile );
 923  }
 924  
 925  /**
 926   * Load the theme's translated strings.
 927   *
 928   * If the current locale exists as a .mo file in the theme's root directory, it
 929   * will be included in the translated strings by the $domain.
 930   *
 931   * The .mo files must be named based on the locale exactly.
 932   *
 933   * @since 1.5.0
 934   * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
 935   *
 936   * @param string       $domain Text domain. Unique identifier for retrieving translated strings.
 937   * @param string|false $path   Optional. Path to the directory containing the .mo file.
 938   *                             Default false.
 939   * @return bool True when textdomain is successfully loaded, false otherwise.
 940   */
 941  function load_theme_textdomain( $domain, $path = false ) {
 942      /**
 943       * Filters a theme's locale.
 944       *
 945       * @since 3.0.0
 946       *
 947       * @param string $locale The theme's current locale.
 948       * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 949       */
 950      $locale = apply_filters( 'theme_locale', determine_locale(), $domain );
 951  
 952      $mofile = $domain . '-' . $locale . '.mo';
 953  
 954      // Try to load from the languages directory first.
 955      if ( load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile ) ) {
 956          return true;
 957      }
 958  
 959      if ( ! $path ) {
 960          $path = get_template_directory();
 961      }
 962  
 963      return load_textdomain( $domain, $path . '/' . $locale . '.mo' );
 964  }
 965  
 966  /**
 967   * Load the child themes translated strings.
 968   *
 969   * If the current locale exists as a .mo file in the child themes
 970   * root directory, it will be included in the translated strings by the $domain.
 971   *
 972   * The .mo files must be named based on the locale exactly.
 973   *
 974   * @since 2.9.0
 975   *
 976   * @param string       $domain Text domain. Unique identifier for retrieving translated strings.
 977   * @param string|false $path   Optional. Path to the directory containing the .mo file.
 978   *                             Default false.
 979   * @return bool True when the theme textdomain is successfully loaded, false otherwise.
 980   */
 981  function load_child_theme_textdomain( $domain, $path = false ) {
 982      if ( ! $path ) {
 983          $path = get_stylesheet_directory();
 984      }
 985      return load_theme_textdomain( $domain, $path );
 986  }
 987  
 988  /**
 989   * Loads the script translated strings.
 990   *
 991   * @since 5.0.0
 992   * @since 5.0.2 Uses load_script_translations() to load translation data.
 993   * @since 5.1.0 The `$domain` parameter was made optional.
 994   *
 995   * @see WP_Scripts::set_translations()
 996   *
 997   * @param string $handle Name of the script to register a translation domain to.
 998   * @param string $domain Optional. Text domain. Default 'default'.
 999   * @param string $path   Optional. The full file path to the directory containing translation files.
1000   * @return string|false The translated strings in JSON encoding on success,
1001   *                      false if the script textdomain could not be loaded.
1002   */
1003  function load_script_textdomain( $handle, $domain = 'default', $path = null ) {
1004      $wp_scripts = wp_scripts();
1005  
1006      if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
1007          return false;
1008      }
1009  
1010      $path   = untrailingslashit( $path );
1011      $locale = determine_locale();
1012  
1013      // If a path was given and the handle file exists simply return it.
1014      $file_base       = 'default' === $domain ? $locale : $domain . '-' . $locale;
1015      $handle_filename = $file_base . '-' . $handle . '.json';
1016  
1017      if ( $path ) {
1018          $translations = load_script_translations( $path . '/' . $handle_filename, $handle, $domain );
1019  
1020          if ( $translations ) {
1021              return $translations;
1022          }
1023      }
1024  
1025      $src = $wp_scripts->registered[ $handle ]->src;
1026  
1027      if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) {
1028          $src = $wp_scripts->base_url . $src;
1029      }
1030  
1031      $relative       = false;
1032      $languages_path = WP_LANG_DIR;
1033  
1034      $src_url     = wp_parse_url( $src );
1035      $content_url = wp_parse_url( content_url() );
1036      $plugins_url = wp_parse_url( plugins_url() );
1037      $site_url    = wp_parse_url( site_url() );
1038  
1039      // If the host is the same or it's a relative URL.
1040      if (
1041          ( ! isset( $content_url['path'] ) || strpos( $src_url['path'], $content_url['path'] ) === 0 ) &&
1042          ( ! isset( $src_url['host'] ) || ! isset( $content_url['host'] ) || $src_url['host'] === $content_url['host'] )
1043      ) {
1044          // Make the src relative the specific plugin or theme.
1045          if ( isset( $content_url['path'] ) ) {
1046              $relative = substr( $src_url['path'], strlen( $content_url['path'] ) );
1047          } else {
1048              $relative = $src_url['path'];
1049          }
1050          $relative = trim( $relative, '/' );
1051          $relative = explode( '/', $relative );
1052  
1053          $languages_path = WP_LANG_DIR . '/' . $relative[0];
1054  
1055          $relative = array_slice( $relative, 2 ); // Remove plugins/<plugin name> or themes/<theme name>.
1056          $relative = implode( '/', $relative );
1057      } elseif (
1058          ( ! isset( $plugins_url['path'] ) || strpos( $src_url['path'], $plugins_url['path'] ) === 0 ) &&
1059          ( ! isset( $src_url['host'] ) || ! isset( $plugins_url['host'] ) || $src_url['host'] === $plugins_url['host'] )
1060      ) {
1061          // Make the src relative the specific plugin.
1062          if ( isset( $plugins_url['path'] ) ) {
1063              $relative = substr( $src_url['path'], strlen( $plugins_url['path'] ) );
1064          } else {
1065              $relative = $src_url['path'];
1066          }
1067          $relative = trim( $relative, '/' );
1068          $relative = explode( '/', $relative );
1069  
1070          $languages_path = WP_LANG_DIR . '/plugins';
1071  
1072          $relative = array_slice( $relative, 1 ); // Remove <plugin name>.
1073          $relative = implode( '/', $relative );
1074      } elseif ( ! isset( $src_url['host'] ) || ! isset( $site_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
1075          if ( ! isset( $site_url['path'] ) ) {
1076              $relative = trim( $src_url['path'], '/' );
1077          } elseif ( ( strpos( $src_url['path'], trailingslashit( $site_url['path'] ) ) === 0 ) ) {
1078              // Make the src relative to the WP root.
1079              $relative = substr( $src_url['path'], strlen( $site_url['path'] ) );
1080              $relative = trim( $relative, '/' );
1081          }
1082      }
1083  
1084      /**
1085       * Filters the relative path of scripts used for finding translation files.
1086       *
1087       * @since 5.0.2
1088       *
1089       * @param string|false $relative The relative path of the script. False if it could not be determined.
1090       * @param string       $src      The full source URL of the script.
1091       */
1092      $relative = apply_filters( 'load_script_textdomain_relative_path', $relative, $src );
1093  
1094      // If the source is not from WP.
1095      if ( false === $relative ) {
1096          return load_script_translations( false, $handle, $domain );
1097      }
1098  
1099      // Translations are always based on the unminified filename.
1100      if ( substr( $relative, -7 ) === '.min.js' ) {
1101          $relative = substr( $relative, 0, -7 ) . '.js';
1102      }
1103  
1104      $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
1105  
1106      if ( $path ) {
1107          $translations = load_script_translations( $path . '/' . $md5_filename, $handle, $domain );
1108  
1109          if ( $translations ) {
1110              return $translations;
1111          }
1112      }
1113  
1114      $translations = load_script_translations( $languages_path . '/' . $md5_filename, $handle, $domain );
1115  
1116      if ( $translations ) {
1117          return $translations;
1118      }
1119  
1120      return load_script_translations( false, $handle, $domain );
1121  }
1122  
1123  /**
1124   * Loads the translation data for the given script handle and text domain.
1125   *
1126   * @since 5.0.2
1127   *
1128   * @param string|false $file   Path to the translation file to load. False if there isn't one.
1129   * @param string       $handle Name of the script to register a translation domain to.
1130   * @param string       $domain The text domain.
1131   * @return string|false The JSON-encoded translated strings for the given script handle and text domain.
1132   *                      False if there are none.
1133   */
1134  function load_script_translations( $file, $handle, $domain ) {
1135      /**
1136       * Pre-filters script translations for the given file, script handle and text domain.
1137       *
1138       * Returning a non-null value allows to override the default logic, effectively short-circuiting the function.
1139       *
1140       * @since 5.0.2
1141       *
1142       * @param string|false|null $translations JSON-encoded translation data. Default null.
1143       * @param string|false      $file         Path to the translation file to load. False if there isn't one.
1144       * @param string            $handle       Name of the script to register a translation domain to.
1145       * @param string            $domain       The text domain.
1146       */
1147      $translations = apply_filters( 'pre_load_script_translations', null, $file, $handle, $domain );
1148  
1149      if ( null !== $translations ) {
1150          return $translations;
1151      }
1152  
1153      /**
1154       * Filters the file path for loading script translations for the given script handle and text domain.
1155       *
1156       * @since 5.0.2
1157       *
1158       * @param string|false $file   Path to the translation file to load. False if there isn't one.
1159       * @param string       $handle Name of the script to register a translation domain to.
1160       * @param string       $domain The text domain.
1161       */
1162      $file = apply_filters( 'load_script_translation_file', $file, $handle, $domain );
1163  
1164      if ( ! $file || ! is_readable( $file ) ) {
1165          return false;
1166      }
1167  
1168      $translations = file_get_contents( $file );
1169  
1170      /**
1171       * Filters script translations for the given file, script handle and text domain.
1172       *
1173       * @since 5.0.2
1174       *
1175       * @param string $translations JSON-encoded translation data.
1176       * @param string $file         Path to the translation file that was loaded.
1177       * @param string $handle       Name of the script to register a translation domain to.
1178       * @param string $domain       The text domain.
1179       */
1180      return apply_filters( 'load_script_translations', $translations, $file, $handle, $domain );
1181  }
1182  
1183  /**
1184   * Loads plugin and theme textdomains just-in-time.
1185   *
1186   * When a textdomain is encountered for the first time, we try to load
1187   * the translation file from `wp-content/languages`, removing the need
1188   * to call load_plugin_texdomain() or load_theme_texdomain().
1189   *
1190   * @since 4.6.0
1191   * @access private
1192   *
1193   * @see get_translations_for_domain()
1194   * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
1195   *
1196   * @param string $domain Text domain. Unique identifier for retrieving translated strings.
1197   * @return bool True when the textdomain is successfully loaded, false otherwise.
1198   */
1199  function _load_textdomain_just_in_time( $domain ) {
1200      global $l10n_unloaded;
1201  
1202      $l10n_unloaded = (array) $l10n_unloaded;
1203  
1204      // Short-circuit if domain is 'default' which is reserved for core.
1205      if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) {
1206          return false;
1207      }
1208  
1209      $translation_path = _get_path_to_translation( $domain );
1210      if ( false === $translation_path ) {
1211          return false;
1212      }
1213  
1214      return load_textdomain( $domain, $translation_path );
1215  }
1216  
1217  /**
1218   * Gets the path to a translation file for loading a textdomain just in time.
1219   *
1220   * Caches the retrieved results internally.
1221   *
1222   * @since 4.7.0
1223   * @access private
1224   *
1225   * @see _load_textdomain_just_in_time()
1226   *
1227   * @param string $domain Text domain. Unique identifier for retrieving translated strings.
1228   * @param bool   $reset  Whether to reset the internal cache. Used by the switch to locale functionality.
1229   * @return string|false The path to the translation file or false if no translation file was found.
1230   */
1231  function _get_path_to_translation( $domain, $reset = false ) {
1232      static $available_translations = array();
1233  
1234      if ( true === $reset ) {
1235          $available_translations = array();
1236      }
1237  
1238      if ( ! isset( $available_translations[ $domain ] ) ) {
1239          $available_translations[ $domain ] = _get_path_to_translation_from_lang_dir( $domain );
1240      }
1241  
1242      return $available_translations[ $domain ];
1243  }
1244  
1245  /**
1246   * Gets the path to a translation file in the languages directory for the current locale.
1247   *
1248   * Holds a cached list of available .mo files to improve performance.
1249   *
1250   * @since 4.7.0
1251   * @access private
1252   *
1253   * @see _get_path_to_translation()
1254   *
1255   * @param string $domain Text domain. Unique identifier for retrieving translated strings.
1256   * @return string|false The path to the translation file or false if no translation file was found.
1257   */
1258  function _get_path_to_translation_from_lang_dir( $domain ) {
1259      static $cached_mofiles = null;
1260  
1261      if ( null === $cached_mofiles ) {
1262          $cached_mofiles = array();
1263  
1264          $locations = array(
1265              WP_LANG_DIR . '/plugins',
1266              WP_LANG_DIR . '/themes',
1267          );
1268  
1269          foreach ( $locations as $location ) {
1270              $mofiles = glob( $location . '/*.mo' );
1271              if ( $mofiles ) {
1272                  $cached_mofiles = array_merge( $cached_mofiles, $mofiles );
1273              }
1274          }
1275      }
1276  
1277      $locale = determine_locale();
1278      $mofile = "{$domain}-{$locale}.mo";
1279  
1280      $path = WP_LANG_DIR . '/plugins/' . $mofile;
1281      if ( in_array( $path, $cached_mofiles, true ) ) {
1282          return $path;
1283      }
1284  
1285      $path = WP_LANG_DIR . '/themes/' . $mofile;
1286      if ( in_array( $path, $cached_mofiles, true ) ) {
1287          return $path;
1288      }
1289  
1290      return false;
1291  }
1292  
1293  /**
1294   * Return the Translations instance for a text domain.
1295   *
1296   * If there isn't one, returns empty Translations instance.
1297   *
1298   * @since 2.8.0
1299   *
1300   * @global MO[] $l10n
1301   *
1302   * @param string $domain Text domain. Unique identifier for retrieving translated strings.
1303   * @return Translations|NOOP_Translations A Translations instance.
1304   */
1305  function get_translations_for_domain( $domain ) {
1306      global $l10n;
1307      if ( isset( $l10n[ $domain ] ) || ( _load_textdomain_just_in_time( $domain ) && isset( $l10n[ $domain ] ) ) ) {
1308          return $l10n[ $domain ];
1309      }
1310  
1311      static $noop_translations = null;
1312      if ( null === $noop_translations ) {
1313          $noop_translations = new NOOP_Translations;
1314      }
1315  
1316      return $noop_translations;
1317  }
1318  
1319  /**
1320   * Whether there are translations for the text domain.
1321   *
1322   * @since 3.0.0
1323   *
1324   * @global MO[] $l10n
1325   *
1326   * @param string $domain Text domain. Unique identifier for retrieving translated strings.
1327   * @return bool Whether there are translations.
1328   */
1329  function is_textdomain_loaded( $domain ) {
1330      global $l10n;
1331      return isset( $l10n[ $domain ] );
1332  }
1333  
1334  /**
1335   * Translates role name.
1336   *
1337   * Since the role names are in the database and not in the source there
1338   * are dummy gettext calls to get them into the POT file and this function
1339   * properly translates them back.
1340   *
1341   * The before_last_bar() call is needed, because older installations keep the roles
1342   * using the old context format: 'Role name|User role' and just skipping the
1343   * content after the last bar is easier than fixing them in the DB. New installations
1344   * won't suffer from that problem.
1345   *
1346   * @since 2.8.0
1347   * @since 5.2.0 Added the `$domain` parameter.
1348   *
1349   * @param string $name   The role name.
1350   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
1351   *                       Default 'default'.
1352   * @return string Translated role name on success, original name on failure.
1353   */
1354  function translate_user_role( $name, $domain = 'default' ) {
1355      return translate_with_gettext_context( before_last_bar( $name ), 'User role', $domain );
1356  }
1357  
1358  /**
1359   * Get all available languages based on the presence of *.mo files in a given directory.
1360   *
1361   * The default directory is WP_LANG_DIR.
1362   *
1363   * @since 3.0.0
1364   * @since 4.7.0 The results are now filterable with the {@see 'get_available_languages'} filter.
1365   *
1366   * @param string $dir A directory to search for language files.
1367   *                    Default WP_LANG_DIR.
1368   * @return string[] An array of language codes or an empty array if no languages are present. Language codes are formed by stripping the .mo extension from the language file names.
1369   */
1370  function get_available_languages( $dir = null ) {
1371      $languages = array();
1372  
1373      $lang_files = glob( ( is_null( $dir ) ? WP_LANG_DIR : $dir ) . '/*.mo' );
1374      if ( $lang_files ) {
1375          foreach ( $lang_files as $lang_file ) {
1376              $lang_file = basename( $lang_file, '.mo' );
1377              if ( 0 !== strpos( $lang_file, 'continents-cities' ) && 0 !== strpos( $lang_file, 'ms-' ) &&
1378                  0 !== strpos( $lang_file, 'admin-' ) ) {
1379                  $languages[] = $lang_file;
1380              }
1381          }
1382      }
1383  
1384      /**
1385       * Filters the list of available language codes.
1386       *
1387       * @since 4.7.0
1388       *
1389       * @param string[] $languages An array of available language codes.
1390       * @param string   $dir       The directory where the language files were found.
1391       */
1392      return apply_filters( 'get_available_languages', $languages, $dir );
1393  }
1394  
1395  /**
1396   * Get installed translations.
1397   *
1398   * Looks in the wp-content/languages directory for translations of
1399   * plugins or themes.
1400   *
1401   * @since 3.7.0
1402   *
1403   * @param string $type What to search for. Accepts 'plugins', 'themes', 'core'.
1404   * @return array Array of language data.
1405   */
1406  function wp_get_installed_translations( $type ) {
1407      if ( 'themes' !== $type && 'plugins' !== $type && 'core' !== $type ) {
1408          return array();
1409      }
1410  
1411      $dir = 'core' === $type ? '' : "/$type";
1412  
1413      if ( ! is_dir( WP_LANG_DIR ) ) {
1414          return array();
1415      }
1416  
1417      if ( $dir && ! is_dir( WP_LANG_DIR . $dir ) ) {
1418          return array();
1419      }
1420  
1421      $files = scandir( WP_LANG_DIR . $dir );
1422      if ( ! $files ) {
1423          return array();
1424      }
1425  
1426      $language_data = array();
1427  
1428      foreach ( $files as $file ) {
1429          if ( '.' === $file[0] || is_dir( WP_LANG_DIR . "$dir/$file" ) ) {
1430              continue;
1431          }
1432          if ( substr( $file, -3 ) !== '.po' ) {
1433              continue;
1434          }
1435          if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?).po/', $file, $match ) ) {
1436              continue;
1437          }
1438          if ( ! in_array( substr( $file, 0, -3 ) . '.mo', $files, true ) ) {
1439              continue;
1440          }
1441  
1442          list( , $textdomain, $language ) = $match;
1443          if ( '' === $textdomain ) {
1444              $textdomain = 'default';
1445          }
1446          $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( WP_LANG_DIR . "$dir/$file" );
1447      }
1448      return $language_data;
1449  }
1450  
1451  /**
1452   * Extract headers from a PO file.
1453   *
1454   * @since 3.7.0
1455   *
1456   * @param string $po_file Path to PO file.
1457   * @return string[] Array of PO file header values keyed by header name.
1458   */
1459  function wp_get_pomo_file_data( $po_file ) {
1460      $headers = get_file_data(
1461          $po_file,
1462          array(
1463              'POT-Creation-Date'  => '"POT-Creation-Date',
1464              'PO-Revision-Date'   => '"PO-Revision-Date',
1465              'Project-Id-Version' => '"Project-Id-Version',
1466              'X-Generator'        => '"X-Generator',
1467          )
1468      );
1469      foreach ( $headers as $header => $value ) {
1470          // Remove possible contextual '\n' and closing double quote.
1471          $headers[ $header ] = preg_replace( '~(\\\n)?"$~', '', $value );
1472      }
1473      return $headers;
1474  }
1475  
1476  /**
1477   * Language selector.
1478   *
1479   * @since 4.0.0
1480   * @since 4.3.0 Introduced the `echo` argument.
1481   * @since 4.7.0 Introduced the `show_option_site_default` argument.
1482   * @since 5.1.0 Introduced the `show_option_en_us` argument.
1483   *
1484   * @see get_available_languages()
1485   * @see wp_get_available_translations()
1486   *
1487   * @param string|array $args {
1488   *     Optional. Array or string of arguments for outputting the language selector.
1489   *
1490   *     @type string   $id                           ID attribute of the select element. Default 'locale'.
1491   *     @type string   $name                         Name attribute of the select element. Default 'locale'.
1492   *     @type array    $languages                    List of installed languages, contain only the locales.
1493   *                                                  Default empty array.
1494   *     @type array    $translations                 List of available translations. Default result of
1495   *                                                  wp_get_available_translations().
1496   *     @type string   $selected                     Language which should be selected. Default empty.
1497   *     @type bool|int $echo                         Whether to echo the generated markup. Accepts 0, 1, or their
1498   *                                                  boolean equivalents. Default 1.
1499   *     @type bool     $show_available_translations  Whether to show available translations. Default true.
1500   *     @type bool     $show_option_site_default     Whether to show an option to fall back to the site's locale. Default false.
1501   *     @type bool     $show_option_en_us            Whether to show an option for English (United States). Default true.
1502   * }
1503   * @return string HTML dropdown list of languages.
1504   */
1505  function wp_dropdown_languages( $args = array() ) {
1506  
1507      $parsed_args = wp_parse_args(
1508          $args,
1509          array(
1510              'id'                          => 'locale',
1511              'name'                        => 'locale',
1512              'languages'                   => array(),
1513              'translations'                => array(),
1514              'selected'                    => '',
1515              'echo'                        => 1,
1516              'show_available_translations' => true,
1517              'show_option_site_default'    => false,
1518              'show_option_en_us'           => true,
1519          )
1520      );
1521  
1522      // Bail if no ID or no name.
1523      if ( ! $parsed_args['id'] || ! $parsed_args['name'] ) {
1524          return;
1525      }
1526  
1527      // English (United States) uses an empty string for the value attribute.
1528      if ( 'en_US' === $parsed_args['selected'] ) {
1529          $parsed_args['selected'] = '';
1530      }
1531  
1532      $translations = $parsed_args['translations'];
1533      if ( empty( $translations ) ) {
1534          require_once ABSPATH . 'wp-admin/includes/translation-install.php';
1535          $translations = wp_get_available_translations();
1536      }
1537  
1538      /*
1539       * $parsed_args['languages'] should only contain the locales. Find the locale in
1540       * $translations to get the native name. Fall back to locale.
1541       */
1542      $languages = array();
1543      foreach ( $parsed_args['languages'] as $locale ) {
1544          if ( isset( $translations[ $locale ] ) ) {
1545              $translation = $translations[ $locale ];
1546              $languages[] = array(
1547                  'language'    => $translation['language'],
1548                  'native_name' => $translation['native_name'],
1549                  'lang'        => current( $translation['iso'] ),
1550              );
1551  
1552              // Remove installed language from available translations.
1553              unset( $translations[ $locale ] );
1554          } else {
1555              $languages[] = array(
1556                  'language'    => $locale,
1557                  'native_name' => $locale,
1558                  'lang'        => '',
1559              );
1560          }
1561      }
1562  
1563      $translations_available = ( ! empty( $translations ) && $parsed_args['show_available_translations'] );
1564  
1565      // Holds the HTML markup.
1566      $structure = array();
1567  
1568      // List installed languages.
1569      if ( $translations_available ) {
1570          $structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';
1571      }
1572  
1573      // Site default.
1574      if ( $parsed_args['show_option_site_default'] ) {
1575          $structure[] = sprintf(
1576              '<option value="site-default" data-installed="1"%s>%s</option>',
1577              selected( 'site-default', $parsed_args['selected'], false ),
1578              _x( 'Site Default', 'default site language' )
1579          );
1580      }
1581  
1582      if ( $parsed_args['show_option_en_us'] ) {
1583          $structure[] = sprintf(
1584              '<option value="" lang="en" data-installed="1"%s>English (United States)</option>',
1585              selected( '', $parsed_args['selected'], false )
1586          );
1587      }
1588  
1589      // List installed languages.
1590      foreach ( $languages as $language ) {
1591          $structure[] = sprintf(
1592              '<option value="%s" lang="%s"%s data-installed="1">%s</option>',
1593              esc_attr( $language['language'] ),
1594              esc_attr( $language['lang'] ),
1595              selected( $language['language'], $parsed_args['selected'], false ),
1596              esc_html( $language['native_name'] )
1597          );
1598      }
1599      if ( $translations_available ) {
1600          $structure[] = '</optgroup>';
1601      }
1602  
1603      // List available translations.
1604      if ( $translations_available ) {
1605          $structure[] = '<optgroup label="' . esc_attr_x( 'Available', 'translations' ) . '">';
1606          foreach ( $translations as $translation ) {
1607              $structure[] = sprintf(
1608                  '<option value="%s" lang="%s"%s>%s</option>',
1609                  esc_attr( $translation['language'] ),
1610                  esc_attr( current( $translation['iso'] ) ),
1611                  selected( $translation['language'], $parsed_args['selected'], false ),
1612                  esc_html( $translation['native_name'] )
1613              );
1614          }
1615          $structure[] = '</optgroup>';
1616      }
1617  
1618      // Combine the output string.
1619      $output  = sprintf( '<select name="%s" id="%s">', esc_attr( $parsed_args['name'] ), esc_attr( $parsed_args['id'] ) );
1620      $output .= implode( "\n", $structure );
1621      $output .= '</select>';
1622  
1623      if ( $parsed_args['echo'] ) {
1624          echo $output;
1625      }
1626  
1627      return $output;
1628  }
1629  
1630  /**
1631   * Determines whether the current locale is right-to-left (RTL).
1632   *
1633   * For more information on this and similar theme functions, check out
1634   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
1635   * Conditional Tags} article in the Theme Developer Handbook.
1636   *
1637   * @since 3.0.0
1638   *
1639   * @global WP_Locale $wp_locale WordPress date and time locale object.
1640   *
1641   * @return bool Whether locale is RTL.
1642   */
1643  function is_rtl() {
1644      global $wp_locale;
1645      if ( ! ( $wp_locale instanceof WP_Locale ) ) {
1646          return false;
1647      }
1648      return $wp_locale->is_rtl();
1649  }
1650  
1651  /**
1652   * Switches the translations according to the given locale.
1653   *
1654   * @since 4.7.0
1655   *
1656   * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
1657   *
1658   * @param string $locale The locale.
1659   * @return bool True on success, false on failure.
1660   */
1661  function switch_to_locale( $locale ) {
1662      /* @var WP_Locale_Switcher $wp_locale_switcher */
1663      global $wp_locale_switcher;
1664  
1665      return $wp_locale_switcher->switch_to_locale( $locale );
1666  }
1667  
1668  /**
1669   * Restores the translations according to the previous locale.
1670   *
1671   * @since 4.7.0
1672   *
1673   * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
1674   *
1675   * @return string|false Locale on success, false on error.
1676   */
1677  function restore_previous_locale() {
1678      /* @var WP_Locale_Switcher $wp_locale_switcher */
1679      global $wp_locale_switcher;
1680  
1681      return $wp_locale_switcher->restore_previous_locale();
1682  }
1683  
1684  /**
1685   * Restores the translations according to the original locale.
1686   *
1687   * @since 4.7.0
1688   *
1689   * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
1690   *
1691   * @return string|false Locale on success, false on error.
1692   */
1693  function restore_current_locale() {
1694      /* @var WP_Locale_Switcher $wp_locale_switcher */
1695      global $wp_locale_switcher;
1696  
1697      return $wp_locale_switcher->restore_current_locale();
1698  }
1699  
1700  /**
1701   * Whether switch_to_locale() is in effect.
1702   *
1703   * @since 4.7.0
1704   *
1705   * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
1706   *
1707   * @return bool True if the locale has been switched, false otherwise.
1708   */
1709  function is_locale_switched() {
1710      /* @var WP_Locale_Switcher $wp_locale_switcher */
1711      global $wp_locale_switcher;
1712  
1713      return $wp_locale_switcher->is_switched();
1714  }


Generated: Sun Aug 1 01:00:05 2021 Cross-referenced by PHPXref 0.7.1