@@ -19,37 +19,33 @@ final class Language
1919 /**
2020 * The constructor.
2121 *
22- * @param string|string[] $langOrTrans the language string or translations array
22+ * @param string|string[] $target the language string or translations dict
2323 */
24- public function __construct($langOrTrans = 'eng')
24+ public function __construct($target = 'eng')
2525 {
26- $this->setLanguageOrTranslations($langOrTrans );
26+ $this->setLanguageOrTranslations($target );
2727 }
2828
2929 /**
30- * Set the language name .
30+ * Set up this class .
3131 *
32- * @param string|string[] $langOrTrans the language string or translations array
32+ * @param string|string[] $target the language string or translations array
3333 *
3434 * @throws \InvalidArgumentException
3535 *
3636 * @return self
3737 */
38- public function setLanguageOrTranslations($langOrTrans ): self
38+ public function setLanguageOrTranslations($target ): self
3939 {
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[]');
4446 }
4547
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;
5349 }
5450
5551 /**
@@ -83,7 +79,7 @@ public function getTranslations(): array
8379 *
8480 * @return string[]
8581 */
86- public function getTranslationsByLanguage(string $language): array
82+ public static function getTranslationsByLanguage(string $language): array
8783 {
8884 $filePath = __DIR__ . "/../languages/{$language}.json";
8985 $file = new \SplFileObject($filePath, 'r');
@@ -93,7 +89,11 @@ public function getTranslationsByLanguage(string $language): array
9389 $decoded = \json_decode($fileContent, true);
9490
9591 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+ ));
9797 }
9898
9999 return (array) $decoded;
@@ -112,31 +112,32 @@ public function translate(string $text): string
112112 }
113113
114114 /**
115- * Set the language name.
115+ * Set up this class by language name.
116116 *
117117 * @param string $language the language name
118118 *
119119 * @return self
120120 */
121- private function setLanguage (string $language): self
121+ private function setUpWithLanguage (string $language): self
122122 {
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+ ) ;
127127 }
128128
129129 /**
130- * Set the translations.
130+ * Set up this class by translations.
131131 *
132- * @param string[] $translations the language translations array
132+ * @param string[] $translations the translations dict
133+ * @param string $language the language name
133134 *
134135 * @return self
135136 */
136- private function setTranslations (array $translations): self
137+ private function setUpWithTranslations (array $translations, string $language = '_custom_' ): self
137138 {
138- $this->language = '_custom_' ;
139- $this->translations = $translations;
139+ $this->language = $language ;
140+ $this->translations = \array_map('strval', $translations) ;
140141
141142 return $this;
142143 }
0 commit comments