aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/languages.php
blob: 16af2444607e31ca0034a5c7801f221f31825b49 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php

	/**
	 * Elgg language module
	 * Functions to manage language and translations.
	 * 
	 * @package Elgg
	 * @subpackage Core
	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
	 * @author Curverider Ltd
	 * @copyright Curverider Ltd 2008-2009
	 * @link http://elgg.org/
	 */

	/**
	 * Add a translation.
	 * 
	 * Translations are arrays in the Zend Translation array format, eg:
	 * 
	 *	$english = array('message1' => 'message1', 'message2' => 'message2');
	 *  $german = array('message1' => 'Nachricht1','message2' => 'Nachricht2');
	 *
	 * @param string $country_code Standard country code (eg 'en', 'nl', 'es')
	 * @param array $language_array Formatted array of strings
	 * @return true|false Depending on success
	 */

		function add_translation($country_code, $language_array) {
			
			global $CONFIG;
			if (!isset($CONFIG->translations))
				$CONFIG->translations = array();
			
			$country_code = strtolower($country_code);
			$country_code = trim($country_code);
			if (is_array($language_array) && sizeof($language_array) > 0 && $country_code != "") {
				
				if (!isset($CONFIG->translations[$country_code])) {
					$CONFIG->translations[$country_code] = $language_array;
				} else {
					$CONFIG->translations[$country_code] = array_merge($CONFIG->translations[$country_code],$language_array);
				}
				return true;
				
			}
			return false;
			
		}
		
	/**
	 * Detect the current language being used by the current site or logged in user.
	 *
	 */
	function get_current_language()
	{
		global $CONFIG;
		
		$user = get_loggedin_user();
		
		if ((isset($user)) && ($user->language))
			$language = $user->language;
	
		if ((empty($language)) && (isset($CONFIG->language)))
			$language = $CONFIG->language;
			
		if (empty($language))
			$language = 'en';
			
		return $language;
	}
		
	/**
	 * Gets the current language in use by the system or user
	 *
	 * @return string The language code (eg "en")
	 */
		function get_language() {
			
			global $CONFIG;
			static $lang;
			
			if (isset($lang)) return $lang;
			
			$user = get_loggedin_user();
			
			if ((empty($language)) && (isset($user)) && ($user->language))
				$language = $user->language;
	
			if ((empty($language)) && (isset($CONFIG->language)))
				$language = $CONFIG->language;
				
			if (!empty($lang)) {
				$lang = $language;
				return $lang;
			}
			
			return false;
			
		}
	
	/**
	 * Given a message shortcode, returns an appropriately translated full-text string 
	 *
	 * @param string $message_key The short message code
	 * @param string $language Optionally, the standard language code (defaults to the site default, then English)
	 * @return string Either the translated string, or the original English string, or an empty string
	 */
		function elgg_echo($message_key, $language = "") {
			
			global $CONFIG;
				
			$language = get_language();

			if (isset($CONFIG->translations[$language][$message_key])) {
				return $CONFIG->translations[$language][$message_key];
			} else if (isset($CONFIG->translations["en"][$message_key])) {
				return $CONFIG->translations["en"][$message_key];
			}
				
			return $message_key;
			
		}
		
	/**
	 * When given a full path, finds translation files and loads them
	 *
	 * @param string $path Full path
	 */
		function register_translations($path) {
			global $CONFIG;
			
			if (isset($CONFIG->debug) && $CONFIG->debug == true) error_log("Translations loaded from : $path");
			
			if ($handle = opendir($path)) {
				while ($language = readdir($handle)) {
					if (!in_array($language,array('.','..','.svn','CVS', '.DS_Store', 'Thumbs.db',)) && !is_dir($path . $language)) {
						@include($path . $language);
					}
				}
			}
			else
				error_log("Missing translation path $path");
		}
		
	/**
	 * Return an array of installed translations as an associative array "two letter code" => "native language name".
	 */
		function get_installed_translations()
		{
			global $CONFIG;
			
			$installed = array();
			
			foreach ($CONFIG->translations as $k => $v)
			{
				$installed[$k] = elgg_echo($k, $k);
				
				$completeness = get_language_completeness($k);
				if ((isadminloggedin()) && ($completeness<100) && ($k!='en'))
					$installed[$k] .= " (" . $completeness . "% " . elgg_echo('complete') . ")";
			}
			
			return $installed;
		}
		
	/**
	 * Return the level of completeness for a given language code (compared to english)
	 */
		function get_language_completeness($language)
		{
			global $CONFIG;
			
			$language = sanitise_string($language);
			
			$en = count($CONFIG->translations['en']);
			
			$missing = get_missing_language_keys($language);
			if ($missing) $missing = count($missing); else $missing = 0;
			
			//$lang = count($CONFIG->translations[$language]);
			$lang = $en - $missing;
			
			return round(($lang / $en) * 100, 2);
		}
		
	/**
	 * Return the translation keys missing from a given language, or those that are identical to the english version.
	 */
		function get_missing_language_keys($language)
		{
			global $CONFIG;
			
			$missing = array();
			
			foreach ($CONFIG->translations['en'] as $k => $v)
			{
				if ((!isset($CONFIG->translations[$language][$k])) 
				|| ($CONFIG->translations[$language][$k] == $CONFIG->translations['en'][$k])) 
					$missing[] = $k;
			}
			
			if (count($missing))
				return $missing;
				
			return false;
		}
		
		register_translations(dirname(dirname(dirname(__FILE__))) . "/languages/");

?>