[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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 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 = 0 ) {
  95      $user_object = false;
  96  
  97      if ( 0 === $user && function_exists( 'wp_get_current_user' ) ) {
  98          $user_object = wp_get_current_user();
  99      } elseif ( $user instanceof WP_User ) {
 100          $user_object = $user;
 101      } elseif ( $user && is_numeric( $user ) ) {
 102          $user_object = get_user_by( 'id', $user );
 103      }
 104  
 105      if ( ! $user_object ) {
 106          return get_locale();
 107      }
 108  
 109      $locale = $user_object->locale;
 110  
 111      return $locale ? $locale : get_locale();
 112  }
 113  
 114  /**
 115   * Determines the current locale desired for the request.
 116   *
 117   * @since 5.0.0
 118   *
 119   * @global string $pagenow The filename of the current screen.
 120   *
 121   * @return string The determined locale.
 122   */
 123  function determine_locale() {
 124      /**
 125       * Filters the locale for the current request prior to the default determination process.
 126       *
 127       * Using this filter allows to override the default logic, effectively short-circuiting the function.
 128       *
 129       * @since 5.0.0
 130       *
 131       * @param string|null $locale The locale to return and short-circuit. Default null.
 132       */
 133      $determined_locale = apply_filters( 'pre_determine_locale', null );
 134  
 135      if ( $determined_locale && is_string( $determined_locale ) ) {
 136          return $determined_locale;
 137      }
 138  
 139      if (
 140          isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] &&
 141          ( ! empty( $_GET['wp_lang'] ) || ! empty( $_COOKIE['wp_lang'] ) )
 142      ) {
 143          if ( ! empty( $_GET['wp_lang'] ) ) {
 144              $determined_locale = sanitize_locale_name( $_GET['wp_lang'] );
 145          } else {
 146              $determined_locale = sanitize_locale_name( $_COOKIE['wp_lang'] );
 147          }
 148      } elseif (
 149          is_admin() ||
 150          ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() )
 151      ) {
 152          $determined_locale = get_user_locale();
 153      } elseif (
 154          ( ! empty( $_REQUEST['language'] ) || isset( $GLOBALS['wp_local_package'] ) )
 155          && wp_installing()
 156      ) {
 157          if ( ! empty( $_REQUEST['language'] ) ) {
 158              $determined_locale = sanitize_locale_name( $_REQUEST['language'] );
 159          } else {
 160              $determined_locale = $GLOBALS['wp_local_package'];
 161          }
 162      }
 163  
 164      if ( ! $determined_locale ) {
 165          $determined_locale = get_locale();
 166      }
 167  
 168      /**
 169       * Filters the locale for the current request.
 170       *
 171       * @since 5.0.0
 172       *
 173       * @param string $determined_locale The locale.
 174       */
 175      return apply_filters( 'determine_locale', $determined_locale );
 176  }
 177  
 178  /**
 179   * Retrieves the translation of $text.
 180   *
 181   * If there is no translation, or the text domain isn't loaded, the original text is returned.
 182   *
 183   * *Note:* Don't use translate() directly, use __() or related functions.
 184   *
 185   * @since 2.2.0
 186   * @since 5.5.0 Introduced `gettext-{$domain}` filter.
 187   *
 188   * @param string $text   Text to translate.
 189   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 190   *                       Default 'default'.
 191   * @return string Translated text.
 192   */
 193  function translate( $text, $domain = 'default' ) {
 194      $translations = get_translations_for_domain( $domain );
 195      $translation  = $translations->translate( $text );
 196  
 197      /**
 198       * Filters text with its translation.
 199       *
 200       * @since 2.0.11
 201       *
 202       * @param string $translation Translated text.
 203       * @param string $text        Text to translate.
 204       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 205       */
 206      $translation = apply_filters( 'gettext', $translation, $text, $domain );
 207  
 208      /**
 209       * Filters text with its translation for a domain.
 210       *
 211       * The dynamic portion of the hook name, `$domain`, refers to the text domain.
 212       *
 213       * @since 5.5.0
 214       *
 215       * @param string $translation Translated text.
 216       * @param string $text        Text to translate.
 217       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 218       */
 219      $translation = apply_filters( "gettext_{$domain}", $translation, $text, $domain );
 220  
 221      return $translation;
 222  }
 223  
 224  /**
 225   * Removes last item on a pipe-delimited string.
 226   *
 227   * Meant for removing the last item in a string, such as 'Role name|User role'. The original
 228   * string will be returned if no pipe '|' characters are found in the string.
 229   *
 230   * @since 2.8.0
 231   *
 232   * @param string $text A pipe-delimited string.
 233   * @return string Either $text or everything before the last pipe.
 234   */
 235  function before_last_bar( $text ) {
 236      $last_bar = strrpos( $text, '|' );
 237      if ( false === $last_bar ) {
 238          return $text;
 239      } else {
 240          return substr( $text, 0, $last_bar );
 241      }
 242  }
 243  
 244  /**
 245   * Retrieves the translation of $text in the context defined in $context.
 246   *
 247   * If there is no translation, or the text domain isn't loaded, the original text is returned.
 248   *
 249   * *Note:* Don't use translate_with_gettext_context() directly, use _x() or related functions.
 250   *
 251   * @since 2.8.0
 252   * @since 5.5.0 Introduced `gettext_with_context-{$domain}` filter.
 253   *
 254   * @param string $text    Text to translate.
 255   * @param string $context Context information for the translators.
 256   * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
 257   *                        Default 'default'.
 258   * @return string Translated text on success, original text on failure.
 259   */
 260  function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
 261      $translations = get_translations_for_domain( $domain );
 262      $translation  = $translations->translate( $text, $context );
 263  
 264      /**
 265       * Filters text with its translation based on context information.
 266       *
 267       * @since 2.8.0
 268       *
 269       * @param string $translation Translated text.
 270       * @param string $text        Text to translate.
 271       * @param string $context     Context information for the translators.
 272       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 273       */
 274      $translation = apply_filters( 'gettext_with_context', $translation, $text, $context, $domain );
 275  
 276      /**
 277       * Filters text with its translation based on context information for a domain.
 278       *
 279       * The dynamic portion of the hook name, `$domain`, refers to the text domain.
 280       *
 281       * @since 5.5.0
 282       *
 283       * @param string $translation Translated text.
 284       * @param string $text        Text to translate.
 285       * @param string $context     Context information for the translators.
 286       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 287       */
 288      $translation = apply_filters( "gettext_with_context_{$domain}", $translation, $text, $context, $domain );
 289  
 290      return $translation;
 291  }
 292  
 293  /**
 294   * Retrieves the translation of $text.
 295   *
 296   * If there is no translation, or the text domain isn't loaded, the original text is returned.
 297   *
 298   * @since 2.1.0
 299   *
 300   * @param string $text   Text to translate.
 301   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 302   *                       Default 'default'.
 303   * @return string Translated text.
 304   */
 305  function __( $text, $domain = 'default' ) {
 306      return translate( $text, $domain );
 307  }
 308  
 309  /**
 310   * Retrieves the translation of $text and escapes it for safe use in an attribute.
 311   *
 312   * If there is no translation, or the text domain isn't loaded, the original text is 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 on success, original text on failure.
 320   */
 321  function esc_attr__( $text, $domain = 'default' ) {
 322      return esc_attr( translate( $text, $domain ) );
 323  }
 324  
 325  /**
 326   * Retrieves the translation of $text and escapes it for safe use in HTML output.
 327   *
 328   * If there is no translation, or the text domain isn't loaded, the original text
 329   * is escaped and returned.
 330   *
 331   * @since 2.8.0
 332   *
 333   * @param string $text   Text to translate.
 334   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 335   *                       Default 'default'.
 336   * @return string Translated text.
 337   */
 338  function esc_html__( $text, $domain = 'default' ) {
 339      return esc_html( translate( $text, $domain ) );
 340  }
 341  
 342  /**
 343   * Displays translated text.
 344   *
 345   * @since 1.2.0
 346   *
 347   * @param string $text   Text to translate.
 348   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 349   *                       Default 'default'.
 350   */
 351  function _e( $text, $domain = 'default' ) {
 352      echo translate( $text, $domain );
 353  }
 354  
 355  /**
 356   * Displays translated text that has been escaped for safe use in an attribute.
 357   *
 358   * Encodes `< > & " '` (less than, greater than, ampersand, double quote, single quote).
 359   * Will never double encode entities.
 360   *
 361   * If you need the value for use in PHP, use esc_attr__().
 362   *
 363   * @since 2.8.0
 364   *
 365   * @param string $text   Text to translate.
 366   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 367   *                       Default 'default'.
 368   */
 369  function esc_attr_e( $text, $domain = 'default' ) {
 370      echo esc_attr( translate( $text, $domain ) );
 371  }
 372  
 373  /**
 374   * Displays translated text that has been escaped for safe use in HTML output.
 375   *
 376   * If there is no translation, or the text domain isn't loaded, the original text
 377   * is escaped and displayed.
 378   *
 379   * If you need the value for use in PHP, use esc_html__().
 380   *
 381   * @since 2.8.0
 382   *
 383   * @param string $text   Text to translate.
 384   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 385   *                       Default 'default'.
 386   */
 387  function esc_html_e( $text, $domain = 'default' ) {
 388      echo esc_html( translate( $text, $domain ) );
 389  }
 390  
 391  /**
 392   * Retrieves translated string with gettext context.
 393   *
 394   * Quite a few times, there will be collisions with similar translatable text
 395   * found in more than two places, but with different translated context.
 396   *
 397   * By including the context in the pot file, translators can translate the two
 398   * strings differently.
 399   *
 400   * @since 2.8.0
 401   *
 402   * @param string $text    Text to translate.
 403   * @param string $context Context information for the translators.
 404   * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
 405   *                        Default 'default'.
 406   * @return string Translated context string without pipe.
 407   */
 408  function _x( $text, $context, $domain = 'default' ) {
 409      return translate_with_gettext_context( $text, $context, $domain );
 410  }
 411  
 412  /**
 413   * Displays translated string with gettext context.
 414   *
 415   * @since 3.0.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   */
 422  function _ex( $text, $context, $domain = 'default' ) {
 423      echo _x( $text, $context, $domain );
 424  }
 425  
 426  /**
 427   * Translates string with gettext context, and escapes it for safe use in an attribute.
 428   *
 429   * If there is no translation, or the text domain isn't loaded, the original text
 430   * is escaped and returned.
 431   *
 432   * @since 2.8.0
 433   *
 434   * @param string $text    Text to translate.
 435   * @param string $context Context information for the translators.
 436   * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
 437   *                        Default 'default'.
 438   * @return string Translated text.
 439   */
 440  function esc_attr_x( $text, $context, $domain = 'default' ) {
 441      return esc_attr( translate_with_gettext_context( $text, $context, $domain ) );
 442  }
 443  
 444  /**
 445   * Translates string with gettext context, and escapes it for safe use in HTML output.
 446   *
 447   * If there is no translation, or the text domain isn't loaded, the original text
 448   * is escaped and returned.
 449   *
 450   * @since 2.9.0
 451   *
 452   * @param string $text    Text to translate.
 453   * @param string $context Context information for the translators.
 454   * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
 455   *                        Default 'default'.
 456   * @return string Translated text.
 457   */
 458  function esc_html_x( $text, $context, $domain = 'default' ) {
 459      return esc_html( translate_with_gettext_context( $text, $context, $domain ) );
 460  }
 461  
 462  /**
 463   * Translates and retrieves the singular or plural form based on the supplied number.
 464   *
 465   * Used when you want to use the appropriate form of a string based on whether a
 466   * number is singular or plural.
 467   *
 468   * Example:
 469   *
 470   *     printf( _n( '%s person', '%s people', $count, 'text-domain' ), number_format_i18n( $count ) );
 471   *
 472   * @since 2.8.0
 473   * @since 5.5.0 Introduced `ngettext-{$domain}` filter.
 474   *
 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 int    $number The number to compare against to use either the singular or plural form.
 478   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
 479   *                       Default 'default'.
 480   * @return string The translated singular or plural form.
 481   */
 482  function _n( $single, $plural, $number, $domain = 'default' ) {
 483      $translations = get_translations_for_domain( $domain );
 484      $translation  = $translations->translate_plural( $single, $plural, $number );
 485  
 486      /**
 487       * Filters the singular or plural form of a string.
 488       *
 489       * @since 2.2.0
 490       *
 491       * @param string $translation Translated text.
 492       * @param string $single      The text to be used if the number is singular.
 493       * @param string $plural      The text to be used if the number is plural.
 494       * @param int    $number      The number to compare against to use either the singular or plural form.
 495       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 496       */
 497      $translation = apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
 498  
 499      /**
 500       * Filters the singular or plural form of a string for a domain.
 501       *
 502       * The dynamic portion of the hook name, `$domain`, refers to the text domain.
 503       *
 504       * @since 5.5.0
 505       *
 506       * @param string $translation Translated text.
 507       * @param string $single      The text to be used if the number is singular.
 508       * @param string $plural      The text to be used if the number is plural.
 509       * @param int    $number      The number to compare against to use either the singular or plural form.
 510       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 511       */
 512      $translation = apply_filters( "ngettext_{$domain}", $translation, $single, $plural, $number, $domain );
 513  
 514      return $translation;
 515  }
 516  
 517  /**
 518   * Translates and retrieves the singular or plural form based on the supplied number, with gettext context.
 519   *
 520   * This is a hybrid of _n() and _x(). It supports context and plurals.
 521   *
 522   * Used when you want to use the appropriate form of a string with context based on whether a
 523   * number is singular or plural.
 524   *
 525   * Example of a generic phrase which is disambiguated via the context parameter:
 526   *
 527   *     printf( _nx( '%s group', '%s groups', $people, 'group of people', 'text-domain' ), number_format_i18n( $people ) );
 528   *     printf( _nx( '%s group', '%s groups', $animals, 'group of animals', 'text-domain' ), number_format_i18n( $animals ) );
 529   *
 530   * @since 2.8.0
 531   * @since 5.5.0 Introduced `ngettext_with_context-{$domain}` filter.
 532   *
 533   * @param string $single  The text to be used if the number is singular.
 534   * @param string $plural  The text to be used if the number is plural.
 535   * @param int    $number  The number to compare against to use either the singular or plural form.
 536   * @param string $context Context information for the translators.
 537   * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
 538   *                        Default 'default'.
 539   * @return string The translated singular or plural form.
 540   */
 541  function _nx( $single, $plural, $number, $context, $domain = 'default' ) {
 542      $translations = get_translations_for_domain( $domain );
 543      $translation  = $translations->translate_plural( $single, $plural, $number, $context );
 544  
 545      /**
 546       * Filters the singular or plural form of a string with gettext context.
 547       *
 548       * @since 2.8.0
 549       *
 550       * @param string $translation Translated text.
 551       * @param string $single      The text to be used if the number is singular.
 552       * @param string $plural      The text to be used if the number is plural.
 553       * @param int    $number      The number to compare against to use either the singular or plural form.
 554       * @param string $context     Context information for the translators.
 555       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 556       */
 557      $translation = apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
 558  
 559      /**
 560       * Filters the singular or plural form of a string with gettext context for a domain.
 561       *
 562       * The dynamic portion of the hook name, `$domain`, refers to the text domain.
 563       *
 564       * @since 5.5.0
 565       *
 566       * @param string $translation Translated text.
 567       * @param string $single      The text to be used if the number is singular.
 568       * @param string $plural      The text to be used if the number is plural.
 569       * @param int    $number      The number to compare against to use either the singular or plural form.
 570       * @param string $context     Context information for the translators.
 571       * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
 572       */
 573      $translation = apply_filters( "ngettext_with_context_{$domain}", $translation, $single, $plural, $number, $context, $domain );
 574  
 575      return $translation;
 576  }
 577  
 578  /**
 579   * Registers plural strings in POT file, but does not translate them.
 580   *
 581   * Used when you want to keep structures with translatable plural
 582   * strings and use them later when the number is known.
 583   *
 584   * Example:
 585   *
 586   *     $message = _n_noop( '%s post', '%s posts', 'text-domain' );
 587   *     ...
 588   *     printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
 589   *
 590   * @since 2.5.0
 591   *
 592   * @param string $singular Singular form to be localized.
 593   * @param string $plural   Plural form to be localized.
 594   * @param string $domain   Optional. Text domain. Unique identifier for retrieving translated strings.
 595   *                         Default null.
 596   * @return array {
 597   *     Array of translation information for the strings.
 598   *
 599   *     @type string      $0        Singular form to be localized. No longer used.
 600   *     @type string      $1        Plural form to be localized. No longer used.
 601   *     @type string      $singular Singular form to be localized.
 602   *     @type string      $plural   Plural form to be localized.
 603   *     @type null        $context  Context information for the translators.
 604   *     @type string|null $domain   Text domain.
 605   * }
 606   */
 607  function _n_noop( $singular, $plural, $domain = null ) {
 608      return array(
 609          0          => $singular,
 610          1          => $plural,
 611          'singular' => $singular,
 612          'plural'   => $plural,
 613          'context'  => null,
 614          'domain'   => $domain,
 615      );
 616  }
 617  
 618  /**
 619   * Registers plural strings with gettext context in POT file, but does not translate them.
 620   *
 621   * Used when you want to keep structures with translatable plural
 622   * strings and use them later when the number is known.
 623   *
 624   * Example of a generic phrase which is disambiguated via the context parameter:
 625   *
 626   *     $messages = array(
 627   *          'people'  => _nx_noop( '%s group', '%s groups', 'people', 'text-domain' ),
 628   *          'animals' => _nx_noop( '%s group', '%s groups', 'animals', 'text-domain' ),
 629   *     );
 630   *     ...
 631   *     $message = $messages[ $type ];
 632   *     printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
 633   *
 634   * @since 2.8.0
 635   *
 636   * @param string $singular Singular form to be localized.
 637   * @param string $plural   Plural form to be localized.
 638   * @param string $context  Context information for the translators.
 639   * @param string $domain   Optional. Text domain. Unique identifier for retrieving translated strings.
 640   *                         Default null.
 641   * @return array {
 642   *     Array of translation information for the strings.
 643   *
 644   *     @type string      $0        Singular form to be localized. No longer used.
 645   *     @type string      $1        Plural form to be localized. No longer used.
 646   *     @type string      $2        Context information for the translators. No longer used.
 647   *     @type string      $singular Singular form to be localized.
 648   *     @type string      $plural   Plural form to be localized.
 649   *     @type string      $context  Context information for the translators.
 650   *     @type string|null $domain   Text domain.
 651   * }
 652   */
 653  function _nx_noop( $singular, $plural, $context, $domain = null ) {
 654      return array(
 655          0          => $singular,
 656          1          => $plural,
 657          2          => $context,
 658          'singular' => $singular,
 659          'plural'   => $plural,
 660          'context'  => $context,
 661          'domain'   => $domain,
 662      );
 663  }
 664  
 665  /**
 666   * Translates and returns the singular or plural form of a string that's been registered
 667   * with _n_noop() or _nx_noop().
 668   *
 669   * Used when you want to use a translatable plural string once the number is known.
 670   *
 671   * Example:
 672   *
 673   *     $message = _n_noop( '%s post', '%s posts', 'text-domain' );
 674   *     ...
 675   *     printf( translate_nooped_plural( $message, $count, 'text-domain' ), number_format_i18n( $count ) );
 676   *
 677   * @since 3.1.0
 678   *
 679   * @param array  $nooped_plural {
 680   *     Array that is usually a return value from _n_noop() or _nx_noop().
 681   *
 682   *     @type string      $singular Singular form to be localized.
 683   *     @type string      $plural   Plural form to be localized.
 684   *     @type string|null $context  Context information for the translators.
 685   *     @type string|null $domain   Text domain.
 686   * }
 687   * @param int    $count         Number of objects.
 688   * @param string $domain        Optional. Text domain. Unique identifier for retrieving translated strings. If $nooped_plural contains
 689   *                              a text domain passed to _n_noop() or _nx_noop(), it will override this value. Default 'default'.
 690   * @return string Either $singular or $plural translated text.
 691   */
 692  function translate_nooped_plural( $nooped_plural, $count, $domain = 'default' ) {
 693      if ( $nooped_plural['domain'] ) {
 694          $domain = $nooped_plural['domain'];
 695      }
 696  
 697      if ( $nooped_plural['context'] ) {
 698          return _nx( $nooped_plural['singular'], $nooped_plural['plural'], $count, $nooped_plural['context'], $domain );
 699      } else {
 700          return _n( $nooped_plural['singular'], $nooped_plural['plural'], $count, $domain );
 701      }
 702  }
 703  
 704  /**
 705   * Loads a .mo file into the text domain $domain.
 706   *
 707   * If the text domain already exists, the translations will be merged. If both
 708   * sets have the same string, the translation from the original value will be taken.
 709   *
 710   * On success, the .mo file will be placed in the $l10n global by $domain
 711   * and will be a MO object.
 712   *
 713   * @since 1.5.0
 714   * @since 6.1.0 Added the `$locale` parameter.
 715   *
 716   * @global MO[]                   $l10n                   An array of all currently loaded text domains.
 717   * @global MO[]                   $l10n_unloaded          An array of all text domains that have been unloaded again.
 718   * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
 719   *
 720   * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 721   * @param string $mofile Path to the .mo file.
 722   * @param string $locale Optional. Locale. Default is the current locale.
 723   * @return bool True on success, false on failure.
 724   */
 725  function load_textdomain( $domain, $mofile, $locale = null ) {
 726      /** @var WP_Textdomain_Registry $wp_textdomain_registry */
 727      global $l10n, $l10n_unloaded, $wp_textdomain_registry;
 728  
 729      $l10n_unloaded = (array) $l10n_unloaded;
 730  
 731      if ( ! is_string( $domain ) ) {
 732          return false;
 733      }
 734  
 735      /**
 736       * Filters whether to short-circuit loading .mo file.
 737       *
 738       * Returning a non-null value from the filter will effectively short-circuit
 739       * the loading, returning the passed value instead.
 740       *
 741       * @since 6.3.0
 742       *
 743       * @param bool|null   $loaded The result of loading a .mo file. Default null.
 744       * @param string      $domain Text domain. Unique identifier for retrieving translated strings.
 745       * @param string      $mofile Path to the MO file.
 746       * @param string|null $locale Locale.
 747       */
 748      $loaded = apply_filters( 'pre_load_textdomain', null, $domain, $mofile, $locale );
 749      if ( null !== $loaded ) {
 750          if ( true === $loaded ) {
 751              unset( $l10n_unloaded[ $domain ] );
 752          }
 753  
 754          return $loaded;
 755      }
 756  
 757      /**
 758       * Filters whether to override the .mo file loading.
 759       *
 760       * @since 2.9.0
 761       * @since 6.2.0 Added the `$locale` parameter.
 762       *
 763       * @param bool        $override Whether to override the .mo file loading. Default false.
 764       * @param string      $domain   Text domain. Unique identifier for retrieving translated strings.
 765       * @param string      $mofile   Path to the MO file.
 766       * @param string|null $locale   Locale.
 767       */
 768      $plugin_override = apply_filters( 'override_load_textdomain', false, $domain, $mofile, $locale );
 769  
 770      if ( true === (bool) $plugin_override ) {
 771          unset( $l10n_unloaded[ $domain ] );
 772  
 773          return true;
 774      }
 775  
 776      /**
 777       * Fires before the MO translation file is loaded.
 778       *
 779       * @since 2.9.0
 780       *
 781       * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 782       * @param string $mofile Path to the .mo file.
 783       */
 784      do_action( 'load_textdomain', $domain, $mofile );
 785  
 786      /**
 787       * Filters MO file path for loading translations for a specific text domain.
 788       *
 789       * @since 2.9.0
 790       *
 791       * @param string $mofile Path to the MO file.
 792       * @param string $domain Text domain. Unique identifier for retrieving translated strings.
 793       */
 794      $mofile = apply_filters( 'load_textdomain_mofile', $mofile, $domain );
 795  
 796      if ( ! $locale ) {
 797          $locale = determine_locale();
 798      }
 799  
 800      $i18n_controller = WP_Translation_Controller::get_instance();
 801  
 802      // Ensures the correct locale is set as the current one, in case it was filtered.
 803      $i18n_controller->set_locale( $locale );
 804  
 805      /**
 806       * Filters the preferred file format for translation files.
 807       *
 808       * Can be used to disable the use of PHP files for translations.
 809       *
 810       * @since 6.5.0
 811       *
 812       * @param string $preferred_format Preferred file format. Possible values: 'php', 'mo'. Default: 'php'.
 813       * @param string $domain           The text domain.
 814       */
 815      $preferred_format = apply_filters( 'translation_file_format', 'php', $domain );
 816      if ( ! in_array( $preferred_format, array( 'php', 'mo' ), true ) ) {
 817          $preferred_format = 'php';
 818      }
 819  
 820      $translation_files = array();
 821  
 822      if ( 'mo' !== $preferred_format ) {
 823          $translation_files[] = substr_replace( $mofile, ".l10n.$preferred_format", - strlen( '.mo' ) );
 824      }
 825  
 826      $translation_files[] = $mofile;
 827  
 828      foreach ( $translation_files as $file ) {
 829          /**
 830           * Filters the file path for loading translations for the given text domain.
 831           *
 832           * Similar to the {@see 'load_textdomain_mofile'} filter with the difference that
 833           * the file path could be for an MO or PHP file.
 834           *
 835           * @since 6.5.0
 836           * @since 6.6.0 Added the `$locale` parameter.
 837           *
 838           * @param string $file   Path to the translation file to load.
 839           * @param string $domain The text domain.
 840           * @param string $locale The locale.
 841           */
 842          $file = (string) apply_filters( 'load_translation_file', $file, $domain, $locale );
 843  
 844          $success = $i18n_controller->load_file( $file, $domain, $locale );
 845  
 846          if ( $success ) {
 847              if ( isset( $l10n[ $domain ] ) && $l10n[ $domain ] instanceof MO ) {
 848                  $i18n_controller->load_file( $l10n[ $domain ]->get_filename(), $domain, $locale );
 849              }
 850  
 851              // Unset NOOP_Translations reference in get_translations_for_domain().
 852              unset( $l10n[ $domain ] );
 853  
 854              $l10n[ $domain ] = new WP_Translations( $i18n_controller, $domain );
 855  
 856              $wp_textdomain_registry->set( $domain, $locale, dirname( $file ) );
 857  
 858              return true;
 859          }
 860      }
 861  
 862      return false;
 863  }
 864  
 865  /**
 866   * Unloads translations for a text domain.
 867   *
 868   * @since 3.0.0
 869   * @since 6.1.0 Added the `$reloadable` parameter.
 870   *
 871   * @global MO[] $l10n          An array of all currently loaded text domains.
 872   * @global MO[] $l10n_unloaded An array of all text domains that have been unloaded again.
 873   *
 874   * @param string $domain     Text domain. Unique identifier for retrieving translated strings.
 875   * @param bool   $reloadable Whether the text domain can be loaded just-in-time again.
 876   * @return bool Whether textdomain was unloaded.
 877   */
 878  function unload_textdomain( $domain, $reloadable = false ) {
 879      global $l10n, $l10n_unloaded;
 880  
 881      $l10n_unloaded = (array) $l10n_unloaded;
 882  
 883      /**
 884       * Filters whether to override the text domain unloading.
 885       *
 886       * @since 3.0.0
 887       * @since 6.1.0 Added the `$reloadable` parameter.
 888       *
 889       * @param bool   $override   Whether to override the text domain unloading. Default false.
 890       * @param string $domain     Text domain. Unique identifier for retrieving translated strings.
 891       * @param bool   $reloadable Whether the text domain can be loaded just-in-time again.
 892       */
 893      $plugin_override = apply_filters( 'override_unload_textdomain', false, $domain, $reloadable );
 894  
 895      if ( $plugin_override ) {
 896          if ( ! $reloadable ) {
 897              $l10n_unloaded[ $domain ] = true;
 898          }
 899  
 900          return true;
 901      }
 902  
 903      /**
 904       * Fires before the text domain is unloaded.
 905       *
 906       * @since 3.0.0
 907       * @since 6.1.0 Added the `$reloadable` parameter.
 908       *
 909       * @param string $domain     Text domain. Unique identifier for retrieving translated strings.
 910       * @param bool   $reloadable Whether the text domain can be loaded just-in-time again.
 911       */
 912      do_action( 'unload_textdomain', $domain, $reloadable );
 913  
 914      // Since multiple locales are supported, reloadable text domains don't actually need to be unloaded.
 915      if ( ! $reloadable ) {
 916          WP_Translation_Controller::get_instance()->unload_textdomain( $domain );
 917      }
 918  
 919      if ( isset( $l10n[ $domain ] ) ) {
 920          if ( $l10n[ $domain ] instanceof NOOP_Translations ) {
 921              unset( $l10n[ $domain ] );
 922  
 923              return false;
 924          }
 925  
 926          unset( $l10n[ $domain ] );
 927  
 928          if ( ! $reloadable ) {
 929              $l10n_unloaded[ $domain ] = true;
 930          }
 931  
 932          return true;
 933      }
 934  
 935      return false;
 936  }
 937  
 938  /**
 939   * Loads default translated strings based on locale.
 940   *
 941   * Loads the .mo file in WP_LANG_DIR constant path from WordPress root.
 942   * The translated (.mo) file is named based on the locale.
 943   *
 944   * @see load_textdomain()
 945   *
 946   * @since 1.5.0
 947   *
 948   * @param string $locale Optional. Locale to load. Default is the value of get_locale().
 949   * @return bool Whether the textdomain was loaded.
 950   */
 951  function load_default_textdomain( $locale = null ) {
 952      if ( null === $locale ) {
 953          $locale = determine_locale();
 954      }
 955  
 956      // Unload previously loaded strings so we can switch translations.
 957      unload_textdomain( 'default', true );
 958  
 959      $return = load_textdomain( 'default', WP_LANG_DIR . "/$locale.mo", $locale );
 960  
 961      if ( ( is_multisite() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) && ! file_exists( WP_LANG_DIR . "/admin-$locale.mo" ) ) {
 962          load_textdomain( 'default', WP_LANG_DIR . "/ms-$locale.mo", $locale );
 963          return $return;
 964      }
 965  
 966      if ( is_admin() || wp_installing() || ( defined( 'WP_REPAIRING' ) && WP_REPAIRING ) ) {
 967          load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo", $locale );
 968      }
 969  
 970      if ( is_network_admin() || ( defined( 'WP_INSTALLING_NETWORK' ) && WP_INSTALLING_NETWORK ) ) {
 971          load_textdomain( 'default', WP_LANG_DIR . "/admin-network-$locale.mo", $locale );
 972      }
 973  
 974      return $return;
 975  }
 976  
 977  /**
 978   * Loads a plugin's translated strings.
 979   *
 980   * If the path is not given then it will be the root of the plugin directory.
 981   *
 982   * The .mo file should be named based on the text domain with a dash, and then the locale exactly.
 983   *
 984   * @since 1.5.0
 985   * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
 986   * @since 6.7.0 Translations are no longer immediately loaded, but handed off to the just-in-time loading mechanism.
 987   *
 988   * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
 989   * @global array<string, WP_Translations|NOOP_Translations> $l10n An array of all currently loaded text domains.
 990   *
 991   * @param string       $domain          Unique identifier for retrieving translated strings
 992   * @param string|false $deprecated      Optional. Deprecated. Use the $plugin_rel_path parameter instead.
 993   *                                      Default false.
 994   * @param string|false $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides.
 995   *                                      Default false.
 996   * @return bool True when textdomain is successfully loaded, false otherwise.
 997   */
 998  function load_plugin_textdomain( $domain, $deprecated = false, $plugin_rel_path = false ) {
 999      /** @var WP_Textdomain_Registry $wp_textdomain_registry */
1000      /** @var array<string, WP_Translations|NOOP_Translations> $l10n */
1001      global $wp_textdomain_registry, $l10n;
1002  
1003      if ( ! is_string( $domain ) ) {
1004          return false;
1005      }
1006  
1007      if ( false !== $plugin_rel_path ) {
1008          $path = WP_PLUGIN_DIR . '/' . trim( $plugin_rel_path, '/' );
1009      } elseif ( false !== $deprecated ) {
1010          _deprecated_argument( __FUNCTION__, '2.7.0' );
1011          $path = ABSPATH . trim( $deprecated, '/' );
1012      } else {
1013          $path = WP_PLUGIN_DIR;
1014      }
1015  
1016      $wp_textdomain_registry->set_custom_path( $domain, $path );
1017  
1018      // If just-in-time loading was triggered before, reset the entry so it can be tried again.
1019      if ( isset( $l10n[ $domain ] ) && $l10n[ $domain ] instanceof NOOP_Translations ) {
1020          unset( $l10n[ $domain ] );
1021      }
1022  
1023      return true;
1024  }
1025  
1026  /**
1027   * Loads the translated strings for a plugin residing in the mu-plugins directory.
1028   *
1029   * @since 3.0.0
1030   * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
1031   * @since 6.7.0 Translations are no longer immediately loaded, but handed off to the just-in-time loading mechanism.
1032   *
1033   * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
1034   * @global array<string, WP_Translations|NOOP_Translations> $l10n An array of all currently loaded text domains.
1035   *
1036   * @param string $domain             Text domain. Unique identifier for retrieving translated strings.
1037   * @param string $mu_plugin_rel_path Optional. Relative to `WPMU_PLUGIN_DIR` directory in which the .mo
1038   *                                   file resides. Default empty string.
1039   * @return bool True when textdomain is successfully loaded, false otherwise.
1040   */
1041  function load_muplugin_textdomain( $domain, $mu_plugin_rel_path = '' ) {
1042      /** @var WP_Textdomain_Registry $wp_textdomain_registry */
1043      /** @var array<string, WP_Translations|NOOP_Translations> $l10n */
1044      global $wp_textdomain_registry, $l10n;
1045  
1046      if ( ! is_string( $domain ) ) {
1047          return false;
1048      }
1049  
1050      $path = WPMU_PLUGIN_DIR . '/' . ltrim( $mu_plugin_rel_path, '/' );
1051  
1052      $wp_textdomain_registry->set_custom_path( $domain, $path );
1053  
1054      // If just-in-time loading was triggered before, reset the entry so it can be tried again.
1055      if ( isset( $l10n[ $domain ] ) && $l10n[ $domain ] instanceof NOOP_Translations ) {
1056          unset( $l10n[ $domain ] );
1057      }
1058  
1059      return true;
1060  }
1061  
1062  /**
1063   * Loads the theme's translated strings.
1064   *
1065   * If the current locale exists as a .mo file in the theme's root directory, it
1066   * will be included in the translated strings by the $domain.
1067   *
1068   * The .mo files must be named based on the locale exactly.
1069   *
1070   * @since 1.5.0
1071   * @since 4.6.0 The function now tries to load the .mo file from the languages directory first.
1072   * @since 6.7.0 Translations are no longer immediately loaded, but handed off to the just-in-time loading mechanism.
1073   *
1074   * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
1075   * @global array<string, WP_Translations|NOOP_Translations> $l10n An array of all currently loaded text domains.
1076   *
1077   * @param string       $domain Text domain. Unique identifier for retrieving translated strings.
1078   * @param string|false $path   Optional. Path to the directory containing the .mo file.
1079   *                             Default false.
1080   * @return bool True when textdomain is successfully loaded, false otherwise.
1081   */
1082  function load_theme_textdomain( $domain, $path = false ) {
1083      /** @var WP_Textdomain_Registry $wp_textdomain_registry */
1084      /** @var array<string, WP_Translations|NOOP_Translations> $l10n */
1085      global $wp_textdomain_registry, $l10n;
1086  
1087      if ( ! is_string( $domain ) ) {
1088          return false;
1089      }
1090  
1091      if ( ! $path ) {
1092          $path = get_template_directory();
1093      }
1094  
1095      $wp_textdomain_registry->set_custom_path( $domain, $path );
1096  
1097      // If just-in-time loading was triggered before, reset the entry so it can be tried again.
1098      if ( isset( $l10n[ $domain ] ) && $l10n[ $domain ] instanceof NOOP_Translations ) {
1099          unset( $l10n[ $domain ] );
1100      }
1101  
1102      return true;
1103  }
1104  
1105  /**
1106   * Loads the child theme's translated strings.
1107   *
1108   * If the current locale exists as a .mo file in the child theme's
1109   * root directory, it will be included in the translated strings by the $domain.
1110   *
1111   * The .mo files must be named based on the locale exactly.
1112   *
1113   * @since 2.9.0
1114   *
1115   * @param string       $domain Text domain. Unique identifier for retrieving translated strings.
1116   * @param string|false $path   Optional. Path to the directory containing the .mo file.
1117   *                             Default false.
1118   * @return bool True when the theme textdomain is successfully loaded, false otherwise.
1119   */
1120  function load_child_theme_textdomain( $domain, $path = false ) {
1121      if ( ! $path ) {
1122          $path = get_stylesheet_directory();
1123      }
1124      return load_theme_textdomain( $domain, $path );
1125  }
1126  
1127  /**
1128   * Loads the script translated strings.
1129   *
1130   * @since 5.0.0
1131   * @since 5.0.2 Uses load_script_translations() to load translation data.
1132   * @since 5.1.0 The `$domain` parameter was made optional.
1133   *
1134   * @see WP_Scripts::set_translations()
1135   *
1136   * @param string $handle Name of the script to register a translation domain to.
1137   * @param string $domain Optional. Text domain. Default 'default'.
1138   * @param string $path   Optional. The full file path to the directory containing translation files.
1139   * @return string|false The translated strings in JSON encoding on success,
1140   *                      false if the script textdomain could not be loaded.
1141   */
1142  function load_script_textdomain( $handle, $domain = 'default', $path = '' ) {
1143      $wp_scripts = wp_scripts();
1144  
1145      if ( ! isset( $wp_scripts->registered[ $handle ] ) ) {
1146          return false;
1147      }
1148  
1149      $path   = untrailingslashit( $path );
1150      $locale = determine_locale();
1151  
1152      // If a path was given and the handle file exists simply return it.
1153      $file_base       = 'default' === $domain ? $locale : $domain . '-' . $locale;
1154      $handle_filename = $file_base . '-' . $handle . '.json';
1155  
1156      if ( $path ) {
1157          $translations = load_script_translations( $path . '/' . $handle_filename, $handle, $domain );
1158  
1159          if ( $translations ) {
1160              return $translations;
1161          }
1162      }
1163  
1164      $src = $wp_scripts->registered[ $handle ]->src;
1165  
1166      if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && str_starts_with( $src, $wp_scripts->content_url ) ) ) {
1167          $src = $wp_scripts->base_url . $src;
1168      }
1169  
1170      $relative       = false;
1171      $languages_path = WP_LANG_DIR;
1172  
1173      $src_url     = wp_parse_url( $src );
1174      $content_url = wp_parse_url( content_url() );
1175      $plugins_url = wp_parse_url( plugins_url() );
1176      $site_url    = wp_parse_url( site_url() );
1177      $theme_root  = get_theme_root();
1178  
1179      // If the host is the same or it's a relative URL.
1180      if (
1181          ( ! isset( $content_url['path'] ) || str_starts_with( $src_url['path'], $content_url['path'] ) ) &&
1182          ( ! isset( $src_url['host'] ) || ! isset( $content_url['host'] ) || $src_url['host'] === $content_url['host'] )
1183      ) {
1184          // Make the src relative the specific plugin or theme.
1185          if ( isset( $content_url['path'] ) ) {
1186              $relative = substr( $src_url['path'], strlen( $content_url['path'] ) );
1187          } else {
1188              $relative = $src_url['path'];
1189          }
1190          $relative = trim( $relative, '/' );
1191          $relative = explode( '/', $relative );
1192  
1193          /*
1194           * Ensure correct languages path when using a custom `WP_PLUGIN_DIR` / `WP_PLUGIN_URL` configuration,
1195           * a custom theme root, and/or using Multisite with subdirectories.
1196           * See https://core.trac.wordpress.org/ticket/60891 and https://core.trac.wordpress.org/ticket/62016.
1197           */
1198  
1199          $theme_dir = array_slice( explode( '/', $theme_root ), -1 );
1200          $dirname   = $theme_dir[0] === $relative[0] ? 'themes' : 'plugins';
1201  
1202          $languages_path = WP_LANG_DIR . '/' . $dirname;
1203  
1204          $relative = array_slice( $relative, 2 ); // Remove plugins/<plugin name> or themes/<theme name>.
1205          $relative = implode( '/', $relative );
1206      } elseif (
1207          ( ! isset( $plugins_url['path'] ) || str_starts_with( $src_url['path'], $plugins_url['path'] ) ) &&
1208          ( ! isset( $src_url['host'] ) || ! isset( $plugins_url['host'] ) || $src_url['host'] === $plugins_url['host'] )
1209      ) {
1210          // Make the src relative the specific plugin.
1211          if ( isset( $plugins_url['path'] ) ) {
1212              $relative = substr( $src_url['path'], strlen( $plugins_url['path'] ) );
1213          } else {
1214              $relative = $src_url['path'];
1215          }
1216          $relative = trim( $relative, '/' );
1217          $relative = explode( '/', $relative );
1218  
1219          $languages_path = WP_LANG_DIR . '/plugins';
1220  
1221          $relative = array_slice( $relative, 1 ); // Remove <plugin name>.
1222          $relative = implode( '/', $relative );
1223      } elseif ( ! isset( $src_url['host'] ) || ! isset( $site_url['host'] ) || $src_url['host'] === $site_url['host'] ) {
1224          if ( ! isset( $site_url['path'] ) ) {
1225              $relative = trim( $src_url['path'], '/' );
1226          } elseif ( str_starts_with( $src_url['path'], trailingslashit( $site_url['path'] ) ) ) {
1227              // Make the src relative to the WP root.
1228              $relative = substr( $src_url['path'], strlen( $site_url['path'] ) );
1229              $relative = trim( $relative, '/' );
1230          }
1231      }
1232  
1233      /**
1234       * Filters the relative path of scripts used for finding translation files.
1235       *
1236       * @since 5.0.2
1237       *
1238       * @param string|false $relative The relative path of the script. False if it could not be determined.
1239       * @param string       $src      The full source URL of the script.
1240       */
1241      $relative = apply_filters( 'load_script_textdomain_relative_path', $relative, $src );
1242  
1243      // If the source is not from WP.
1244      if ( false === $relative ) {
1245          return load_script_translations( false, $handle, $domain );
1246      }
1247  
1248      // Translations are always based on the unminified filename.
1249      if ( str_ends_with( $relative, '.min.js' ) ) {
1250          $relative = substr( $relative, 0, -7 ) . '.js';
1251      }
1252  
1253      $md5_filename = $file_base . '-' . md5( $relative ) . '.json';
1254  
1255      if ( $path ) {
1256          $translations = load_script_translations( $path . '/' . $md5_filename, $handle, $domain );
1257  
1258          if ( $translations ) {
1259              return $translations;
1260          }
1261      }
1262  
1263      $translations = load_script_translations( $languages_path . '/' . $md5_filename, $handle, $domain );
1264  
1265      if ( $translations ) {
1266          return $translations;
1267      }
1268  
1269      return load_script_translations( false, $handle, $domain );
1270  }
1271  
1272  /**
1273   * Loads the translation data for the given script handle and text domain.
1274   *
1275   * @since 5.0.2
1276   *
1277   * @param string|false $file   Path to the translation file to load. False if there isn't one.
1278   * @param string       $handle Name of the script to register a translation domain to.
1279   * @param string       $domain The text domain.
1280   * @return string|false The JSON-encoded translated strings for the given script handle and text domain.
1281   *                      False if there are none.
1282   */
1283  function load_script_translations( $file, $handle, $domain ) {
1284      /**
1285       * Pre-filters script translations for the given file, script handle and text domain.
1286       *
1287       * Returning a non-null value allows to override the default logic, effectively short-circuiting the function.
1288       *
1289       * @since 5.0.2
1290       *
1291       * @param string|false|null $translations JSON-encoded translation data. Default null.
1292       * @param string|false      $file         Path to the translation file to load. False if there isn't one.
1293       * @param string            $handle       Name of the script to register a translation domain to.
1294       * @param string            $domain       The text domain.
1295       */
1296      $translations = apply_filters( 'pre_load_script_translations', null, $file, $handle, $domain );
1297  
1298      if ( null !== $translations ) {
1299          return $translations;
1300      }
1301  
1302      /**
1303       * Filters the file path for loading script translations for the given script handle and text domain.
1304       *
1305       * @since 5.0.2
1306       *
1307       * @param string|false $file   Path to the translation file to load. False if there isn't one.
1308       * @param string       $handle Name of the script to register a translation domain to.
1309       * @param string       $domain The text domain.
1310       */
1311      $file = apply_filters( 'load_script_translation_file', $file, $handle, $domain );
1312  
1313      if ( ! $file || ! is_readable( $file ) ) {
1314          return false;
1315      }
1316  
1317      $translations = file_get_contents( $file );
1318  
1319      /**
1320       * Filters script translations for the given file, script handle and text domain.
1321       *
1322       * @since 5.0.2
1323       *
1324       * @param string $translations JSON-encoded translation data.
1325       * @param string $file         Path to the translation file that was loaded.
1326       * @param string $handle       Name of the script to register a translation domain to.
1327       * @param string $domain       The text domain.
1328       */
1329      return apply_filters( 'load_script_translations', $translations, $file, $handle, $domain );
1330  }
1331  
1332  /**
1333   * Loads plugin and theme text domains just-in-time.
1334   *
1335   * When a textdomain is encountered for the first time, we try to load
1336   * the translation file from `wp-content/languages`, removing the need
1337   * to call load_plugin_textdomain() or load_theme_textdomain().
1338   *
1339   * @since 4.6.0
1340   * @access private
1341   *
1342   * @global MO[]                   $l10n_unloaded          An array of all text domains that have been unloaded again.
1343   * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
1344   *
1345   * @param string $domain Text domain. Unique identifier for retrieving translated strings.
1346   * @return bool True when the textdomain is successfully loaded, false otherwise.
1347   */
1348  function _load_textdomain_just_in_time( $domain ) {
1349      /** @var WP_Textdomain_Registry $wp_textdomain_registry */
1350      global $l10n_unloaded, $wp_textdomain_registry;
1351  
1352      $l10n_unloaded = (array) $l10n_unloaded;
1353  
1354      // Short-circuit if domain is 'default' which is reserved for core.
1355      if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) {
1356          return false;
1357      }
1358  
1359      if ( ! $wp_textdomain_registry->has( $domain ) ) {
1360          return false;
1361      }
1362  
1363      $locale = determine_locale();
1364      $path   = $wp_textdomain_registry->get( $domain, $locale );
1365      if ( ! $path ) {
1366          return false;
1367      }
1368  
1369      if ( ! doing_action( 'after_setup_theme' ) && ! did_action( 'after_setup_theme' ) ) {
1370          _doing_it_wrong(
1371              __FUNCTION__,
1372              sprintf(
1373                  /* translators: 1: The text domain. 2: 'init'. */
1374                  __( 'Translation loading for the %1$s domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the %2$s action or later.' ),
1375                  '<code>' . $domain . '</code>',
1376                  '<code>init</code>'
1377              ),
1378              '6.7.0'
1379          );
1380      }
1381  
1382      // Themes with their language directory outside of WP_LANG_DIR have a different file name.
1383      $template_directory   = trailingslashit( get_template_directory() );
1384      $stylesheet_directory = trailingslashit( get_stylesheet_directory() );
1385      if ( str_starts_with( $path, $template_directory ) || str_starts_with( $path, $stylesheet_directory ) ) {
1386          $mofile = "{$path}{$locale}.mo";
1387      } else {
1388          $mofile = "{$path}{$domain}-{$locale}.mo";
1389      }
1390  
1391      return load_textdomain( $domain, $mofile, $locale );
1392  }
1393  
1394  /**
1395   * Returns the Translations instance for a text domain.
1396   *
1397   * If there isn't one, returns empty Translations instance.
1398   *
1399   * @since 2.8.0
1400   *
1401   * @global MO[] $l10n An array of all currently loaded text domains.
1402   *
1403   * @param string $domain Text domain. Unique identifier for retrieving translated strings.
1404   * @return Translations|NOOP_Translations A Translations instance.
1405   */
1406  function get_translations_for_domain( $domain ) {
1407      global $l10n;
1408      if ( isset( $l10n[ $domain ] ) || ( _load_textdomain_just_in_time( $domain ) && isset( $l10n[ $domain ] ) ) ) {
1409          return $l10n[ $domain ];
1410      }
1411  
1412      static $noop_translations = null;
1413      if ( null === $noop_translations ) {
1414          $noop_translations = new NOOP_Translations();
1415      }
1416  
1417      $l10n[ $domain ] = &$noop_translations;
1418  
1419      return $noop_translations;
1420  }
1421  
1422  /**
1423   * Determines whether there are translations for the text domain.
1424   *
1425   * @since 3.0.0
1426   *
1427   * @global MO[] $l10n An array of all currently loaded text domains.
1428   *
1429   * @param string $domain Text domain. Unique identifier for retrieving translated strings.
1430   * @return bool Whether there are translations.
1431   */
1432  function is_textdomain_loaded( $domain ) {
1433      global $l10n;
1434      return isset( $l10n[ $domain ] ) && ! $l10n[ $domain ] instanceof NOOP_Translations;
1435  }
1436  
1437  /**
1438   * Translates role name.
1439   *
1440   * Since the role names are in the database and not in the source there
1441   * are dummy gettext calls to get them into the POT file and this function
1442   * properly translates them back.
1443   *
1444   * The before_last_bar() call is needed, because older installations keep the roles
1445   * using the old context format: 'Role name|User role' and just skipping the
1446   * content after the last bar is easier than fixing them in the DB. New installations
1447   * won't suffer from that problem.
1448   *
1449   * @since 2.8.0
1450   * @since 5.2.0 Added the `$domain` parameter.
1451   *
1452   * @param string $name   The role name.
1453   * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
1454   *                       Default 'default'.
1455   * @return string Translated role name on success, original name on failure.
1456   */
1457  function translate_user_role( $name, $domain = 'default' ) {
1458      return translate_with_gettext_context( before_last_bar( $name ), 'User role', $domain );
1459  }
1460  
1461  /**
1462   * Gets all available languages based on the presence of *.mo and *.l10n.php files in a given directory.
1463   *
1464   * The default directory is WP_LANG_DIR.
1465   *
1466   * @since 3.0.0
1467   * @since 4.7.0 The results are now filterable with the {@see 'get_available_languages'} filter.
1468   * @since 6.5.0 The initial file list is now cached and also takes into account *.l10n.php files.
1469   *
1470   * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
1471   *
1472   * @param string $dir A directory to search for language files.
1473   *                    Default WP_LANG_DIR.
1474   * @return string[] An array of language codes or an empty array if no languages are present.
1475   *                  Language codes are formed by stripping the file extension from the language file names.
1476   */
1477  function get_available_languages( $dir = null ) {
1478      global $wp_textdomain_registry;
1479  
1480      $languages = array();
1481  
1482      $path       = is_null( $dir ) ? WP_LANG_DIR : $dir;
1483      $lang_files = $wp_textdomain_registry->get_language_files_from_path( $path );
1484  
1485      if ( $lang_files ) {
1486          foreach ( $lang_files as $lang_file ) {
1487              $lang_file = basename( $lang_file, '.mo' );
1488              $lang_file = basename( $lang_file, '.l10n.php' );
1489  
1490              if ( ! str_starts_with( $lang_file, 'continents-cities' ) && ! str_starts_with( $lang_file, 'ms-' ) &&
1491                  ! str_starts_with( $lang_file, 'admin-' ) ) {
1492                  $languages[] = $lang_file;
1493              }
1494          }
1495      }
1496  
1497      /**
1498       * Filters the list of available language codes.
1499       *
1500       * @since 4.7.0
1501       *
1502       * @param string[] $languages An array of available language codes.
1503       * @param string   $dir       The directory where the language files were found.
1504       */
1505      return apply_filters( 'get_available_languages', array_unique( $languages ), $dir );
1506  }
1507  
1508  /**
1509   * Gets installed translations.
1510   *
1511   * Looks in the wp-content/languages directory for translations of
1512   * plugins or themes.
1513   *
1514   * @since 3.7.0
1515   *
1516   * @global WP_Textdomain_Registry $wp_textdomain_registry WordPress Textdomain Registry.
1517   *
1518   * @param string $type What to search for. Accepts 'plugins', 'themes', 'core'.
1519   * @return array Array of language data.
1520   */
1521  function wp_get_installed_translations( $type ) {
1522      global $wp_textdomain_registry;
1523  
1524      if ( 'themes' !== $type && 'plugins' !== $type && 'core' !== $type ) {
1525          return array();
1526      }
1527  
1528      $dir = 'core' === $type ? WP_LANG_DIR : WP_LANG_DIR . "/$type";
1529  
1530      if ( ! is_dir( $dir ) ) {
1531          return array();
1532      }
1533  
1534      $files = $wp_textdomain_registry->get_language_files_from_path( $dir );
1535      if ( ! $files ) {
1536          return array();
1537      }
1538  
1539      $language_data = array();
1540  
1541      foreach ( $files as $file ) {
1542          if ( ! preg_match( '/(?:(.+)-)?([a-z]{2,3}(?:_[A-Z]{2})?(?:_[a-z0-9]+)?)\.(?:mo|l10n\.php)/', basename( $file ), $match ) ) {
1543              continue;
1544          }
1545  
1546          list( , $textdomain, $language ) = $match;
1547          if ( '' === $textdomain ) {
1548              $textdomain = 'default';
1549          }
1550  
1551          if ( str_ends_with( $file, '.mo' ) ) {
1552              $pofile = substr_replace( $file, '.po', - strlen( '.mo' ) );
1553  
1554              if ( ! file_exists( $pofile ) ) {
1555                  continue;
1556              }
1557  
1558              $language_data[ $textdomain ][ $language ] = wp_get_pomo_file_data( $pofile );
1559          } else {
1560              $pofile = substr_replace( $file, '.po', - strlen( '.l10n.php' ) );
1561  
1562              // If both a PO and a PHP file exist, prefer the PO file.
1563              if ( file_exists( $pofile ) ) {
1564                  continue;
1565              }
1566  
1567              $language_data[ $textdomain ][ $language ] = wp_get_l10n_php_file_data( $file );
1568          }
1569      }
1570      return $language_data;
1571  }
1572  
1573  /**
1574   * Extracts headers from a PO file.
1575   *
1576   * @since 3.7.0
1577   *
1578   * @param string $po_file Path to PO file.
1579   * @return string[] Array of PO file header values keyed by header name.
1580   */
1581  function wp_get_pomo_file_data( $po_file ) {
1582      $headers = get_file_data(
1583          $po_file,
1584          array(
1585              'POT-Creation-Date'  => '"POT-Creation-Date',
1586              'PO-Revision-Date'   => '"PO-Revision-Date',
1587              'Project-Id-Version' => '"Project-Id-Version',
1588              'X-Generator'        => '"X-Generator',
1589          )
1590      );
1591      foreach ( $headers as $header => $value ) {
1592          // Remove possible contextual '\n' and closing double quote.
1593          $headers[ $header ] = preg_replace( '~(\\\n)?"$~', '', $value );
1594      }
1595      return $headers;
1596  }
1597  
1598  /**
1599   * Extracts headers from a PHP translation file.
1600   *
1601   * @since 6.6.0
1602   *
1603   * @param string $php_file Path to a `.l10n.php` file.
1604   * @return string[] Array of file header values keyed by header name.
1605   */
1606  function wp_get_l10n_php_file_data( $php_file ) {
1607      $data = (array) include $php_file;
1608  
1609      unset( $data['messages'] );
1610      $headers = array(
1611          'POT-Creation-Date'  => 'pot-creation-date',
1612          'PO-Revision-Date'   => 'po-revision-date',
1613          'Project-Id-Version' => 'project-id-version',
1614          'X-Generator'        => 'x-generator',
1615      );
1616  
1617      $result = array(
1618          'POT-Creation-Date'  => '',
1619          'PO-Revision-Date'   => '',
1620          'Project-Id-Version' => '',
1621          'X-Generator'        => '',
1622      );
1623  
1624      foreach ( $headers as $po_header => $php_header ) {
1625          if ( isset( $data[ $php_header ] ) ) {
1626              $result[ $po_header ] = $data[ $php_header ];
1627          }
1628      }
1629  
1630      return $result;
1631  }
1632  
1633  /**
1634   * Displays or returns a Language selector.
1635   *
1636   * @since 4.0.0
1637   * @since 4.3.0 Introduced the `echo` argument.
1638   * @since 4.7.0 Introduced the `show_option_site_default` argument.
1639   * @since 5.1.0 Introduced the `show_option_en_us` argument.
1640   * @since 5.9.0 Introduced the `explicit_option_en_us` argument.
1641   *
1642   * @see get_available_languages()
1643   * @see wp_get_available_translations()
1644   *
1645   * @param string|array $args {
1646   *     Optional. Array or string of arguments for outputting the language selector.
1647   *
1648   *     @type string   $id                           ID attribute of the select element. Default 'locale'.
1649   *     @type string   $name                         Name attribute of the select element. Default 'locale'.
1650   *     @type string[] $languages                    List of installed languages, contain only the locales.
1651   *                                                  Default empty array.
1652   *     @type array    $translations                 List of available translations. Default result of
1653   *                                                  wp_get_available_translations().
1654   *     @type string   $selected                     Language which should be selected. Default empty.
1655   *     @type bool|int $echo                         Whether to echo the generated markup. Accepts 0, 1, or their
1656   *                                                  boolean equivalents. Default 1.
1657   *     @type bool     $show_available_translations  Whether to show available translations. Default true.
1658   *     @type bool     $show_option_site_default     Whether to show an option to fall back to the site's locale. Default false.
1659   *     @type bool     $show_option_en_us            Whether to show an option for English (United States). Default true.
1660   *     @type bool     $explicit_option_en_us        Whether the English (United States) option uses an explicit value of en_US
1661   *                                                  instead of an empty value. Default false.
1662   * }
1663   * @return string HTML dropdown list of languages.
1664   */
1665  function wp_dropdown_languages( $args = array() ) {
1666  
1667      $parsed_args = wp_parse_args(
1668          $args,
1669          array(
1670              'id'                          => 'locale',
1671              'name'                        => 'locale',
1672              'languages'                   => array(),
1673              'translations'                => array(),
1674              'selected'                    => '',
1675              'echo'                        => 1,
1676              'show_available_translations' => true,
1677              'show_option_site_default'    => false,
1678              'show_option_en_us'           => true,
1679              'explicit_option_en_us'       => false,
1680          )
1681      );
1682  
1683      // Bail if no ID or no name.
1684      if ( ! $parsed_args['id'] || ! $parsed_args['name'] ) {
1685          return;
1686      }
1687  
1688      // English (United States) uses an empty string for the value attribute.
1689      if ( 'en_US' === $parsed_args['selected'] && ! $parsed_args['explicit_option_en_us'] ) {
1690          $parsed_args['selected'] = '';
1691      }
1692  
1693      $translations = $parsed_args['translations'];
1694      if ( empty( $translations ) ) {
1695          require_once  ABSPATH . 'wp-admin/includes/translation-install.php';
1696          $translations = wp_get_available_translations();
1697      }
1698  
1699      /*
1700       * $parsed_args['languages'] should only contain the locales. Find the locale in
1701       * $translations to get the native name. Fall back to locale.
1702       */
1703      $languages = array();
1704      foreach ( $parsed_args['languages'] as $locale ) {
1705          if ( isset( $translations[ $locale ] ) ) {
1706              $translation = $translations[ $locale ];
1707              $languages[] = array(
1708                  'language'    => $translation['language'],
1709                  'native_name' => $translation['native_name'],
1710                  'lang'        => current( $translation['iso'] ),
1711              );
1712  
1713              // Remove installed language from available translations.
1714              unset( $translations[ $locale ] );
1715          } else {
1716              $languages[] = array(
1717                  'language'    => $locale,
1718                  'native_name' => $locale,
1719                  'lang'        => '',
1720              );
1721          }
1722      }
1723  
1724      $translations_available = ( ! empty( $translations ) && $parsed_args['show_available_translations'] );
1725  
1726      // Holds the HTML markup.
1727      $structure = array();
1728  
1729      // List installed languages.
1730      if ( $translations_available ) {
1731          $structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';
1732      }
1733  
1734      // Site default.
1735      if ( $parsed_args['show_option_site_default'] ) {
1736          $structure[] = sprintf(
1737              '<option value="site-default" data-installed="1"%s>%s</option>',
1738              selected( 'site-default', $parsed_args['selected'], false ),
1739              _x( 'Site Default', 'default site language' )
1740          );
1741      }
1742  
1743      if ( $parsed_args['show_option_en_us'] ) {
1744          $value       = ( $parsed_args['explicit_option_en_us'] ) ? 'en_US' : '';
1745          $structure[] = sprintf(
1746              '<option value="%s" lang="en" data-installed="1"%s>English (United States)</option>',
1747              esc_attr( $value ),
1748              selected( '', $parsed_args['selected'], false )
1749          );
1750      }
1751  
1752      // List installed languages.
1753      foreach ( $languages as $language ) {
1754          $structure[] = sprintf(
1755              '<option value="%s" lang="%s"%s data-installed="1">%s</option>',
1756              esc_attr( $language['language'] ),
1757              esc_attr( $language['lang'] ),
1758              selected( $language['language'], $parsed_args['selected'], false ),
1759              esc_html( $language['native_name'] )
1760          );
1761      }
1762      if ( $translations_available ) {
1763          $structure[] = '</optgroup>';
1764      }
1765  
1766      // List available translations.
1767      if ( $translations_available ) {
1768          $structure[] = '<optgroup label="' . esc_attr_x( 'Available', 'translations' ) . '">';
1769          foreach ( $translations as $translation ) {
1770              $structure[] = sprintf(
1771                  '<option value="%s" lang="%s"%s>%s</option>',
1772                  esc_attr( $translation['language'] ),
1773                  esc_attr( current( $translation['iso'] ) ),
1774                  selected( $translation['language'], $parsed_args['selected'], false ),
1775                  esc_html( $translation['native_name'] )
1776              );
1777          }
1778          $structure[] = '</optgroup>';
1779      }
1780  
1781      // Combine the output string.
1782      $output  = sprintf( '<select name="%s" id="%s">', esc_attr( $parsed_args['name'] ), esc_attr( $parsed_args['id'] ) );
1783      $output .= implode( "\n", $structure );
1784      $output .= '</select>';
1785  
1786      if ( $parsed_args['echo'] ) {
1787          echo $output;
1788      }
1789  
1790      return $output;
1791  }
1792  
1793  /**
1794   * Determines whether the current locale is right-to-left (RTL).
1795   *
1796   * For more information on this and similar theme functions, check out
1797   * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
1798   * Conditional Tags} article in the Theme Developer Handbook.
1799   *
1800   * @since 3.0.0
1801   *
1802   * @global WP_Locale $wp_locale WordPress date and time locale object.
1803   *
1804   * @return bool Whether locale is RTL.
1805   */
1806  function is_rtl() {
1807      global $wp_locale;
1808      if ( ! ( $wp_locale instanceof WP_Locale ) ) {
1809          return false;
1810      }
1811      return $wp_locale->is_rtl();
1812  }
1813  
1814  /**
1815   * Switches the translations according to the given locale.
1816   *
1817   * @since 4.7.0
1818   *
1819   * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
1820   *
1821   * @param string $locale The locale.
1822   * @return bool True on success, false on failure.
1823   */
1824  function switch_to_locale( $locale ) {
1825      /* @var WP_Locale_Switcher $wp_locale_switcher */
1826      global $wp_locale_switcher;
1827  
1828      if ( ! $wp_locale_switcher ) {
1829          return false;
1830      }
1831  
1832      return $wp_locale_switcher->switch_to_locale( $locale );
1833  }
1834  
1835  /**
1836   * Switches the translations according to the given user's locale.
1837   *
1838   * @since 6.2.0
1839   *
1840   * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
1841   *
1842   * @param int $user_id User ID.
1843   * @return bool True on success, false on failure.
1844   */
1845  function switch_to_user_locale( $user_id ) {
1846      /* @var WP_Locale_Switcher $wp_locale_switcher */
1847      global $wp_locale_switcher;
1848  
1849      if ( ! $wp_locale_switcher ) {
1850          return false;
1851      }
1852  
1853      return $wp_locale_switcher->switch_to_user_locale( $user_id );
1854  }
1855  
1856  /**
1857   * Restores the translations according to the previous locale.
1858   *
1859   * @since 4.7.0
1860   *
1861   * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
1862   *
1863   * @return string|false Locale on success, false on error.
1864   */
1865  function restore_previous_locale() {
1866      /* @var WP_Locale_Switcher $wp_locale_switcher */
1867      global $wp_locale_switcher;
1868  
1869      if ( ! $wp_locale_switcher ) {
1870          return false;
1871      }
1872  
1873      return $wp_locale_switcher->restore_previous_locale();
1874  }
1875  
1876  /**
1877   * Restores the translations according to the original locale.
1878   *
1879   * @since 4.7.0
1880   *
1881   * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
1882   *
1883   * @return string|false Locale on success, false on error.
1884   */
1885  function restore_current_locale() {
1886      /* @var WP_Locale_Switcher $wp_locale_switcher */
1887      global $wp_locale_switcher;
1888  
1889      if ( ! $wp_locale_switcher ) {
1890          return false;
1891      }
1892  
1893      return $wp_locale_switcher->restore_current_locale();
1894  }
1895  
1896  /**
1897   * Determines whether switch_to_locale() is in effect.
1898   *
1899   * @since 4.7.0
1900   *
1901   * @global WP_Locale_Switcher $wp_locale_switcher WordPress locale switcher object.
1902   *
1903   * @return bool True if the locale has been switched, false otherwise.
1904   */
1905  function is_locale_switched() {
1906      /* @var WP_Locale_Switcher $wp_locale_switcher */
1907      global $wp_locale_switcher;
1908  
1909      return $wp_locale_switcher->is_switched();
1910  }
1911  
1912  /**
1913   * Translates the provided settings value using its i18n schema.
1914   *
1915   * @since 5.9.0
1916   * @access private
1917   *
1918   * @param string|string[]|array[]|object $i18n_schema I18n schema for the setting.
1919   * @param string|string[]|array[]        $settings    Value for the settings.
1920   * @param string                         $textdomain  Textdomain to use with translations.
1921   *
1922   * @return string|string[]|array[] Translated settings.
1923   */
1924  function translate_settings_using_i18n_schema( $i18n_schema, $settings, $textdomain ) {
1925      if ( empty( $i18n_schema ) || empty( $settings ) || empty( $textdomain ) ) {
1926          return $settings;
1927      }
1928  
1929      if ( is_string( $i18n_schema ) && is_string( $settings ) ) {
1930          return translate_with_gettext_context( $settings, $i18n_schema, $textdomain );
1931      }
1932      if ( is_array( $i18n_schema ) && is_array( $settings ) ) {
1933          $translated_settings = array();
1934          foreach ( $settings as $value ) {
1935              $translated_settings[] = translate_settings_using_i18n_schema( $i18n_schema[0], $value, $textdomain );
1936          }
1937          return $translated_settings;
1938      }
1939      if ( is_object( $i18n_schema ) && is_array( $settings ) ) {
1940          $group_key           = '*';
1941          $translated_settings = array();
1942          foreach ( $settings as $key => $value ) {
1943              if ( isset( $i18n_schema->$key ) ) {
1944                  $translated_settings[ $key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $value, $textdomain );
1945              } elseif ( isset( $i18n_schema->$group_key ) ) {
1946                  $translated_settings[ $key ] = translate_settings_using_i18n_schema( $i18n_schema->$group_key, $value, $textdomain );
1947              } else {
1948                  $translated_settings[ $key ] = $value;
1949              }
1950          }
1951          return $translated_settings;
1952      }
1953      return $settings;
1954  }
1955  
1956  /**
1957   * Retrieves the list item separator based on the locale.
1958   *
1959   * @since 6.0.0
1960   *
1961   * @global WP_Locale $wp_locale WordPress date and time locale object.
1962   *
1963   * @return string Locale-specific list item separator.
1964   */
1965  function wp_get_list_item_separator() {
1966      global $wp_locale;
1967  
1968      if ( ! ( $wp_locale instanceof WP_Locale ) ) {
1969          // Default value of WP_Locale::get_list_item_separator().
1970          /* translators: Used between list items, there is a space after the comma. */
1971          return __( ', ' );
1972      }
1973  
1974      return $wp_locale->get_list_item_separator();
1975  }
1976  
1977  /**
1978   * Retrieves the word count type based on the locale.
1979   *
1980   * @since 6.2.0
1981   *
1982   * @global WP_Locale $wp_locale WordPress date and time locale object.
1983   *
1984   * @return string Locale-specific word count type. Possible values are `characters_excluding_spaces`,
1985   *                `characters_including_spaces`, or `words`. Defaults to `words`.
1986   */
1987  function wp_get_word_count_type() {
1988      global $wp_locale;
1989  
1990      if ( ! ( $wp_locale instanceof WP_Locale ) ) {
1991          // Default value of WP_Locale::get_word_count_type().
1992          return 'words';
1993      }
1994  
1995      return $wp_locale->get_word_count_type();
1996  }
1997  
1998  /**
1999   * Returns a boolean to indicate whether a translation exists for a given string with optional text domain and locale.
2000   *
2001   * @since 6.7.0
2002   *
2003   * @param string  $singular   Singular translation to check.
2004   * @param string  $textdomain Optional. Text domain. Default 'default'.
2005   * @param ?string $locale     Optional. Locale. Default current locale.
2006   * @return bool  True if the translation exists, false otherwise.
2007   */
2008  function has_translation( string $singular, string $textdomain = 'default', ?string $locale = null ): bool {
2009      return WP_Translation_Controller::get_instance()->has_translation( $singular, $textdomain, $locale );
2010  }


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref