Skip to content

Commit 3e67905

Browse files
committedMay 8, 2017
I18n enhancements
- fix some mistakes structures
1 parent cdff803 commit 3e67905

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed
 

‎src/Lang.php

+42-25
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,56 @@
22

33
namespace Simples\Message;
44

5+
use Simples\Error\SimplesRunTimeError;
56
use Simples\Helper\File;
7+
use Simples\Helper\Text;
68
use Simples\Kernel\App;
79

810
/**
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 = [])
1113
*
1214
* Class Lang
1315
* @package Simples\Kernel
1416
*/
1517
abstract class Lang
1618
{
1719
/**
18-
* @param $default
20+
* @param string $default
1921
* @param string $fallback
2022
*/
21-
public static function locale($default, $fallback = '')
23+
public static function locale(string $default, string $fallback = '')
2224
{
2325
App::options('lang', ['default' => $default, 'fallback' => $fallback]);
2426
}
2527

2628
/**
27-
* @param $name
28-
* @param $arguments
29+
* @param string $name
30+
* @param string $arguments
2931
* @return string
32+
* @throws SimplesRunTimeError
3033
*/
3134
public static function __callStatic($name, $arguments)
3235
{
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 = [];
3343
if (isset($arguments[1])) {
34-
return self::lang($name, $arguments[0], $arguments[1]);
44+
$parameters = $arguments[1];
3545
}
36-
return self::lang($name, $arguments[0]);
46+
return static::replace($name, $arguments[0], $parameters);
3747
}
3848

39-
4049
/**
41-
* @param $scope
42-
* @param $path
43-
* @param array $parameters
44-
* @return string
50+
* @param string $scope
51+
* @param string $path
52+
* @return mixed
4553
*/
46-
public static function lang($scope, $path, array $parameters = [])
54+
public static function get(string $scope, string $path)
4755
{
4856
$i18n = "Lang '{$scope}.{$path}' not found";
4957
$languages = App::options('lang');
@@ -52,26 +60,35 @@ public static function lang($scope, $path, array $parameters = [])
5260
if ($filename) {
5361
/** @noinspection PhpIncludeInspection */
5462
$phrases = include $filename;
55-
5663
$i18n = search($phrases, $path);
57-
if (gettype($i18n) === TYPE_STRING) {
58-
return self::replace($i18n, $parameters);
59-
}
6064
}
6165
return $i18n;
6266
}
6367

6468
/**
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
6874
*/
69-
public static function replace($i18n, $parameters)
75+
public static function replace(string $scope, string $path, array $parameters = []): string
7076
{
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);
7380
}
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);
7592
}
7693

7794
/**

0 commit comments

Comments
 (0)
Please sign in to comment.