@@ -19,55 +19,52 @@ final class Language
19
19
/**
20
20
* The constructor.
21
21
*
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
23
23
*/
24
24
public function __construct ($ target = 'eng ' )
25
25
{
26
- $ this ->setLanguageOrTranslations ($ target );
26
+ $ this ->load ($ target );
27
27
}
28
28
29
29
/**
30
- * Set up this class .
30
+ * Gets the language .
31
31
*
32
- * @param string|string[] $target the language string or translations array
33
- *
34
- * @throws \InvalidArgumentException
32
+ * @return string the language
35
33
*/
36
- public function setLanguageOrTranslations ( $ target ): self
34
+ public function getLanguage ( ): string
37
35
{
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
+ }
49
38
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 ;
51
47
}
52
48
53
49
/**
54
- * Get the language.
50
+ * Loads the target language.
55
51
*
56
- * @return string the language
52
+ * @param array<int, string|string[]>|string|string[] $target the language ID or translations dict
57
53
*/
58
- public function getLanguage ( ): string
54
+ public function load ( $ target ): void
59
55
{
60
- return $ this ->language ;
56
+ $ this ->translations = $ this ->resolve ($ target );
57
+ $ this ->language = \is_string ($ target ) ? $ target : '_custom_ ' ;
61
58
}
62
59
63
60
/**
64
- * Get the translations .
61
+ * Translates the text .
65
62
*
66
- * @return array the translations
63
+ * @param string $text the text
67
64
*/
68
- public function getTranslations ( ): array
65
+ public function translate ( string $ text ): string
69
66
{
70
- return $ this ->translations ;
67
+ return $ this ->translations [ $ text ] ?? " ![ { $ text } ] " ;
71
68
}
72
69
73
70
/**
@@ -81,7 +78,7 @@ public function getTranslations(): array
81
78
*
82
79
* @return string[]
83
80
*/
84
- public static function getTranslationsByLanguage (string $ language ): array
81
+ private static function getTranslationsByLanguage (string $ language ): array
85
82
{
86
83
$ filePath = __DIR__ . "/../languages/ {$ language }.json " ;
87
84
$ file = new \SplFileObject ($ filePath , 'r ' );
@@ -97,39 +94,34 @@ public static function getTranslationsByLanguage(string $language): array
97
94
}
98
95
99
96
/**
100
- * Translation the text .
97
+ * Resolves the target language .
101
98
*
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
111
100
*
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
124
102
*
125
- * @param string[] $translations the translations dict
126
- * @param string $language the language name
103
+ * @return string[] the resolved translations
127
104
*/
128
- private function setUpWithTranslations ( array $ translations , string $ language = ' _custom_ ' ): self
105
+ private function resolve ( $ target ): array
129
106
{
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
+ }
132
124
133
- return $ this ;
125
+ throw new \ InvalidArgumentException ( ' $target is not in valid form ' ) ;
134
126
}
135
127
}
0 commit comments