PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.0
28.6 28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 All 130 releases
wordpress-seo / admin / class-admin-asset-yoast-components-l10n.php

class-admin-asset-yoast-components-l10n.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.0, at admin/class-admin-asset-yoast-components-l10n.php

64 lines 1.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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