@@ -120,39 +120,39 @@ protected function init()
120120 * {@inheritdoc}
121121 * @see self::translate()
122122 */
123- public function t ($ key , array $ args = [], $ default = '' )
123+ public function t ($ key , array $ args = [], $ lang = '' )
124124 {
125- return $ this ->translate ($ key , $ args , $ default );
125+ return $ this ->translate ($ key , $ args , $ lang );
126126 }
127127
128128 /**
129129 * {@inheritdoc}
130130 * @see self::translate()
131131 */
132- public function tl ($ key , array $ args = [], $ default = null )
132+ public function tl ($ key , array $ args = [], $ lang = null )
133133 {
134- return $ this ->translate ($ key , $ args , $ default );
134+ return $ this ->translate ($ key , $ args , $ lang );
135135 }
136136
137137 /**
138138 * {@inheritdoc}
139139 * @see self::translate()
140140 */
141- public function trans ($ key , array $ args = [], $ default = null )
141+ public function trans ($ key , array $ args = [], $ lang = null )
142142 {
143- return $ this ->translate ($ key , $ args , $ default );
143+ return $ this ->translate ($ key , $ args , $ lang );
144144 }
145145
146146 /**
147147 * how to use language translate ? please see '/doc/language.md'
148148 * @param string|bool $key
149149 * @param array $args
150- * @param string $default
150+ * @param string $lang
151151 * @return string|array
152152 * @throws \Inhere\Exceptions\NotFoundException
153153 * @throws \InvalidArgumentException
154154 */
155- public function translate ($ key , array $ args = [], $ default = null )
155+ public function translate ($ key , array $ args = [], $ lang = null )
156156 {
157157 if (!is_string ($ key )) {
158158 throw new \InvalidArgumentException ('The translate key must be a string. ' );
@@ -162,14 +162,16 @@ public function translate($key, array $args = [], $default = null)
162162 throw new \InvalidArgumentException ('Cannot translate the empty key ' );
163163 }
164164
165+ list ($ lang , $ key ) = $ this ->parseKey ($ key , $ lang );
166+
165167 // translate form current language. if not found, translate form fallback language.
166168 if (($ value = $ this ->findTranslationText ($ key )) === null ) {
167- $ value = $ this ->transByFallbackLang ($ key, $ default );
169+ $ value = $ this ->transByFallbackLang ($ key );
168170 }
169171
170172 // no translate text
171173 if ($ value === '' || $ value === null ) {
172- return ucfirst (Str::toSnakeCase (str_replace (['- ' , '_ ' ], ' ' , $ key ), ' ' ));
174+ return ucfirst (Str::toSnake (str_replace (['- ' , '_ ' ], ' ' , $ key ), ' ' ));
173175 }
174176
175177 // $args is not empty
@@ -238,13 +240,12 @@ protected function findTranslationText($key)
238240
239241 /**
240242 * @param string $key
241- * @param string $default
242243 * @return mixed
243244 */
244- protected function transByFallbackLang ($ key, $ default = null )
245+ protected function transByFallbackLang ($ key )
245246 {
246247 if ($ this ->lang === $ this ->fallbackLang || !$ this ->fallbackLang ) {
247- return $ default ;
248+ return null ;
248249 }
249250
250251 // init fallbackData
@@ -278,17 +279,40 @@ protected function transByFallbackLang($key, $default = null)
278279 $ this ->loadedFiles [] = $ file ;
279280 $ this ->fallbackData ->set ($ fileKey , Collection::read ($ file , $ this ->format ));
280281
281- return $ this ->fallbackData ->get ($ key, $ default );
282+ return $ this ->fallbackData ->get ($ key );
282283 }
283284 }
284285
285- return $ default ;
286+ return null ;
286287 }
287288
289+
288290 /*********************************************************************************
289- * helper method
291+ * helper
290292 *********************************************************************************/
291293
294+ /**
295+ * @param string $key
296+ * @param string $lang
297+ * @return array
298+ */
299+ private function parseKey ($ key , $ lang = null )
300+ {
301+ if ($ lang ) {
302+ return [$ lang , $ key ];
303+ }
304+
305+ if (strpos ($ key , $ this ->separator )) {
306+ $ info = explode ($ this ->separator , $ key , 2 );
307+
308+ if ($ this ->isLang ($ info [0 ])) {
309+ return $ info ;
310+ }
311+ }
312+
313+ return [$ this ->lang , $ key ];
314+ }
315+
292316 /**
293317 * @param $filename
294318 * @param string $lang
@@ -376,6 +400,19 @@ public function hasLangFileData($fileKey)
376400 * getter/setter
377401 *********************************************************************************/
378402
403+ /**
404+ * @param string $lang
405+ * @return bool
406+ */
407+ public function hasLang ($ lang )
408+ {
409+ return $ this ->isLang ($ lang );
410+ }
411+ public function isLang ($ lang )
412+ {
413+ return $ lang && in_array ($ lang , $ this ->langs ,true );
414+ }
415+
379416 /**
380417 * Allow quick access default file translate by `$lang->key`,
381418 * is equals to `$lang->tl('key')`.
0 commit comments