v1.0.0.0
StocketBase
- <?php
-
- declare(strict_types=1);
-
- require_once __DIR__ . '/languages.php';
-
- final class Language
- {
- private const FILE_PATH = __DIR__ . '/../translations/language.php';
-
- private static array $merged = [];
-
- public static function base(): array
- {
- if (!is_file(self::FILE_PATH)) {
- return [];
- }
-
- $strings = require self::FILE_PATH;
- return is_array($strings) ? $strings : [];
- }
-
- public static function all(): array
- {
- $code = Languages::current();
- if ($code === '') {
- return self::base();
- }
-
- if (!isset(self::$merged[$code])) {
- $translated = array_filter(Languages::strings($code), static fn($value): bool => is_string($value) && $value !== '');
- self::$merged[$code] = array_merge(self::base(), $translated);
- }
-
- return self::$merged[$code];
- }
-
- public static function get(string $key, string $default = ''): string
- {
- $strings = self::all();
- return (string) ($strings[$key] ?? $default);
- }
-
- public const ADDED_DEFAULTS = [
- 'language_switcher_label' => 'Language',
- 'article_toc_title' => 'Table of contents',
- 'back_to_top' => 'Back to top',
- 'reading_progress_left' => '{percent}% left',
- 'breadcrumb_label' => 'Breadcrumb',
- 'article_written_by' => 'Written by',
- 'author_articles_count' => 'Articles: {count}',
- 'artist_articles_title' => 'Articles by {name}',
- 'artist_not_found' => 'Author not found.',
- 'artist_website' => 'Website',
- 'artist_no_articles' => 'No published articles yet.',
- 'article_tags_label' => 'Tags:',
- 'tag_page_title' => 'Tagged: {name}',
- 'tag_not_found' => 'Tag not found.',
- 'tag_no_articles' => 'No published articles with this tag yet.',
- 'read_aloud_title' => 'Listen to this article',
- 'read_aloud_play' => 'Play',
- 'read_aloud_pause' => 'Pause',
- 'read_aloud_seek' => 'Seek',
- 'read_aloud_speed' => 'Playback speed',
- 'article_subscribers_only' => 'Subscribers only',
- 'article_subscribe_button' => 'Subscribe',
- 'article_login_button' => 'Log in',
- 'article_already_subscriber' => 'Already a subscriber?',
- 'article_buy_lifetime_button' => 'Get lifetime access',
- 'subscription_one_time' => 'one-time payment',
- 'subscription_per_month' => 'month',
- 'subscription_per_year' => 'year',
- ];
-
- public static function ensureKeys(array $defaults): void
- {
- $current = self::base();
- $missing = array_diff_key($defaults, $current);
-
- if (empty($current) || empty($missing)) {
- return;
- }
-
- self::save($current + $missing);
- }
-
- public static function save(array $strings): bool
- {
- self::$merged = [];
-
- $clean = [];
- foreach ($strings as $key => $value) {
- if (!is_string($key)) {
- continue;
- }
- $clean[$key] = strip_tags((string) $value);
- }
-
- $export = "<?php\n\ndeclare(strict_types=1);\n\nreturn [\n";
- foreach ($clean as $key => $value) {
- $export .= " " . var_export($key, true) . " => " . var_export($value, true) . ",\n";
- }
- $export .= "];\n";
-
- return file_put_contents(self::FILE_PATH, $export) !== false;
- }
- }
-