1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HMLB\Date\Translation; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Translation\Loader\ArrayLoader; |
6
|
|
|
use Symfony\Component\Translation\Translator; |
7
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait LocalizationCapabilities. |
11
|
|
|
* |
12
|
|
|
* @author Hugues Maignol <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
trait DateLocalizationCapabilities |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* A translator to ... er ... translate stuff. |
18
|
|
|
* |
19
|
|
|
* @var TranslatorInterface |
20
|
|
|
*/ |
21
|
|
|
protected static $translator; |
22
|
|
|
|
23
|
|
|
/////////////////////////////////////////////////////////////////// |
24
|
|
|
/////////////////////// LOCALIZATION ////////////////////////////// |
25
|
|
|
/////////////////////////////////////////////////////////////////// |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Intialize the translator instance if necessary. |
29
|
|
|
* |
30
|
|
|
* @return TranslatorInterface |
31
|
|
|
*/ |
32
|
|
|
protected static function translator() |
33
|
|
|
{ |
34
|
|
|
if (static::$translator === null) { |
35
|
|
|
$translator = new Translator('en'); |
36
|
|
|
$translator->addLoader('array', new ArrayLoader()); |
37
|
|
|
static::$translator = $translator; |
38
|
|
|
static::setLocale('en'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return static::$translator; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the translator instance in use. |
46
|
|
|
* |
47
|
|
|
* @return TranslatorInterface |
48
|
|
|
*/ |
49
|
|
|
public static function getTranslator() |
50
|
|
|
{ |
51
|
|
|
return static::translator(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Set the translator instance to use. |
56
|
|
|
* |
57
|
|
|
* @param TranslatorInterface $translator |
58
|
|
|
*/ |
59
|
|
|
public static function setTranslator(TranslatorInterface $translator) |
60
|
|
|
{ |
61
|
|
|
static::$translator = $translator; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the current translator locale. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public static function getLocale() |
70
|
|
|
{ |
71
|
|
|
return static::translator()->getLocale(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the current translator locale. |
76
|
|
|
* |
77
|
|
|
* @param string $locale |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public static function setLocale($locale) |
82
|
|
|
{ |
83
|
|
|
static::translator()->setLocale($locale); |
84
|
|
|
|
85
|
|
|
return static::loadLocaleResource($locale); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $locale |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
protected static function loadLocaleResource($locale) |
94
|
|
|
{ |
95
|
|
|
$translator = static::translator(); |
96
|
|
|
if ($translator instanceof Translator) { |
97
|
|
|
foreach (static::getLocaleResourceFilepaths($locale) as $filepath) { |
98
|
|
|
if (is_readable($filepath)) { |
99
|
|
|
$translator->addResource('array', require $filepath, $locale); |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $locale |
113
|
|
|
* |
114
|
|
|
* @return string[] |
115
|
|
|
*/ |
116
|
|
|
protected static function getLocaleResourceFilepaths($locale) |
117
|
|
|
{ |
118
|
|
|
$localePartDelimiters = ['-', '_']; |
119
|
|
|
$multipart = false; |
120
|
|
|
foreach ($localePartDelimiters as $delimiter) { |
121
|
|
|
$locale = explode($delimiter, $locale); |
122
|
|
|
if (1 !== count($locale)) { |
123
|
|
|
$multipart = true; |
124
|
|
|
break; |
125
|
|
|
} |
126
|
|
|
$locale = $locale[0]; |
127
|
|
|
} |
128
|
|
|
if (!$multipart) { |
129
|
|
|
return [sprintf('%s/Lang/%s.php', __DIR__, $locale)]; |
130
|
|
|
} |
131
|
|
|
$paths = []; |
132
|
|
|
foreach ($localePartDelimiters as $delimiter) { |
133
|
|
|
$paths[] = sprintf('%s/Lang/%s%s%s.php', __DIR__, $locale[0], $delimiter, $locale[1]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $paths; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|