@@ -19,55 +19,52 @@ final class Language
1919 /**
2020 * The constructor.
2121 *
22- * @param string|string[] $target the language string or translations dict
22+ * @param array<int, string|string[]>|string|string[] $target the language ID or translations dict
2323 */
2424 public function __construct ($ target = 'eng ' )
2525 {
26- $ this ->setLanguageOrTranslations ($ target );
26+ $ this ->load ($ target );
2727 }
2828
2929 /**
30- * Set up this class .
30+ * Gets the language .
3131 *
32- * @param string|string[] $target the language string or translations array
33- *
34- * @throws \InvalidArgumentException
32+ * @return string the language
3533 */
36- public function setLanguageOrTranslations ( $ target ): self
34+ public function getLanguage ( ): string
3735 {
38- if (\is_string ($ target )) {
39- $ this ->setUpWithLanguage ($ target );
40-
41- return $ this ;
42- }
43-
44- if (\is_array ($ target )) {
45- $ this ->setUpWithTranslations ($ target );
46-
47- return $ this ;
48- }
36+ return $ this ->language ;
37+ }
4938
50- throw new \InvalidArgumentException ('$target must be the type of string|string[] ' );
39+ /**
40+ * Gets the translations.
41+ *
42+ * @return array the translations
43+ */
44+ public function getTranslations (): array
45+ {
46+ return $ this ->translations ;
5147 }
5248
5349 /**
54- * Get the language.
50+ * Loads the target language.
5551 *
56- * @return string the language
52+ * @param array<int, string|string[]>|string|string[] $target the language ID or translations dict
5753 */
58- public function getLanguage ( ): string
54+ public function load ( $ target ): void
5955 {
60- return $ this ->language ;
56+ $ this ->translations = $ this ->resolve ($ target );
57+ $ this ->language = \is_string ($ target ) ? $ target : '_custom_ ' ;
6158 }
6259
6360 /**
64- * Get the translations .
61+ * Translates the text .
6562 *
66- * @return array the translations
63+ * @param string $text the text
6764 */
68- public function getTranslations ( ): array
65+ public function translate ( string $ text ): string
6966 {
70- return $ this ->translations ;
67+ return $ this ->translations [ $ text ] ?? " ![ { $ text } ] " ;
7168 }
7269
7370 /**
@@ -81,7 +78,7 @@ public function getTranslations(): array
8178 *
8279 * @return string[]
8380 */
84- public static function getTranslationsByLanguage (string $ language ): array
81+ private static function getTranslationsByLanguage (string $ language ): array
8582 {
8683 $ filePath = __DIR__ . "/../languages/ {$ language }.json " ;
8784 $ file = new \SplFileObject ($ filePath , 'r ' );
@@ -97,39 +94,34 @@ public static function getTranslationsByLanguage(string $language): array
9794 }
9895
9996 /**
100- * Translation the text .
97+ * Resolves the target language .
10198 *
102- * @param string $text the text
103- */
104- public function translate (string $ text ): string
105- {
106- return $ this ->translations [$ text ] ?? "![ {$ text }] " ;
107- }
108-
109- /**
110- * Set up this class by language name.
99+ * @param array<int,string|string[]>|string|string[] $target the language ID or translations array
111100 *
112- * @param string $language the language name
113- */
114- private function setUpWithLanguage (string $ language ): self
115- {
116- return $ this ->setUpWithTranslations (
117- self ::getTranslationsByLanguage ($ language ),
118- $ language ,
119- );
120- }
121-
122- /**
123- * Set up this class by translations.
101+ * @throws \InvalidArgumentException
124102 *
125- * @param string[] $translations the translations dict
126- * @param string $language the language name
103+ * @return string[] the resolved translations
127104 */
128- private function setUpWithTranslations ( array $ translations , string $ language = ' _custom_ ' ): self
105+ private function resolve ( $ target ): array
129106 {
130- $ this ->language = $ language ;
131- $ this ->translations = array_map ('strval ' , $ translations );
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 ($ carry , $ translation ) => array_merge ($ carry , $ this ->resolve ($ translation )),
121+ [],
122+ );
123+ }
132124
133- return $ this ;
125+ throw new \ InvalidArgumentException ( ' $target is not in valid form ' ) ;
134126 }
135127}
0 commit comments