Elgg  Version 1.9
languages.php
Go to the documentation of this file.
1 <?php
21 function elgg_echo($message_key, $args = array(), $language = "") {
23 
24  static $CURRENT_LANGUAGE;
25 
26  // old param order is deprecated
27  if (!is_array($args)) {
29  'As of Elgg 1.8, the 2nd arg to elgg_echo() is an array of string replacements and the 3rd arg is the language.',
30  1.8
31  );
32 
33  $language = $args;
34  $args = array();
35  }
36 
37  if (!$CURRENT_LANGUAGE) {
38  $CURRENT_LANGUAGE = get_language();
39  }
40 
41  if (!$language) {
42  $language = $CURRENT_LANGUAGE;
43  }
44 
45  if (!isset($CONFIG->translations[$language])) {
46  // The language being requested is not the same as the language of the
47  // logged in user, so we will have to load it separately. (Most likely
48  // we're sending a notification and the recipient is using a different
49  // language than the logged in user.)
51  }
52 
53  if (isset($CONFIG->translations[$language][$message_key])) {
54  $string = $CONFIG->translations[$language][$message_key];
55  } else if (isset($CONFIG->translations["en"][$message_key])) {
56  $string = $CONFIG->translations["en"][$message_key];
57  elgg_log(sprintf('Missing %s translation for "%s" language key', $language, $message_key), 'NOTICE');
58  } else {
59  $string = $message_key;
60  elgg_log(sprintf('Missing English translation for "%s" language key', $message_key), 'NOTICE');
61  }
62 
63  // only pass through if we have arguments to allow backward compatibility
64  // with manual sprintf() calls.
65  if ($args) {
66  $string = vsprintf($string, $args);
67  }
68 
69  return $string;
70 }
71 
85 function add_translation($country_code, $language_array) {
87  if (!isset($CONFIG->translations)) {
88  $CONFIG->translations = array();
89  }
90 
91  $country_code = strtolower($country_code);
92  $country_code = trim($country_code);
93  if (is_array($language_array) && sizeof($language_array) > 0 && $country_code != "") {
94  if (!isset($CONFIG->translations[$country_code])) {
95  $CONFIG->translations[$country_code] = $language_array;
96  } else {
97  $CONFIG->translations[$country_code] = $language_array + $CONFIG->translations[$country_code];
98  }
99  return true;
100  }
101  return false;
102 }
103 
111 
112  if (!$language) {
113  $language = 'en';
114  }
115 
116  return $language;
117 }
118 
124 function get_language() {
125  global $CONFIG;
126 
128  $language = false;
129 
130  if (($user) && ($user->language)) {
131  $language = $user->language;
132  }
133 
134  if ((!$language) && (isset($CONFIG->language)) && ($CONFIG->language)) {
135  $language = $CONFIG->language;
136  }
137 
138  if ($language) {
139  return $language;
140  }
141 
142  return false;
143 }
144 
149  global $CONFIG;
150 
151  if ($CONFIG->system_cache_enabled) {
152  $loaded = true;
153  $languages = array_unique(array('en', get_current_language()));
154  foreach ($languages as $language) {
155  $data = elgg_load_system_cache("$language.lang");
156  if ($data) {
157  add_translation($language, unserialize($data));
158  } else {
159  $loaded = false;
160  }
161  }
162 
163  if ($loaded) {
164  $CONFIG->i18n_loaded_from_cache = true;
165  // this is here to force
166  $CONFIG->language_paths[dirname(dirname(dirname(__FILE__))) . "/languages/"] = true;
167  return;
168  }
169  }
170 
171  // load core translations from languages directory
172  register_translations(dirname(dirname(dirname(__FILE__))) . "/languages/");
173 }
174 
190  global $CONFIG;
191 
192  // Try to load translations from system cache
193  if (!empty($CONFIG->system_cache_enabled)) {
194  $data = elgg_load_system_cache("$language.lang");
195  if ($data) {
196  $added = add_translation($language, unserialize($data));
197 
198  if ($added) {
199  // Translations were successfully loaded from system cache
200  return true;
201  }
202  }
203  }
204 
205  // Read translations from the core languages directory
206  _elgg_register_translations_for_language(dirname(dirname(dirname(__FILE__))) . "/languages/", $language);
207 
208  // Get active plugins
209  $plugins = elgg_get_plugins('active');
210 
211  if (!$plugins) {
212  // Active plugins were not found, so no need to register plugin translations
213  return true;
214  }
215 
216  foreach ($plugins as $plugin) {
217  $languages_path = "{$plugin->getPath()}languages/";
218 
219  if (!is_dir($languages_path)) {
220  // This plugin doesn't have anything to translate
221  continue;
222  }
223 
224  $language_file = "{$languages_path}{$language}.php";
225 
226  if (!file_exists($language_file)) {
227  // This plugin doesn't have translations for the requested language
228 
229  $name = $plugin->getFriendlyName();
230  elgg_log("Plugin $name is missing translations for $language language", 'NOTICE');
231 
232  continue;
233  }
234 
235  // Register translations from the plugin languages directory
236  if (!_elgg_register_translations_for_language($languages_path, $language)) {
237  $msg = elgg_echo('ElggPlugin:Exception:CannotRegisterLanguages',
238  array($plugin->getID(), $plugin->guid, $languages_path));
239  throw new PluginException($msg);
240  }
241  }
242 
243  return true;
244 }
245 
255 function register_translations($path, $load_all = false) {
256  global $CONFIG;
257 
259 
260  // Make a note of this path just incase we need to register this language later
261  if (!isset($CONFIG->language_paths)) {
262  $CONFIG->language_paths = array();
263  }
264  $CONFIG->language_paths[$path] = true;
265 
266  // Get the current language based on site defaults and user preference
267  $current_language = get_current_language();
268 
269  elgg_log("Translations loaded from: $path", "INFO");
270 
271  // only load these files unless $load_all is true.
272  $load_language_files = array(
273  'en.php',
274  "$current_language.php"
275  );
276 
277  $load_language_files = array_unique($load_language_files);
278 
279  $handle = opendir($path);
280  if (!$handle) {
281  elgg_log("Could not open language path: $path", 'ERROR');
282  return false;
283  }
284 
285  $return = true;
286  while (false !== ($language = readdir($handle))) {
287  // ignore bad files
288  if (substr($language, 0, 1) == '.' || substr($language, -4) !== '.php') {
289  continue;
290  }
291 
292  if (in_array($language, $load_language_files) || $load_all) {
293  $result = include_once($path . $language);
294  if (!$result) {
295  $return = false;
296  continue;
297  } elseif (is_array($result)) {
298  add_translation(basename($language, '.php'), $result);
299  }
300  }
301  }
302 
303  return $return;
304 }
305 
324  global $CONFIG;
325 
327 
328  // Make a note of this path just in case we need to register this language later
329  if (!isset($CONFIG->language_paths)) {
330  $CONFIG->language_paths = array();
331  }
332  $CONFIG->language_paths[$path] = true;
333 
334  $language_file = "{$path}{$language}.php";
335 
336  if (!file_exists($language_file)) {
337  elgg_log("Could not find language file: $language_file", 'NOTICE');
338 
339  return false;
340  }
341 
342  $result = include_once($language_file);
343 
344  elgg_log("Translations loaded from: $language_file", "INFO");
345 
346  // The old (< 1.9) translation files call add_translation() independently.
347  // The new ones however just return the translations array. In this case
348  // we need to add the translation here.
349  if (is_array($result)) {
351  }
352 
353  return true;
354 }
355 
366  global $CONFIG;
367 
368  static $LANG_RELOAD_ALL_RUN;
369  if ($LANG_RELOAD_ALL_RUN) {
370  return;
371  }
372 
373  if ($CONFIG->i18n_loaded_from_cache) {
374  $cache = elgg_get_system_cache();
375  $cache_dir = $cache->getVariable("cache_path");
376  $filenames = elgg_get_file_list($cache_dir, array(), array(), array(".lang"));
377  foreach ($filenames as $filename) {
378  // Look for files matching for example 'en.lang', 'cmn.lang' or 'pt_br.lang'.
379  // Note that this regex is just for the system cache. The original language
380  // files are allowed to have uppercase letters (e.g. pt_BR.php).
381  if (preg_match('/(([a-z]{2,3})(_[a-z]{2})?)\.lang$/', $filename, $matches)) {
382  $language = $matches[1];
383  $data = elgg_load_system_cache("$language.lang");
384  if ($data) {
385  add_translation($language, unserialize($data));
386  }
387  }
388  }
389  } else {
390  foreach ($CONFIG->language_paths as $path => $dummy) {
392  }
393  }
394 
395  $LANG_RELOAD_ALL_RUN = true;
396 }
397 
405  global $CONFIG;
406 
407  // Ensure that all possible translations are loaded
409 
410  $installed = array();
411 
412  foreach ($CONFIG->translations as $k => $v) {
413  $installed[$k] = elgg_echo($k, array(), $k);
414  if (elgg_is_admin_logged_in()) {
415  $completeness = get_language_completeness($k);
416  if (($completeness < 100) && ($k != 'en')) {
417  $installed[$k] .= " (" . $completeness . "% " . elgg_echo('complete') . ")";
418  }
419  }
420  }
421 
422  return $installed;
423 }
424 
433  global $CONFIG;
434 
435  // Ensure that all possible translations are loaded
437 
439 
440  $en = count($CONFIG->translations['en']);
441 
443  if ($missing) {
444  $missing = count($missing);
445  } else {
446  $missing = 0;
447  }
448 
449  //$lang = count($CONFIG->translations[$language]);
450  $lang = $en - $missing;
451 
452  return round(($lang / $en) * 100, 2);
453 }
454 
464  global $CONFIG;
465 
466  // Ensure that all possible translations are loaded
468 
469  $missing = array();
470 
471  foreach ($CONFIG->translations['en'] as $k => $v) {
472  if ((!isset($CONFIG->translations[$language][$k]))
473  || ($CONFIG->translations[$language][$k] == $CONFIG->translations['en'][$k])) {
474  $missing[] = $k;
475  }
476  }
477 
478  if (count($missing)) {
479  return $missing;
480  }
481 
482  return false;
483 }
get_current_language()
Detect the current language being used by the current site or logged in user.
Definition: languages.php:109
$plugin
_elgg_load_translations_for_language($language)
Load both core and plugin translations for a specific language.
Definition: languages.php:189
elgg_is_admin_logged_in()
Returns whether or not the viewer is currently logged in and an admin user.
Definition: sessions.php:65
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
get_language_completeness($language)
Return the level of completeness for a given language code (compared to english)
Definition: languages.php:432
$lang
Definition: html.php:12
sanitise_filepath($path, $append_slash=true)
Sanitise file paths ensuring that they begin and end with slashes etc.
Definition: elgglib.php:484
elgg_get_system_cache()
Returns an ElggCache object suitable for caching system information.
Definition: cache.php:20
$data
Definition: opendd.php:13
reload_all_translations()
Reload all translations from all registered paths.
Definition: languages.php:365
$return
Definition: opendd.php:15
$args
Some servers don&#39;t allow PHP to check the rewrite, so try via AJAX.
_elgg_load_translations()
private
Definition: languages.php:148
$string
get_missing_language_keys($language)
Return the translation keys missing from a given language, or those that are identical to the english...
Definition: languages.php:463
$plugins
elgg_get_file_list($directory, $exceptions=array(), $list=array(), $extensions=null)
Returns a list of files in $directory.
Definition: elgglib.php:452
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
get_language()
Gets the current language in use by the system or user.
Definition: languages.php:124
global $CONFIG
sanitise_string($string)
Wrapper function for alternate English spelling (.
Definition: database.php:150
$user
Definition: ban.php:13
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1171
elgg global
Pointer to the global context.
Definition: elgglib.js:12
add_translation($country_code, $language_array)
Add a translation.
Definition: languages.php:85
elgg_log($message, $level= 'NOTICE')
Display or log a message.
Definition: elgglib.php:1083
_elgg_register_translations_for_language($path, $language)
When given a full path, finds translation files for a language and loads them.
Definition: languages.php:323
elgg_get_plugins($status= 'active', $site_guid=null)
Returns an ordered list of plugins.
Definition: plugins.php:372
elgg_load_system_cache($type)
Retrieve the contents of a system cache.
Definition: cache.php:69
register_translations($path, $load_all=false)
When given a full path, finds translation files and loads them.
Definition: languages.php:255
get_installed_translations()
Return an array of installed translations as an associative array "two letter code" => "native langua...
Definition: languages.php:404
$filename
Definition: crop.php:23
elgg_get_logged_in_user_entity()
Return the current logged in user, or null if no user is logged in.
Definition: sessions.php:32
$language
$vars[&#39;language&#39;] $vars[&#39;lc&#39;] if present, client will be sent long expires headers ...
Definition: languages.php:7
foreach(array('sitename','sitedescription', 'siteemail') as $field) $languages
$path
Definition: invalid.php:17