L10n.php
167 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Internationalization helper. |
| 4 | * |
| 5 | * Inspired by Justin Tadlock and the work he did with hybrid-core. |
| 6 | * |
| 7 | * @package kirki-framework/l10n |
| 8 | * @author Themeum |
| 9 | * @copyright Copyright (c) 2023, Themeum |
| 10 | * @license https://opensource.org/licenses/MIT |
| 11 | * @since 1.0 |
| 12 | */ |
| 13 | |
| 14 | namespace Kirki; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Handles translations |
| 22 | */ |
| 23 | class L10n { |
| 24 | |
| 25 | /** |
| 26 | * The plugin textdomain |
| 27 | * |
| 28 | * @access private |
| 29 | * @since 1.0 |
| 30 | * @var string |
| 31 | */ |
| 32 | private $textdomain; |
| 33 | |
| 34 | /** |
| 35 | * The folder path containing translation files. |
| 36 | * |
| 37 | * @access private |
| 38 | * @since 1.0 |
| 39 | * @var string |
| 40 | */ |
| 41 | private $languages_path; |
| 42 | |
| 43 | /** |
| 44 | * The theme textdomain |
| 45 | * |
| 46 | * @access private |
| 47 | * @since 1.0 |
| 48 | * @var string |
| 49 | */ |
| 50 | private $theme_textdomain = ''; |
| 51 | |
| 52 | /** |
| 53 | * The class constructor. |
| 54 | * Adds actions & filters to handle the rest of the methods. |
| 55 | * |
| 56 | * @access public |
| 57 | * @since 1.0 |
| 58 | * @param string $textdomain The textdomain we want to use. Defaults to "kirki". |
| 59 | * @param string $languages_path The path to languages files. |
| 60 | */ |
| 61 | public function __construct( $textdomain = 'kirki', $languages_path = '' ) { |
| 62 | |
| 63 | $this->textdomain = $textdomain; |
| 64 | $this->languages_path = $languages_path; |
| 65 | // This will only work if we're inside a plugin. |
| 66 | add_action( 'init', [ $this, 'load_textdomain' ] ); |
| 67 | |
| 68 | // If we got this far, then Kirki is embedded in a plugin. |
| 69 | // We want the theme's textdomain to handle translations. |
| 70 | add_filter( 'override_load_textdomain', [ $this, 'override_load_textdomain' ], 5, 3 ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Load the plugin textdomain |
| 75 | * |
| 76 | * @access public |
| 77 | * @since 1.0 |
| 78 | */ |
| 79 | public function load_textdomain() { |
| 80 | if ( null !== $this->get_path() ) { |
| 81 | load_textdomain( $this->textdomain, $this->get_path() ); |
| 82 | } |
| 83 | load_plugin_textdomain( $this->textdomain, false, $this->languages_path ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Gets the path to a translation file. |
| 88 | * |
| 89 | * @access protected |
| 90 | * @since 1.0 |
| 91 | * @return string Absolute path to the translation file. |
| 92 | */ |
| 93 | protected function get_path() { |
| 94 | $path_found = false; |
| 95 | $found_path = null; |
| 96 | foreach ( $this->get_paths() as $path ) { |
| 97 | if ( $path_found ) { |
| 98 | continue; |
| 99 | } |
| 100 | $path = wp_normalize_path( $path ); |
| 101 | if ( file_exists( $path ) ) { |
| 102 | $path_found = true; |
| 103 | $found_path = $path; |
| 104 | } |
| 105 | } |
| 106 | return $found_path; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns an array of paths where translation files may be located. |
| 111 | * |
| 112 | * @access protected |
| 113 | * @since 1.0 |
| 114 | * @return array |
| 115 | */ |
| 116 | protected function get_paths() { |
| 117 | return [ |
| 118 | WP_LANG_DIR . '/' . $this->textdomain . '-' . get_locale() . '.mo', |
| 119 | trailingslashit( $this->languages_path ) . $this->textdomain . '-' . get_locale() . '.mo', |
| 120 | ]; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Allows overriding the textdomain from a theme. |
| 125 | * |
| 126 | * @access public |
| 127 | * @since 1.0 |
| 128 | * @param bool $override Whether to override the .mo file loading. Default false. |
| 129 | * @param string $domain Text domain. Unique identifier for retrieving translated strings. |
| 130 | * @param string $mofile Path to the MO file. |
| 131 | * @return bool |
| 132 | */ |
| 133 | public function override_load_textdomain( $override, $domain, $mofile ) { |
| 134 | global $l10n; |
| 135 | if ( isset( $l10n[ $this->get_theme_textdomain() ] ) ) { |
| 136 | $l10n[ $this->textdomain ] = $l10n[ $this->get_theme_textdomain() ]; // phpcs:ignore WordPress.WP.GlobalVariablesOverride |
| 137 | } |
| 138 | |
| 139 | // Check if the domain is the one we have defined. |
| 140 | if ( $this->textdomain === $domain ) { |
| 141 | return true; |
| 142 | } |
| 143 | return $override; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Get the theme's textdomain. |
| 148 | * |
| 149 | * @access private |
| 150 | * @since 1.0 |
| 151 | * @return string |
| 152 | */ |
| 153 | private function get_theme_textdomain() { |
| 154 | if ( '' === $this->theme_textdomain ) { |
| 155 | |
| 156 | // Get the textdomain. |
| 157 | $theme = wp_get_theme(); |
| 158 | $this->theme_textdomain = $theme->get( 'TextDomain' ); |
| 159 | |
| 160 | // If no texdomain was found, use the template folder name. |
| 161 | if ( ! $this->theme_textdomain ) { |
| 162 | $this->theme_textdomain = get_template(); |
| 163 | } |
| 164 | } |
| 165 | return $this->theme_textdomain; |
| 166 | } |
| 167 | } |