Passed
Push — master ( 78bb6e...bd5589 )
by Jean-Christophe
01:57
created

Translator   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 26

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A assertValidLocale() 0 3 2
A setRootDir() 0 5 2
A trans() 0 2 1
A setLocale() 0 3 1
A clearCache() 0 2 1
A loadCatalogue() 0 2 1
A getTransId() 0 2 1
A getCatalogue() 0 10 3
A setFallbackLocale() 0 2 1
B transCallable() 0 25 8
A getLocale() 0 2 1
A replaceParams() 0 5 2
A getFallbackLocale() 0 2 1
1
<?php
2
namespace Ubiquity\translation;
3
4
use Ubiquity\translation\loader\ArrayLoader;
5
use Ubiquity\log\Logger;
6
7
class Translator {
8
	protected $locale;
9
	protected $loader;
10
	protected $catalogues;
11
	protected $fallbackLocale;
12
	
13
	public function __construct($locale="en_EN",$fallbackLocale=null,$rootDir=null){
14
		$this->locale=$locale;
15
		$this->fallbackLocale=$fallbackLocale;
16
		$this->setRootDir($rootDir);
17
	}
18
	
19
	public function setLocale($locale){
20
		$this->assertValidLocale($locale);
21
		$this->locale = $locale;
22
	}
23
	
24
	public function setRootDir($rootDir=null){
25
		if(!isset($rootDir)){
26
			$rootDir=ROOT . DS . "translations";
0 ignored issues
show
Bug introduced by
The constant Ubiquity\translation\ROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Ubiquity\translation\DS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
27
		}
28
		$this->loader=new ArrayLoader($rootDir);
29
	}
30
	
31
	public function getLocale(){
32
		return $this->locale;
33
	}
34
	
35
	public function trans($id, array $parameters = array(), $domain = null, $locale = null){
36
		return $this->transCallable(function($catalog,$parameters){return $this->replaceParams($catalog,$parameters);}, $id,$parameters,$domain,$locale);
37
	}
38
	
39
	protected 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 = $this->getCatalogue($locale);
45
		if($catalogue===false){
46
			if(isset($this->fallbackLocale) && $locale!==$this->fallbackLocale){
47
				$this->setLocale($this->fallbackLocale);
48
				Logger::warn("Translation", "Locale ".$locale." not found, set active locale to ".$this->locale);
49
				return $this->trans($id,$parameters,$domain,$this->locale);
50
			}else{
51
				Logger::error("Translation", "Locale not found, no valid fallbackLocale specified");
52
				return $id;
53
			}
54
		}
55
		$transId=$this->getTransId($id, $domain);
56
		if(isset($catalogue[$transId])){
57
			return $callback($catalogue[$transId],$parameters);
58
		}elseif($this->fallbackLocale!==null && $locale!==$this->fallbackLocale){
59
			Logger::warn("Translation", "Translation not found for ".$id.". Switch to fallbackLocale ".$this->fallbackLocale);
60
			return $this->trans($id,$parameters,$domain,$this->fallbackLocale);
61
		}else{
62
			Logger::warn("Translation", "Translation not found for ".$id.". in locales.");
63
			return $id;
64
		}
65
	}
66
	
67
	protected 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 function getTransId($id,$domain){
75
		return $domain.".".$id;
76
	}
77
	
78
	public function getCatalogue(&$locale = null){
79
		if (null === $locale) {
80
			$locale = $this->getLocale();
81
		} else {
82
			$this->assertValidLocale($locale);
83
		}
84
		if (!isset($this->catalogues[$locale])) {
85
			$this->loadCatalogue($locale);
86
		}
87
		return $this->catalogues[$locale];
88
	}
89
	
90
	public function loadCatalogue($locale = null){
91
		$this->catalogues[$locale]=$this->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 function getFallbackLocale() {
103
		return $this->fallbackLocale;
104
	}
105
106
	/**
107
	 * @param mixed $fallbackLocale
108
	 */
109
	public function setFallbackLocale($fallbackLocale) {
110
		$this->fallbackLocale = $fallbackLocale;
111
	}
112
	
113
	public function clearCache(){
114
		apc_clear_cache('user');
115
	}
116
}
117