class-admin-asset-yoast-components-l10n.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * @package WPSEO\Admin
  4. */
  5. /**
  6. * Localizes JavaScript files.
  7. */
  8. final class WPSEO_Admin_Asset_Yoast_Components_L10n {
  9. /**
  10. * Localizes the given script with the JavaScript translations.
  11. *
  12. * @param string $script_handle The script handle to localize for.
  13. *
  14. * @return void
  15. */
  16. public function localize_script( $script_handle ) {
  17. wp_localize_script( $script_handle, 'wpseoYoastJSL10n', array(
  18. 'yoast-components' => $this->get_translations( 'yoast-components' ),
  19. 'wordpress-seo' => $this->get_translations( 'wordpress-seojs' ),
  20. ) );
  21. }
  22. /**
  23. * Returns translations necessary for JS files.
  24. *
  25. * @param string $component The component to retrieve the translations for.
  26. * @return object The translations in a Jed format for JS files.
  27. */
  28. protected function get_translations( $component ) {
  29. $locale = WPSEO_Utils::get_user_locale();
  30. $file = plugin_dir_path( WPSEO_FILE ) . 'languages/' . $component . '-' . $locale . '.json';
  31. if ( file_exists( $file ) ) {
  32. $file = file_get_contents( $file );
  33. if ( is_string( $file ) && $file !== '' ) {
  34. return json_decode( $file, true );
  35. }
  36. }
  37. return null;
  38. }
  39. }