Passed
Push — master ( 2b7b1b...185c2b )
by Jean-Christophe
06:02
created

TranslatorManager::getFallbackLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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