| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Localizes JavaScript files. |
| 10 |
*/ |
| 11 |
final class WPSEO_Admin_Asset_Yoast_Components_L10n { |
| 12 |
|
| 13 |
/** |
| 14 |
* Represents the asset manager. |
| 15 |
* |
| 16 |
* @var WPSEO_Admin_Asset_Manager |
| 17 |
*/ |
| 18 |
protected $asset_manager; |
| 19 |
|
| 20 |
/** |
| 21 |
* WPSEO_Admin_Asset_Yoast_Components_L10n constructor. |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
$this->asset_manager = new WPSEO_Admin_Asset_Manager(); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Localizes the given script with the JavaScript translations. |
| 29 |
* |
| 30 |
* @param string $script_handle The script handle to localize for. |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public function localize_script( $script_handle ) { |
| 35 |
$translations = [ |
| 36 |
'yoast-components' => $this->get_translations( 'yoast-components' ), |
| 37 |
'wordpress-seo' => $this->get_translations( 'wordpress-seojs' ), |
| 38 |
'yoast-schema-blocks' => $this->get_translations( 'yoast-schema-blocks' ), |
| 39 |
]; |
| 40 |
$this->asset_manager->localize_script( $script_handle, 'wpseoYoastJSL10n', $translations ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Returns translations necessary for JS files. |
| 45 |
* |
| 46 |
* @param string $component The component to retrieve the translations for. |
| 47 |
* @return object|null The translations in a Jed format for JS files. |
| 48 |
*/ |
| 49 |
protected function get_translations( $component ) { |
| 50 |
$locale = \get_user_locale(); |
| 51 |
|
| 52 |
$file = WPSEO_PATH . 'languages/' . $component . '-' . $locale . '.json'; |
| 53 |
if ( file_exists( $file ) ) { |
| 54 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Retrieving a local file. |
| 55 |
$file = file_get_contents( $file ); |
| 56 |
if ( is_string( $file ) && $file !== '' ) { |
| 57 |
return json_decode( $file, true ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return null; |
| 62 |
} |
| 63 |
} |
| 64 |
|