Passed
Push — master ( f90742...feb582 )
by Jean-Christophe
02:24
created

TranslatorManager::setLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Ubiquity\translation;
3
4
use Ubiquity\translation\loader\ArrayLoader;
5
use Ubiquity\log\Logger;
6
7
class TranslatorManager {
8
	protected static $locale;
9
	protected static $loader;
10
	protected static $catalogues;
11
	protected static $fallbackLocale;
12
	
13
	public static function start($locale="en_EN",$fallbackLocale=null,$rootDir=null){
14
		self::$locale=$locale;
15
		self::$fallbackLocale=$fallbackLocale;
16
		self::setRootDir($rootDir);
17
	}
18
	
19
	public static function setLocale($locale){
20
		self::assertValidLocale($locale);
0 ignored issues
show
Bug Best Practice introduced by
The method Ubiquity\translation\Tra...er::assertValidLocale() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
		self::/** @scrutinizer ignore-call */ 
21
        assertValidLocale($locale);
Loading history...
21
		self::$locale = $locale;
22
	}
23
	
24
	public static function setRootDir($rootDir=null){
25
		if(!isset($rootDir)){
26
			$rootDir=\ROOT . \DS . "translations";
27
		}
28
		self::$loader=new ArrayLoader($rootDir);
29
	}
30
	
31
	public static function getLocale(){
32
		return self::$locale;
33
	}
34
	
35
	public static function trans($id, array $parameters = array(), $domain = null, $locale = null){
36
		return self::transCallable(function($catalog,$parameters){return self::replaceParams($catalog,$parameters);}, $id,$parameters,$domain,$locale);
37
	}
38
	
39
	protected static function transCallable($callback,$id, array $parameters = array(), $domain = null, $locale = null){
40
		if (null === $domain) {
41
			$domain = 'messages';
42
		}
43
		$id = (string) $id;
44
		$catalogue = self::getCatalogue($locale);
45
		if($catalogue===false){
46
			if(isset(self::$fallbackLocale) && $locale!==self::$fallbackLocale){
47
				self::setLocale(self::$fallbackLocale);
48
				Logger::warn("Translation", "Locale ".$locale." not found, set active locale to ".self::$locale);
49
				return self::trans($id,$parameters,$domain,self::$locale);
50
			}else{
51
				Logger::error("Translation", "Locale not found, no valid fallbackLocale specified");
52
				return $id;
53
			}
54
		}
55
		$transId=self::getTransId($id, $domain);
56
		if(isset($catalogue[$transId])){
57
			return $callback($catalogue[$transId],$parameters);
58
		}elseif(self::$fallbackLocale!==null && $locale!==self::$fallbackLocale){
59
			Logger::warn("Translation", "Translation not found for ".$id.". Switch to fallbackLocale ".self::$fallbackLocale);
60
			return self::trans($id,$parameters,$domain,self::$fallbackLocale);
61
		}else{
62
			Logger::warn("Translation", "Translation not found for ".$id.". in locales.");
63
			return $id;
64
		}
65
	}
66
	
67
	protected static function replaceParams($trans,array $parameters=array()){
68
		foreach ($parameters as $k=>$v){
69
			$trans=str_replace('%'.$k.'%', $v, $trans);
70
		}
71
		return $trans;
72
	}
73
	
74
	protected static function getTransId($id,$domain){
75
		return $domain.".".$id;
76
	}
77
	
78
	public static function getCatalogue(&$locale = null){
79
		if (null === $locale) {
80
			$locale = self::getLocale();
81
		} else {
82
			self::assertValidLocale($locale);
0 ignored issues
show
Bug Best Practice introduced by
The method Ubiquity\translation\Tra...er::assertValidLocale() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
			self::/** @scrutinizer ignore-call */ 
83
         assertValidLocale($locale);
Loading history...
83
		}
84
		if (!isset(self::$catalogues[$locale])) {
85
			self::loadCatalogue($locale);
86
		}
87
		return self::$catalogues[$locale];
88
	}
89
	
90
	public static function loadCatalogue($locale = null){
91
		self::$catalogues[$locale]=self::$loader->load($locale);
92
	}
93
	
94
	protected function assertValidLocale($locale){
95
		if (1 !== preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
96
			throw new \InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
97
		}
98
	}
99
	/**
100
	 * @return mixed
101
	 */
102
	public static function getFallbackLocale() {
103
		return self::$fallbackLocale;
104
	}
105
106
	/**
107
	 * @param mixed $fallbackLocale
108
	 */
109
	public static function setFallbackLocale($fallbackLocale) {
110
		self::$fallbackLocale = $fallbackLocale;
111
	}
112
	
113
	public static function clearCache(){
114
		apcu_clear_cache();
115
	}
116
}
117