WebOrbiton
v1.0.0.0

StocketBase

108 lines · 3.3 KB
  1. <?php
  2. ​
  3. declare(strict_types=1);
  4. ​
  5. require_once __DIR__ . '/languages.php';
  6. ​
  7. final class Language
  8. {
  9. private const FILE_PATH = __DIR__ . '/../translations/language.php';
  10. ​
  11. private static array $merged = [];
  12. ​
  13. public static function base(): array
  14. {
  15. if (!is_file(self::FILE_PATH)) {
  16. return [];
  17. }
  18. ​
  19. $strings = require self::FILE_PATH;
  20. return is_array($strings) ? $strings : [];
  21. }
  22. ​
  23. public static function all(): array
  24. {
  25. $code = Languages::current();
  26. if ($code === '') {
  27. return self::base();
  28. }
  29. ​
  30. if (!isset(self::$merged[$code])) {
  31. $translated = array_filter(Languages::strings($code), static fn($value): bool => is_string($value) && $value !== '');
  32. self::$merged[$code] = array_merge(self::base(), $translated);
  33. }
  34. ​
  35. return self::$merged[$code];
  36. }
  37. ​
  38. public static function get(string $key, string $default = ''): string
  39. {
  40. $strings = self::all();
  41. return (string) ($strings[$key] ?? $default);
  42. }
  43. ​
  44. public const ADDED_DEFAULTS = [
  45. 'language_switcher_label' => 'Language',
  46. 'article_toc_title' => 'Table of contents',
  47. 'back_to_top' => 'Back to top',
  48. 'reading_progress_left' => '{percent}% left',
  49. 'breadcrumb_label' => 'Breadcrumb',
  50. 'article_written_by' => 'Written by',
  51. 'author_articles_count' => 'Articles: {count}',
  52. 'artist_articles_title' => 'Articles by {name}',
  53. 'artist_not_found' => 'Author not found.',
  54. 'artist_website' => 'Website',
  55. 'artist_no_articles' => 'No published articles yet.',
  56. 'article_tags_label' => 'Tags:',
  57. 'tag_page_title' => 'Tagged: {name}',
  58. 'tag_not_found' => 'Tag not found.',
  59. 'tag_no_articles' => 'No published articles with this tag yet.',
  60. 'read_aloud_title' => 'Listen to this article',
  61. 'read_aloud_play' => 'Play',
  62. 'read_aloud_pause' => 'Pause',
  63. 'read_aloud_seek' => 'Seek',
  64. 'read_aloud_speed' => 'Playback speed',
  65. 'article_subscribers_only' => 'Subscribers only',
  66. 'article_subscribe_button' => 'Subscribe',
  67. 'article_login_button' => 'Log in',
  68. 'article_already_subscriber' => 'Already a subscriber?',
  69. 'article_buy_lifetime_button' => 'Get lifetime access',
  70. 'subscription_one_time' => 'one-time payment',
  71. 'subscription_per_month' => 'month',
  72. 'subscription_per_year' => 'year',
  73. ];
  74. ​
  75. public static function ensureKeys(array $defaults): void
  76. {
  77. $current = self::base();
  78. $missing = array_diff_key($defaults, $current);
  79. ​
  80. if (empty($current) || empty($missing)) {
  81. return;
  82. }
  83. ​
  84. self::save($current + $missing);
  85. }
  86. ​
  87. public static function save(array $strings): bool
  88. {
  89. self::$merged = [];
  90. ​
  91. $clean = [];
  92. foreach ($strings as $key => $value) {
  93. if (!is_string($key)) {
  94. continue;
  95. }
  96. $clean[$key] = strip_tags((string) $value);
  97. }
  98. ​
  99. $export = "<?php\n\ndeclare(strict_types=1);\n\nreturn [\n";
  100. foreach ($clean as $key => $value) {
  101. $export .= " " . var_export($key, true) . " => " . var_export($value, true) . ",\n";
  102. }
  103. $export .= "];\n";
  104. ​
  105. return file_put_contents(self::FILE_PATH, $export) !== false;
  106. }
  107. }
  108. ​