88final class Translation
99{
1010 /** @var array<string, string> */
11- private array $ storage = [] ;
11+ private array $ storage ;
1212
1313 /** @var array<string, string> */
1414 private array $ startupState = [];
@@ -19,7 +19,10 @@ final class Translation
1919 */
2020 public function __construct (?string $ data , ?string $ language = null )
2121 {
22- if ($ data !== null && str_starts_with ($ data , 'T:{ ' )) {
22+ if ($ data === null ) {
23+ return ;
24+ }
25+ if (str_starts_with ($ data , 'T:{ ' )) { // format T:{"locale":"haystack"}
2326 $ data = str_replace (["\r\n" , "\r" ], "\n" , $ data );
2427 $ normalize = str_replace ("\n" , '\n ' , $ data );
2528 $ json = (string ) preg_replace ('/^T:/ ' , '' , $ normalize );
@@ -48,19 +51,13 @@ public function __construct(?string $data, ?string $language = null)
4851
4952 $ this ->storage = $ storageData ;
5053 $ this ->startupState = $ storageData ;
51- } else {
54+ } else { // back compatibility for pure string
5255 if ($ language === null ) {
53- if (PHP_SAPI === 'cli ' ) {
54- $ language = LocalizationHelper::getLocalization ()->getDefaultLocale ();
55- } else {
56- $ language = LocalizationHelper::getLocale (true );
57- }
58- }
59- if ($ data !== null ) {
60- $ this ->storage [$ language ] = $ data ;
61- } elseif (isset ($ this ->storage [$ language ])) {
62- unset($ this ->storage [$ language ]);
56+ $ language = PHP_SAPI === 'cli '
57+ ? LocalizationHelper::getLocalization ()->getDefaultLocale ()
58+ : LocalizationHelper::getLocale (true );
6359 }
60+ $ this ->storage = [$ language => $ data ];
6461 }
6562 }
6663
@@ -76,12 +73,12 @@ public function __toString(): string
7673 */
7774 public function getTranslation (?string $ language = null , bool $ fallback = true ): ?string
7875 {
76+ if (isset ($ this ->storage ) === false ) {
77+ return null ;
78+ }
7979 if ($ language === null ) {
8080 $ language = LocalizationHelper::getLocale (true );
8181 }
82- if ($ this ->storage === []) {
83- throw new \LogicException ('Storage has not been set or is empty. ' );
84- }
8582 if (isset ($ this ->storage [$ language ]) === true ) {
8683 return $ this ->storage [$ language ];
8784 }
@@ -113,6 +110,9 @@ public function addTranslate(?string $haystack, ?string $language = null): bool
113110 }
114111 }
115112 if ($ haystack !== null ) {
113+ if (isset ($ this ->storage ) === false ) {
114+ $ this ->storage = [];
115+ }
116116 $ this ->storage [$ language ] = $ haystack ;
117117 }
118118
@@ -128,7 +128,7 @@ public function addTranslate(?string $haystack, ?string $language = null): bool
128128 public function getSerialize (): string
129129 {
130130 return 'T: ' . json_encode (
131- $ this ->storage ,
131+ $ this ->storage ?? [] ,
132132 JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
133133 | (\defined ('JSON_PRESERVE_ZERO_FRACTION ' ) ? JSON_PRESERVE_ZERO_FRACTION : 0 ),
134134 );
@@ -149,7 +149,17 @@ public function getStartupState(): array
149149 */
150150 public function getStorage (): array
151151 {
152- return $ this ->storage ;
152+ return $ this ->storage ?? [];
153+ }
154+
155+
156+ /**
157+ * It detects if storage has been set up and exists.
158+ * If it returns `false`, it is probably a corrupted translation string.
159+ */
160+ public function isStorage (): bool
161+ {
162+ return isset ($ this ->storage );
153163 }
154164
155165
0 commit comments