WebOrbiton
v1.0.0.1

StocketBase

99 lines · 2.9 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. 'back_to_top' => 'Back to top',
  47. 'reading_progress_left' => '{percent}% left',
  48. 'breadcrumb_label' => 'Breadcrumb',
  49. 'product_tags_label' => 'Tags:',
  50. 'tag_page_title' => 'Tagged: {name}',
  51. 'tag_not_found' => 'Tag not found.',
  52. 'tag_no_products' => 'No published products with this tag yet.',
  53. 'subscription_per_month' => 'month',
  54. 'subscription_per_year' => 'year',
  55. 'page_title_not_found' => 'Not found',
  56. 'pagination_label' => 'Pages',
  57. 'product_rating_aria' => 'Average rating {rating} out of 5',
  58. 'antibot_image_label' => 'Security code',
  59. 'checkout_discount_name' => 'Discount',
  60. 'subscription_status_unpaid' => 'unpaid',
  61. 'subscription_status_incomplete' => 'waiting for payment',
  62. 'subscription_status_incomplete_expired' => 'expired',
  63. 'subscription_status_paused' => 'paused',
  64. ];
  65. ​
  66. public static function ensureKeys(array $defaults): void
  67. {
  68. $current = self::base();
  69. $missing = array_diff_key($defaults, $current);
  70. ​
  71. if (empty($current) || empty($missing)) {
  72. return;
  73. }
  74. ​
  75. self::save($current + $missing);
  76. }
  77. ​
  78. public static function save(array $strings): bool
  79. {
  80. self::$merged = [];
  81. ​
  82. $clean = [];
  83. foreach ($strings as $key => $value) {
  84. if (!is_string($key)) {
  85. continue;
  86. }
  87. $clean[$key] = strip_tags((string) $value);
  88. }
  89. ​
  90. $export = "<?php\n\ndeclare(strict_types=1);\n\nreturn [\n";
  91. foreach ($clean as $key => $value) {
  92. $export .= " " . var_export($key, true) . " => " . var_export($value, true) . ",\n";
  93. }
  94. $export .= "];\n";
  95. ​
  96. return file_put_contents(self::FILE_PATH, $export) !== false;
  97. }
  98. }
  99. ​