Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
TranslatorManager.php
Go to the documentation of this file.
1<?php
2
4
7use Ubiquity\translation\loader\ArrayLoader;
10
21 protected static $locale;
22 protected static $loader;
23 protected static $catalogues;
24 protected static $fallbackLocale;
25
35 protected static function transCallable($callback, $id, array $parameters = array (), $domain = null, $locale = null) {
36 if (null === $domain) {
37 $domain = 'messages';
38 }
39 $id = ( string ) $id;
40 $catalogue = self::getCatalogue ( $locale );
41 if ($catalogue === false) {
42 if (isset ( self::$fallbackLocale ) && $locale !== self::$fallbackLocale) {
43 self::setLocale ( self::$fallbackLocale );
44 Logger::warn ( 'Translation', 'Locale ' . $locale . ' not found, set active locale to ' . self::$locale );
45 return self::trans ( $id, $parameters, $domain, self::$locale );
46 } else {
47 Logger::error ( 'Translation', 'Locale not found, no valid fallbackLocale specified' );
48 return $id;
49 }
50 }
51 $transId = self::getTransId ( $id, $domain );
52 if (isset ( $catalogue [$transId] )) {
53 return $callback ( $catalogue [$transId], $parameters );
54 } elseif (self::$fallbackLocale !== null && $locale !== self::$fallbackLocale) {
55 Logger::warn ( 'Translation', 'Translation not found for ' . $id . '. Switch to fallbackLocale ' . self::$fallbackLocale );
56 return self::trans ( $id, $parameters, $domain, self::$fallbackLocale );
57 } else {
58 Logger::warn ( 'Translation', 'Translation not found for ' . $id . '. in locales.' );
59 return $id;
60 }
61 }
62
71 protected static function doChoice($message, array $choice, array $parameters = [ ]) {
72 $message = ( string ) $message;
73
74 $number = ( float ) current ( $choice );
75 $parameters = $parameters + $choice;
76 $parts = [ ];
77 if (preg_match ( '/^\|++$/', $message )) {
78 $parts = explode ( '|', $message );
79 } elseif (preg_match_all ( '/(?:\|\||[^\|])++/', $message, $matches )) {
80 $parts = $matches [0];
81 }
82 $intervalRegexp = <<<'EOF'
83 /^(?P<interval>
84 ({\s*
85 (\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)
86 \s*})
87 |
88 (?P<left_delimiter>[\[\]])
89 \s*
90 (?P<left>-Inf|\-?\d+(\.\d+)?)
91 \s*,\s*
92 (?P<right>\+?Inf|\-?\d+(\.\d+)?)
93 \s*
94 (?P<right_delimiter>[\[\]])
95 )\s*(?P<message>.*?)$/xs
96 EOF;
97 foreach ( $parts as $part ) {
98 $part = trim ( str_replace ( '||', '|', $part ) );
99 if (preg_match ( $intervalRegexp, $part, $matches )) {
100 if ($matches [2]) {
101 foreach ( explode ( ',', $matches [3] ) as $n ) {
102 if ($number == $n) {
103 return self::replaceParams ( $matches ['message'], $parameters );
104 }
105 }
106 } else {
107 $leftNumber = '-Inf' === $matches ['left'] ? - INF : ( float ) $matches ['left'];
108 $rightNumber = \is_numeric ( $matches ['right'] ) ? ( float ) $matches ['right'] : INF;
109 if (('[' === $matches ['left_delimiter'] ? $number >= $leftNumber : $number > $leftNumber) && (']' === $matches ['right_delimiter'] ? $number <= $rightNumber : $number < $rightNumber)) {
110 return self::replaceParams ( $matches ['message'], $parameters );
111 }
112 }
113 } else {
114 return self::replaceParams ( $message, $parameters );
115 }
116 }
117 }
118
119 protected static function replaceParams($trans, array $parameters = array ()) {
120 foreach ( $parameters as $k => $v ) {
121 $trans = str_replace ( '%' . $k . '%', $v, $trans );
122 }
123 return $trans;
124 }
125
126 protected static function getTransId($id, $domain) {
127 return $domain . '.' . $id;
128 }
129
130 protected static function assertValidLocale($locale) {
131 if (1 !== preg_match ( '/^[a-z0-9@_\\.\\-]*$/i', $locale )) {
132 throw new \InvalidArgumentException ( sprintf ( 'Invalid "%s" locale.', $locale ) );
133 }
134 }
135
136 public static function isValidLocale($locale) {
137 return (1 === preg_match ( '/^[a-z0-9@_\\.\\-]*$/i', $locale ));
138 }
139
148 public static function start($locale = 'en_EN', $fallbackLocale = null, $rootDir = null) {
149 self::$locale = $locale;
150 self::$fallbackLocale = $fallbackLocale;
151 self::setRootDir ( $rootDir );
152 }
153
159 public static function setLocale($locale) {
160 self::assertValidLocale ( $locale );
161 self::$locale = $locale;
162 }
163
170 public static function setRootDir($rootDir = null) {
171 if (! isset ( $rootDir )) {
172 $rootDir = \ROOT . \DS . 'translations';
173 }
174 self::$loader = new ArrayLoader ( $rootDir );
175 }
176
182 public static function getLocale() {
183 return self::$locale;
184 }
185
195 public static function trans($id, array $parameters = array (), $domain = null, $locale = null) {
196 return self::transCallable ( function ($trans, $parameters) {
197 return self::replaceParams ( $trans, $parameters );
198 }, $id, $parameters, $domain, $locale );
199 }
200
211 public static function transChoice($id, array $choice, array $parameters = array (), $domain = null, $locale = null) {
212 return self::transCallable ( function ($message, $parameters) use ($choice) {
213 return self::doChoice ( $message, $choice, $parameters );
214 }, $id, $parameters, $domain, $locale );
215 }
216
223 public static function getCatalogue(&$locale = null) {
224 if (null === $locale) {
225 $locale = self::getLocale ();
226 } else {
227 self::assertValidLocale ( $locale );
228 }
229 if (! isset ( self::$catalogues [$locale] )) {
230 self::loadCatalogue ( $locale );
231 }
232 return self::$catalogues [$locale] ?? false;
233 }
234
240 public static function loadCatalogue($locale = null) {
241 self::$catalogues [$locale] = self::$loader->load ( $locale );
242 }
243
249 public static function getFallbackLocale() {
251 }
252
258 public static function setFallbackLocale($fallbackLocale) {
259 self::$fallbackLocale = $fallbackLocale;
260 }
261
265 public static function clearCache() {
266 self::$loader->clearCache ( '*' );
267 }
268
272 public static function clearLocaleCache($locale) {
273 self::$loader->clearCache ( $locale );
274 }
275
281 public static function getLocales() {
282 $locales = [ ];
283 $dirs = \glob ( self::getRootDir () . \DS . '*', GLOB_ONLYDIR );
284 foreach ( $dirs as $dir ) {
285 $locales [] = \basename ( $dir );
286 }
287 return $locales;
288 }
289
296 public static function getDomains($locale) {
297 $catalog = new MessagesCatalog ( $locale, self::$loader );
298 return $catalog->getDomains ();
299 }
300
306 public static function getRootDir() {
307 return self::$loader->getRootDir ();
308 }
309
315 public static function getLoader() {
316 return self::$loader;
317 }
318
324 public static function getCatalogues() {
325 return self::$catalogues;
326 }
327
334 public static function initialize($rootDir = null) {
335 $locale = URequest::getDefaultLanguage ();
336 self::createLocale ( self::fixLocale ( $locale ), $rootDir );
337 return self::getLocales ();
338 }
339
340 public static function fixLocale($language) {
341 return \str_replace ( [ '-','.' ], '_', $language );
342 }
343
350 public static function createLocale($locale, $rootDir = null) {
351 self::setRootDir ( $rootDir );
352 UFileSystem::safeMkdir ( self::getRootDir () . \DS . $locale );
353 }
354
365 public static function createDomain($locale, $domain, $defaultValues = null) {
366 if (isset ( self::$loader )) {
367 $domains = self::getDomains ( $locale );
368 if (\array_search ( $domain, $domains ) === false) {
369 $dom = new MessagesDomain ( $locale, self::$loader, $domain );
370 if (\is_array ( $defaultValues )) {
371 $dom->setMessages ( $defaultValues );
372 }
373 $dom->store ();
374 return $dom;
375 }
376 return false;
377 }
378 throw new CacheException ( 'TranslatorManager is not started!' );
379 }
380
388 public static function cacheExist($locale, $domain = '*') {
389 return self::$loader->cacheExists ( $locale, $domain );
390 }
391}
Abstract class for logging Ubiquity\log$Logger This class is part of Ubiquity.
Definition Logger.php:14
static transCallable($callback, $id, array $parameters=array(), $domain=null, $locale=null)
static doChoice($message, array $choice, array $parameters=[])
Inspired by \Symfony\Contracts\Translation\TranslatorTrait$trans.
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
Definition URequest.php:18