WebOrbiton
v1.1.0.0

StocketBase

110 lines · 3.7 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. 'consent_title' => 'Cookie settings',
  65. 'consent_manage_button' => 'Manage',
  66. 'consent_save' => 'Save choices',
  67. 'consent_necessary_title' => 'Necessary',
  68. 'consent_necessary_desc' => 'Needed for the store to work, for example your cart and these choices. Always on.',
  69. 'consent_analytics_title' => 'Analytics',
  70. 'consent_analytics_desc' => 'Helps us see which products people look at, so we can make the store better.',
  71. 'consent_marketing_title' => 'Marketing',
  72. 'consent_marketing_desc' => 'Lets advertising partners show ads that fit your interests.',
  73. 'consent_preferences_title' => 'Preferences',
  74. 'consent_preferences_desc' => 'Remembers your choices and powers extras like chat.',
  75. ];
  76. ​
  77. public static function ensureKeys(array $defaults): void
  78. {
  79. $current = self::base();
  80. $missing = array_diff_key($defaults, $current);
  81. ​
  82. if (empty($current) || empty($missing)) {
  83. return;
  84. }
  85. ​
  86. self::save($current + $missing);
  87. }
  88. ​
  89. public static function save(array $strings): bool
  90. {
  91. self::$merged = [];
  92. ​
  93. $clean = [];
  94. foreach ($strings as $key => $value) {
  95. if (!is_string($key)) {
  96. continue;
  97. }
  98. $clean[$key] = strip_tags((string) $value);
  99. }
  100. ​
  101. $export = "<?php\n\ndeclare(strict_types=1);\n\nreturn [\n";
  102. foreach ($clean as $key => $value) {
  103. $export .= " " . var_export($key, true) . " => " . var_export($value, true) . ",\n";
  104. }
  105. $export .= "];\n";
  106. ​
  107. return file_put_contents(self::FILE_PATH, $export) !== false;
  108. }
  109. }
  110. ​