2
2
3
3
namespace Simples \Message ;
4
4
5
+ use Simples \Error \SimplesRunTimeError ;
5
6
use Simples \Helper \File ;
7
+ use Simples \Helper \Text ;
6
8
use Simples \Kernel \App ;
7
9
8
10
/**
9
- * @method static string validation($i18, array $parameters = [])
10
- * @method static mixed auth($i18, array $parameters = [])
11
+ * @method static string validation(string $i18, array $parameters = [])
12
+ * @method static string auth(string $i18, array $parameters = [])
11
13
*
12
14
* Class Lang
13
15
* @package Simples\Kernel
14
16
*/
15
17
abstract class Lang
16
18
{
17
19
/**
18
- * @param $default
20
+ * @param string $default
19
21
* @param string $fallback
20
22
*/
21
- public static function locale ($ default , $ fallback = '' )
23
+ public static function locale (string $ default , string $ fallback = '' )
22
24
{
23
25
App::options ('lang ' , ['default ' => $ default , 'fallback ' => $ fallback ]);
24
26
}
25
27
26
28
/**
27
- * @param $name
28
- * @param $arguments
29
+ * @param string $name
30
+ * @param string $arguments
29
31
* @return string
32
+ * @throws SimplesRunTimeError
30
33
*/
31
34
public static function __callStatic ($ name , $ arguments )
32
35
{
36
+ if (!isset ($ arguments [0 ])) {
37
+ throw new SimplesRunTimeError (
38
+ 'Call static scope requires the same parameters what ' .
39
+ '`static::replace(string $scope, string $path, array $parameters = [])` '
40
+ );
41
+ }
42
+ $ parameters = [];
33
43
if (isset ($ arguments [1 ])) {
34
- return self :: lang ( $ name , $ arguments [ 0 ], $ arguments [1 ]) ;
44
+ $ parameters = $ arguments [1 ];
35
45
}
36
- return self :: lang ($ name , $ arguments [0 ]);
46
+ return static :: replace ($ name , $ arguments [0 ], $ parameters );
37
47
}
38
48
39
-
40
49
/**
41
- * @param $scope
42
- * @param $path
43
- * @param array $parameters
44
- * @return string
50
+ * @param string $scope
51
+ * @param string $path
52
+ * @return mixed
45
53
*/
46
- public static function lang ( $ scope , $ path , array $ parameters = [] )
54
+ public static function get ( string $ scope , string $ path )
47
55
{
48
56
$ i18n = "Lang ' {$ scope }. {$ path }' not found " ;
49
57
$ languages = App::options ('lang ' );
@@ -52,26 +60,35 @@ public static function lang($scope, $path, array $parameters = [])
52
60
if ($ filename ) {
53
61
/** @noinspection PhpIncludeInspection */
54
62
$ phrases = include $ filename ;
55
-
56
63
$ i18n = search ($ phrases , $ path );
57
- if (gettype ($ i18n ) === TYPE_STRING ) {
58
- return self ::replace ($ i18n , $ parameters );
59
- }
60
64
}
61
65
return $ i18n ;
62
66
}
63
67
64
68
/**
65
- * @param $i18n
66
- * @param $parameters
67
- * @return mixed
69
+ * @param string $scope
70
+ * @param string $path
71
+ * @param array $parameters
72
+ * @return string
73
+ * @throws SimplesRunTimeError
68
74
*/
69
- public static function replace ($ i18n , $ parameters)
75
+ public static function replace (string $ scope , string $ path , array $ parameters = []): string
70
76
{
71
- foreach ($ parameters as $ key => $ value ) {
72
- $ i18n = str_replace ('{ ' . $ key . '} ' , parse ($ value ), $ i18n );
77
+ $ i18n = static ::get ($ scope , $ path );
78
+ if (gettype ($ i18n ) === TYPE_STRING ) {
79
+ return static ::replacement ($ i18n , $ parameters );
73
80
}
74
- return $ i18n ;
81
+ throw new SimplesRunTimeError ("Can't use ` {$ scope }` and ` {$ path }` to make replacement " , $ parameters );
82
+ }
83
+
84
+ /**
85
+ * @param string $i18n
86
+ * @param array $parameters
87
+ * @return string
88
+ */
89
+ public static function replacement (string $ i18n , array $ parameters ): string
90
+ {
91
+ return Text::replacement ($ i18n , $ parameters );
75
92
}
76
93
77
94
/**
0 commit comments