@@ -19,37 +19,33 @@ final class Language
19
19
/**
20
20
* The constructor.
21
21
*
22
- * @param string|string[] $langOrTrans the language string or translations array
22
+ * @param string|string[] $target the language string or translations dict
23
23
*/
24
- public function __construct ($ langOrTrans = 'eng ' )
24
+ public function __construct ($ target = 'eng ' )
25
25
{
26
- $ this ->setLanguageOrTranslations ($ langOrTrans );
26
+ $ this ->setLanguageOrTranslations ($ target );
27
27
}
28
28
29
29
/**
30
- * Set the language name .
30
+ * Set up this class .
31
31
*
32
- * @param string|string[] $langOrTrans the language string or translations array
32
+ * @param string|string[] $target the language string or translations array
33
33
*
34
34
* @throws \InvalidArgumentException
35
35
*
36
36
* @return self
37
37
*/
38
- public function setLanguageOrTranslations ($ langOrTrans ): self
38
+ public function setLanguageOrTranslations ($ target ): self
39
39
{
40
- if (\is_string ($ langOrTrans )) {
41
- $ this ->setLanguage ($ langOrTrans );
42
-
43
- return $ this ;
40
+ if (\is_string ($ target )) {
41
+ $ this ->setUpWithLanguage ($ target );
42
+ } elseif (\is_array ($ target )) {
43
+ $ this ->setUpWithTranslations ($ target );
44
+ } else {
45
+ throw new \InvalidArgumentException ('$target must be the type of string|string[] ' );
44
46
}
45
47
46
- if (\is_array ($ langOrTrans )) {
47
- $ this ->setTranslations ($ langOrTrans );
48
-
49
- return $ this ;
50
- }
51
-
52
- throw new \InvalidArgumentException ('$langOrTrans must be either string or array ' );
48
+ return $ this ;
53
49
}
54
50
55
51
/**
@@ -83,7 +79,7 @@ public function getTranslations(): array
83
79
*
84
80
* @return string[]
85
81
*/
86
- public function getTranslationsByLanguage (string $ language ): array
82
+ public static function getTranslationsByLanguage (string $ language ): array
87
83
{
88
84
$ filePath = __DIR__ . "/../languages/ {$ language }.json " ;
89
85
$ file = new \SplFileObject ($ filePath , 'r ' );
@@ -93,7 +89,11 @@ public function getTranslationsByLanguage(string $language): array
93
89
$ decoded = \json_decode ($ fileContent , true );
94
90
95
91
if (\json_last_error () !== \JSON_ERROR_NONE ) {
96
- throw new \Exception ("Fail to decode JSON file: {$ filePath }" );
92
+ throw new \Exception (\sprintf (
93
+ 'Fail to decode JSON file (code %d): %s ' ,
94
+ \json_last_error (),
95
+ \realpath ($ filePath )
96
+ ));
97
97
}
98
98
99
99
return (array ) $ decoded ;
@@ -112,31 +112,32 @@ public function translate(string $text): string
112
112
}
113
113
114
114
/**
115
- * Set the language name.
115
+ * Set up this class by language name.
116
116
*
117
117
* @param string $language the language name
118
118
*
119
119
* @return self
120
120
*/
121
- private function setLanguage (string $ language ): self
121
+ private function setUpWithLanguage (string $ language ): self
122
122
{
123
- $ this ->language = $ language ;
124
- $ this -> translations = $ this -> getTranslationsByLanguage ($ language );
125
-
126
- return $ this ;
123
+ return $ this ->setUpWithTranslations (
124
+ self :: getTranslationsByLanguage ($ language ),
125
+ $ language
126
+ ) ;
127
127
}
128
128
129
129
/**
130
- * Set the translations.
130
+ * Set up this class by translations.
131
131
*
132
- * @param string[] $translations the language translations array
132
+ * @param string[] $translations the translations dict
133
+ * @param string $language the language name
133
134
*
134
135
* @return self
135
136
*/
136
- private function setTranslations (array $ translations ): self
137
+ private function setUpWithTranslations (array $ translations, string $ language = ' _custom_ ' ): self
137
138
{
138
- $ this ->language = ' _custom_ ' ;
139
- $ this ->translations = $ translations ;
139
+ $ this ->language = $ language ;
140
+ $ this ->translations = \array_map ( ' strval ' , $ translations) ;
140
141
141
142
return $ this ;
142
143
}
0 commit comments