Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Translate.php « modules - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 63e99810ce0a764b96c9bfaeafb2d4eb50138592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
 * Piwik - Open source web analytics
 * 
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
 * @version $Id$
 * 
 * @package Piwik
 */

/**
 * @package Piwik
 */
class Piwik_Translate
{
	static private $instance = null;
	
	/**
	 * Returns singleton
	 *
	 * @return Piwik_Translate
	 */
	static public function getInstance()
	{
		if (self::$instance == null)
		{			
			$c = __CLASS__;
			self::$instance = new $c();
		}
		return self::$instance;
	}
	
	private function __construct()
	{
		$translations = array();
		
		$language = $this->getFallbackLanguageToLoad();
		require_once "lang/" . $language .".php";
		$this->addTranslationArray($translations);
		
		$language = $this->getLanguageToLoad();
		require_once "lang/" . $language .".php";
		$this->addTranslationArray($translations);
		
		setlocale(LC_ALL, $GLOBALS['Piwik_translations']['General_Locale']);
	}
	
	public function addTranslationArray($translation)
	{
		if(!isset($GLOBALS['Piwik_translations']))
		{
			$GLOBALS['Piwik_translations'] = array();
		}
		// we could check that no string overlap here
		$GLOBALS['Piwik_translations'] = array_merge($GLOBALS['Piwik_translations'], $translation);
	}
	
	/**
	 * @return string the language filename prefix, eg "en" for english
	 * @throws exception if the language set in the config file is not a valid filename
	 */
	public function getLanguageToLoad()
	{
		$language = Zend_Registry::get('config')->Language->current;
		
		if( Piwik_Common::isValidFilename($language))
		{
			return $language;
		}
		else
		{
			throw new Exception("The language selected ('$language') is not a valid language file ");
		}
	}
	
	protected function getFallbackLanguageToLoad()
	{
		return Zend_Registry::get('config')->Language->fallback;
	}
	
	/**
	 * Generate javascript translations array
	 * 
	 * @return string containing javascript code with translations array (including <script> tag)
	 *
	 */
	public function getJavascriptTranslations($moduleList)
	{
		if( !$moduleList )
		{
			return '';
		}
		
		$js = 'var translations = {';
					
		$moduleRegex = '#^(';
		foreach($moduleList as $module)
		{
			$moduleRegex .= $module.'|'; 
		}
		$moduleRegex = substr($moduleRegex, 0, -1);
		$moduleRegex .= ')_([^_]+)_js$#i';
		
		foreach($GLOBALS['Piwik_translations'] as $key => $value)
		{
			$matches = array();
			
			if( preg_match($moduleRegex,$key,$matches) ) {
				$varName = $matches[1].'_'.$matches[2];
				$varValue = $value;
				
				$js .= "".$varName.": '".str_replace("'","\\'",$varValue)."',";
			}
			
			$matches = null;
		}
		$js = substr($js,0,-1);
		$js .= '};';
		$js .= 'function _pk_translate(tvar, str) { '.
			'var s = str; if( typeof(translations[tvar]) != \'undefined\' ) s = translations[tvar];'.
			'return s;}';
		
		return $js;
	}
}

function Piwik_Translate($index)
{
	if(isset($GLOBALS['Piwik_translations'][$index]))
	{
		return $GLOBALS['Piwik_translations'][$index];
	}
	throw new Exception("Translation string '$index' not available.");
}


/**
 * Returns translated string or given message if translation is not found.
 * This function does not throw any exception. Use it to translate exceptions.
 *
 * @param string Translation string index
 * @return string
 */
function Piwik_TranslateException($message)
{
	try {
		return Piwik_Translate($message);		
	}
	catch(Exception $e) {
		return $message;
	}
}