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