-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranslator.php
115 lines (96 loc) · 2.82 KB
/
Translator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Kiberzauras\Translator;
use Illuminate\Foundation\Application;
use Illuminate\Support\Str;
use Kiberzauras\Translator\Modules\Database;
class Translator {
/**
* @var Translator
*/
private static $_instance = null;
/**
* @var bool
*/
private $_database = true;
/**
* @var array
*/
private $_debug = false;
/**
* @var array
*/
public $translations = array();
/**
* @param string $string
* @param array|string $arguments
* @param string $domain
* @param string $locale
* @return string
* @author Rytis Grincevičius <[email protected]>
*/
public static function translate ($string = '', array $arguments = array(), $domain = 'default', $locale = '')
{
$locale = $locale ?: static::_instance()->getLocale();
if (static::_instance()->_database) {
$translation = Database::translate($string, $arguments, $domain, $locale);
} else {
$translation = 'file translations';
}
if ($translation === false)
$translation = (static::$_instance->_debug ? '* ' : '') . $string;
return Translator::applyArguments($translation, $arguments);
}
/**
* @return Translator
* @author Rytis Grincevičius <[email protected]>
*/
protected static function _instance ()
{
if (!static::$_instance) {
static::$_instance = new static;
static::$_instance->_debug = config('app.debug', false);
}
return static::$_instance;
}
/**
* @param string $domain
* @return array
* @author Rytis Grincevičius <[email protected]>
*/
public static function getTranslations ($domain = '')
{
$domain = $domain ? $domain : 'default';
$path = Path::translation_path($domain . '.php');
if (file_exists($path))
return include_once $path;
return [];
}
/**
* @return string
* @author Rytis Grincevičius <[email protected]>
*/
public static function getLocale()
{
return Application::getInstance()->getLocale();
}
/**
* @param $string
* @param array $arguments
* @return mixed
* @author Rytis Grincevičius <[email protected]>
*/
public static function applyArguments($string, array $arguments = array())
{
foreach ($arguments as $key => $value) {
$string = str_replace(
[':' . Str::upper($key), ':' . Str::ucfirst($key), ':'.$key],
[Str::upper($value), Str::ucfirst($value), $value],
$string
);
}
return $string;
}
public static function applyPlurals($string = '', $number = 0, $split = '|')
{
}
}