class-configuration-translations.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\ConfigurationUI
  6. */
  7. /**
  8. * Class WPSEO_Configuration_Structure.
  9. */
  10. class WPSEO_Configuration_Translations {
  11. /**
  12. * Registered steps.
  13. *
  14. * @var array
  15. */
  16. protected $translations = [];
  17. /**
  18. * The locale.
  19. *
  20. * @var string
  21. */
  22. protected $locale;
  23. /**
  24. * Sets the translations based on the file.
  25. *
  26. * @param string $locale The locale to retreive the translations for.
  27. */
  28. public function __construct( $locale ) {
  29. $this->locale = $locale;
  30. $this->translations = $this->get_translations_from_file();
  31. }
  32. /**
  33. * Retrieve the translations.
  34. *
  35. * @return array
  36. */
  37. public function retrieve() {
  38. return $this->translations;
  39. }
  40. /**
  41. * Retrieves the translations from the JSON-file.
  42. *
  43. * @return array Array with the translations.
  44. */
  45. protected function get_translations_from_file() {
  46. $file = plugin_dir_path( WPSEO_FILE ) . 'languages/yoast-components-' . $this->locale . '.json';
  47. if ( file_exists( $file ) ) {
  48. $file = file_get_contents( $file );
  49. if ( is_string( $file ) && $file !== '' ) {
  50. return json_decode( $file, true );
  51. }
  52. }
  53. return [];
  54. }
  55. }