[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-textdomain-registry.php (source)

   1  <?php
   2  /**
   3   * Locale API: WP_Textdomain_Registry class
   4   *
   5   * @package WordPress
   6   * @subpackage i18n
   7   * @since 5.6.0
   8   */
   9  
  10  /**
  11   * Core class used for registering text domains.
  12   *
  13   * @since 5.6.0
  14   */
  15  class WP_Textdomain_Registry {
  16      /**
  17       * List of domains and their language directory paths.
  18       *
  19       * @since 5.6.0
  20       *
  21       * @var array
  22       */
  23      protected $domains = array();
  24  
  25      /**
  26       * Holds a cached list of available .mo files to improve performance.
  27       *
  28       * @since 5.6.0
  29       *
  30       * @var array
  31       */
  32      protected $cached_mo_files;
  33  
  34      /**
  35       * Returns the MO file path for a specific domain.
  36       *
  37       * @since 5.6.0
  38       *
  39       * @param string $domain Text domain.
  40       * @return string|false MO file path or false if there is none available.
  41       */
  42  	public function get( $domain ) {
  43          if ( isset( $this->domains[ $domain ] ) ) {
  44              return $this->domains[ $domain ];
  45          }
  46  
  47          return $this->get_path_from_lang_dir( $domain );
  48      }
  49  
  50      /**
  51       * Sets the MO file path for a specific domain.
  52       *
  53       * @since 5.6.0
  54       *
  55       * @param string $domain Text domain.
  56       * @param string|false $path Language directory path or false if there is none available.
  57       */
  58  	public function set( $domain, $path ) {
  59          $this->domains[ $domain ] = $path ? trailingslashit( $path ) : false;
  60      }
  61  
  62      /**
  63       * Resets the registry state.
  64       *
  65       * @since 5.6.0
  66       */
  67  	public function reset() {
  68          $this->cached_mo_files = null;
  69          $this->domains         = array();
  70      }
  71  
  72      /**
  73       * Gets the path to a translation file in the languages directory for the current locale.
  74       *
  75       * @since 5.6.0
  76       *
  77       * @param string $domain Text domain.
  78       * @return string|false MO file path or false if there is none available.
  79       */
  80  	private function get_path_from_lang_dir( $domain ) {
  81          if ( null === $this->cached_mo_files ) {
  82              $this->cached_mo_files = array();
  83  
  84              $this->set_cached_mo_files();
  85          }
  86  
  87          $locale = determine_locale();
  88          $mofile = "{$domain}-{$locale}.mo";
  89  
  90          $path = WP_LANG_DIR . '/plugins/' . $mofile;
  91          if ( in_array( $path, $this->cached_mo_files, true ) ) {
  92              $path = WP_LANG_DIR . '/plugins/';
  93              $this->set( $domain, $path );
  94  
  95              return $path;
  96          }
  97  
  98          $path = WP_LANG_DIR . '/themes/' . $mofile;
  99          if ( in_array( $path, $this->cached_mo_files, true ) ) {
 100              $path = WP_LANG_DIR . '/themes/';
 101              $this->set( $domain, $path );
 102  
 103              return $path;
 104          }
 105  
 106          $this->set( $domain, false );
 107  
 108          return false;
 109      }
 110  
 111      /**
 112       * Reads and caches all available MO files from the plugins and themes language directories.
 113       *
 114       * @since 5.6.0
 115       */
 116  	protected function set_cached_mo_files() {
 117          $locations = array(
 118              WP_LANG_DIR . '/plugins',
 119              WP_LANG_DIR . '/themes',
 120          );
 121  
 122          foreach ( $locations as $location ) {
 123              $mo_files = glob( $location . '/*.mo' );
 124  
 125              if ( $mo_files ) {
 126                  $this->cached_mo_files = array_merge( $this->cached_mo_files, $mo_files );
 127              }
 128          }
 129      }
 130  }


Generated: Thu Nov 12 01:00:03 2020 Cross-referenced by PHPXref 0.7.1