Passed
Push — master ( be71ba...d26f6c )
by Jean-Christophe
03:55
created

TranslatorManager::getLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Ubiquity\translation;
3
4
use Ubiquity\translation\loader\ArrayLoader;
5
use Ubiquity\log\Logger;
6
7
/**
8
 * Manages translations
9
 * @author jcheron <[email protected]>
10
 * @version 1.0.1
11
 */
12
class TranslatorManager {
13
	protected static $locale;
14
	protected static $loader;
15
	protected static $catalogues;
16
	protected static $fallbackLocale;
17
	
18 1
	protected static function transCallable($callback,$id, array $parameters = array(), $domain = null, $locale = null){
19 1
		if (null === $domain) {
20
			$domain = 'messages';
21
		}
22 1
		$id = (string) $id;
23 1
		$catalogue = self::getCatalogue($locale);
24 1
		if($catalogue===false){
25 1
			if(isset(self::$fallbackLocale) && $locale!==self::$fallbackLocale){
26 1
				self::setLocale(self::$fallbackLocale);
27 1
				Logger::warn("Translation", "Locale ".$locale." not found, set active locale to ".self::$locale);
28 1
				return self::trans($id,$parameters,$domain,self::$locale);
29
			}else{
30 1
				Logger::error("Translation", "Locale not found, no valid fallbackLocale specified");
31 1
				return $id;
32
			}
33
		}
34
		$transId=self::getTransId($id, $domain);
35
		if(isset($catalogue[$transId])){
36
			return $callback($catalogue[$transId],$parameters);
37
		}elseif(self::$fallbackLocale!==null && $locale!==self::$fallbackLocale){
38
			Logger::warn("Translation", "Translation not found for ".$id.". Switch to fallbackLocale ".self::$fallbackLocale);
39
			return self::trans($id,$parameters,$domain,self::$fallbackLocale);
40
		}else{
41
			Logger::warn("Translation", "Translation not found for ".$id.". in locales.");
42
			return $id;
43
		}
44
	}
45
	
46
	protected static function replaceParams($trans,array $parameters=array()){
47
		foreach ($parameters as $k=>$v){
48
			$trans=str_replace('%'.$k.'%', $v, $trans);
49
		}
50
		return $trans;
51
	}
52
	
53
	protected static function getTransId($id,$domain){
54
		return $domain.".".$id;
55
	}
56
	
57 1
	protected static function assertValidLocale($locale){
58 1
		if (1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
59
			throw new \InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
60
		}
61 1
	}
62
	
63 1
	public static function start($locale="en_EN",$fallbackLocale=null,$rootDir=null){
64 1
		self::$locale=$locale;
65 1
		self::$fallbackLocale=$fallbackLocale;
66 1
		self::setRootDir($rootDir);
67 1
	}
68
	
69 1
	public static function setLocale($locale){
70 1
		self::assertValidLocale($locale);
71 1
		self::$locale = $locale;
72 1
	}
73
	
74 1
	public static function setRootDir($rootDir=null){
75 1
		if(!isset($rootDir)){
76 1
			$rootDir=\ROOT . \DS . "translations";
77
		}
78 1
		self::$loader=new ArrayLoader($rootDir);
79 1
	}
80
	
81 1
	public static function getLocale(){
82 1
		return self::$locale;
83
	}
84
	
85
	public static function trans($id, array $parameters = array(), $domain = null, $locale = null){
86
		return self::transCallable(function($catalog,$parameters){return self::replaceParams($catalog,$parameters);}, $id,$parameters,$domain,$locale);
87
	}
88
	
89 1
	public static function getCatalogue(&$locale = null){
90 1
		if (null === $locale) {
91 1
			$locale = self::getLocale();
92
		} else {
93 1
			self::assertValidLocale($locale);
94
		}
95 1
		if (!isset(self::$catalogues[$locale])) {
96 1
			self::loadCatalogue($locale);
97
		}
98 1
		return self::$catalogues[$locale];
99
	}
100
	
101 1
	public static function loadCatalogue($locale = null){
102 1
		self::$catalogues[$locale]=self::$loader->load($locale);
103 1
	}
104
	
105
	/**
106
	 * @return mixed
107
	 */
108
	public static function getFallbackLocale() {
109
		return self::$fallbackLocale;
110
	}
111
112
	/**
113
	 * @param mixed $fallbackLocale
114
	 */
115 1
	public static function setFallbackLocale($fallbackLocale) {
116 1
		self::$fallbackLocale = $fallbackLocale;
117 1
	}
118
	
119
	public static function clearCache(){
120
		self::$loader->clearCache();
121
	}
122
}
123