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


Generated: Sun Apr 5 01:00:03 2020 Cross-referenced by PHPXref 0.7.1