| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jfcherng\Diff\Utility; |
| 6 |
|
| 7 |
final class Language |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @var string[] the translation dict |
| 11 |
*/ |
| 12 |
private array $translations = []; |
| 13 |
|
| 14 |
/** |
| 15 |
* @var string the language name |
| 16 |
*/ |
| 17 |
private string $language = '_custom_'; |
| 18 |
|
| 19 |
/** |
| 20 |
* The constructor. |
| 21 |
* |
| 22 |
* @param array<int,string|string[]>|string|string[] $target the language ID or translations dict |
| 23 |
*/ |
| 24 |
public function __construct($target = 'eng') |
| 25 |
{ |
| 26 |
$this->load($target); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Gets the language. |
| 31 |
* |
| 32 |
* @return string the language |
| 33 |
*/ |
| 34 |
public function getLanguage(): string |
| 35 |
{ |
| 36 |
return $this->language; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Gets the translations. |
| 41 |
* |
| 42 |
* @return array the translations |
| 43 |
*/ |
| 44 |
public function getTranslations(): array |
| 45 |
{ |
| 46 |
return $this->translations; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Loads the target language. |
| 51 |
* |
| 52 |
* @param array<int,string|string[]>|string|string[] $target the language ID or translations dict |
| 53 |
*/ |
| 54 |
public function load($target): void |
| 55 |
{ |
| 56 |
$this->translations = $this->resolve($target); |
| 57 |
$this->language = \is_string($target) ? $target : '_custom_'; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Translates the text. |
| 62 |
* |
| 63 |
* @param string $text the text |
| 64 |
*/ |
| 65 |
public function translate(string $text): string |
| 66 |
{ |
| 67 |
return $this->translations[$text] ?? "![{$text}]"; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get the translations from the language file. |
| 72 |
* |
| 73 |
* @param string $language the language |
| 74 |
* |
| 75 |
* @throws \Exception fail to decode the JSON file |
| 76 |
* @throws \LogicException path is a directory |
| 77 |
* @throws \RuntimeException path cannot be opened |
| 78 |
* |
| 79 |
* @return string[] |
| 80 |
*/ |
| 81 |
private static function getTranslationsByLanguage(string $language): array |
| 82 |
{ |
| 83 |
$filePath = __DIR__ . "/../languages/{$language}.json"; |
| 84 |
$file = new \SplFileObject($filePath, 'r'); |
| 85 |
$fileContent = $file->fread($file->getSize()); |
| 86 |
|
| 87 |
try { |
| 88 |
$decoded = json_decode($fileContent, true, 512, \JSON_THROW_ON_ERROR); |
| 89 |
} catch (\JsonException $e) { |
| 90 |
throw new \Exception(sprintf('Fail to decode JSON file (%s): %s', realpath($filePath), (string) $e)); |
| 91 |
} |
| 92 |
|
| 93 |
return (array) $decoded; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Resolves the target language. |
| 98 |
* |
| 99 |
* @param array<int,string|string[]>|string|string[] $target the language ID or translations array |
| 100 |
* |
| 101 |
* @throws \InvalidArgumentException |
| 102 |
* |
| 103 |
* @return string[] the resolved translations |
| 104 |
*/ |
| 105 |
private function resolve($target): array |
| 106 |
{ |
| 107 |
if (\is_string($target)) { |
| 108 |
return self::getTranslationsByLanguage($target); |
| 109 |
} |
| 110 |
|
| 111 |
if (\is_array($target)) { |
| 112 |
// $target is an associative array |
| 113 |
if (Arr::isAssociative($target)) { |
| 114 |
return $target; |
| 115 |
} |
| 116 |
|
| 117 |
// $target is a list of "key-value pairs or language ID" |
| 118 |
return array_reduce( |
| 119 |
$target, |
| 120 |
fn (array $carry, $translation): array => array_merge($carry, $this->resolve($translation)), |
| 121 |
[], |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
throw new \InvalidArgumentException('$target is not in valid form'); |
| 126 |
} |
| 127 |
} |