Elgg  Version 1.10
Translator.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\I18n;
3 
13 class Translator {
14 
20  private $CONFIG;
21 
25  public function __construct() {
26  global $CONFIG;
27  $this->CONFIG = $CONFIG;
28  $this->defaultPath = dirname(dirname(dirname(dirname(__DIR__)))) . "/languages/";
29  }
30 
42  function translate($message_key, $args = array(), $language = "") {
43 
44 
45  static $CURRENT_LANGUAGE;
46 
47  // old param order is deprecated
48  if (!is_array($args)) {
50  'As of Elgg 1.8, the 2nd arg to elgg_echo() is an array of string replacements and the 3rd arg is the language.',
51  1.8
52  );
53 
54  $language = $args;
55  $args = array();
56  }
57 
58  if (!isset($this->CONFIG->translations)) {
59  // this means we probably had an exception before translations were initialized
60  $this->registerTranslations($this->defaultPath);
61  }
62 
63  if (!$CURRENT_LANGUAGE) {
64  $CURRENT_LANGUAGE = $this->getLanguage();
65  }
66  if (!$language) {
67  $language = $CURRENT_LANGUAGE;
68  }
69 
70  if (!isset($this->CONFIG->translations[$language])) {
71  // The language being requested is not the same as the language of the
72  // logged in user, so we will have to load it separately. (Most likely
73  // we're sending a notification and the recipient is using a different
74  // language than the logged in user.)
76  }
77 
78  if (isset($this->CONFIG->translations[$language][$message_key])) {
79  $string = $this->CONFIG->translations[$language][$message_key];
80  } else if (isset($this->CONFIG->translations["en"][$message_key])) {
81  $string = $this->CONFIG->translations["en"][$message_key];
82  _elgg_services()->logger->notice(sprintf('Missing %s translation for "%s" language key', $language, $message_key));
83  } else {
84  $string = $message_key;
85  _elgg_services()->logger->notice(sprintf('Missing English translation for "%s" language key', $message_key));
86  }
87 
88  // only pass through if we have arguments to allow backward compatibility
89  // with manual sprintf() calls.
90  if ($args) {
91  $string = vsprintf($string, $args);
92  }
93 
94  return $string;
95  }
96 
110  function addTranslation($country_code, $language_array) {
111 
112  if (!isset($this->CONFIG->translations)) {
113  $this->CONFIG->translations = array();
114  }
115 
116  $country_code = strtolower($country_code);
117  $country_code = trim($country_code);
118  if (is_array($language_array) && $country_code != "") {
119  if (sizeof($language_array) > 0) {
120  if (!isset($this->CONFIG->translations[$country_code])) {
121  $this->CONFIG->translations[$country_code] = $language_array;
122  } else {
123  $this->CONFIG->translations[$country_code] = $language_array + $this->CONFIG->translations[$country_code];
124  }
125  }
126  return true;
127  }
128  return false;
129  }
130 
136  function getCurrentLanguage() {
137  $language = $this->getLanguage();
138 
139  if (!$language) {
140  $language = 'en';
141  }
142 
143  return $language;
144  }
145 
151  function getLanguage() {
152  $user = _elgg_services()->session->getLoggedInUser();
153  $language = false;
154 
155  if (($user) && ($user->language)) {
156  $language = $user->language;
157  }
158 
159  if ((!$language) && (isset($this->CONFIG->language)) && ($this->CONFIG->language)) {
160  $language = $this->CONFIG->language;
161  }
162 
163  if ($language) {
164  return $language;
165  }
166 
167  return false;
168  }
169 
173  function loadTranslations() {
174 
175 
176  if ($this->CONFIG->system_cache_enabled) {
177  $loaded = true;
178  $languages = array_unique(array('en', $this->getCurrentLanguage()));
179  foreach ($languages as $language) {
180  $data = elgg_load_system_cache("$language.lang");
181  if ($data) {
182  $this->addTranslation($language, unserialize($data));
183  } else {
184  $loaded = false;
185  }
186  }
187 
188  if ($loaded) {
189  $this->CONFIG->i18n_loaded_from_cache = true;
190  // this is here to force
191  $this->CONFIG->language_paths[$this->defaultPath] = true;
192  return;
193  }
194  }
195 
196  // load core translations from languages directory
197  $this->registerTranslations($this->defaultPath);
198  }
199 
200 
201 
211  function registerTranslations($path, $load_all = false) {
213 
214  // Make a note of this path just incase we need to register this language later
215  if (!isset($this->CONFIG->language_paths)) {
216  $this->CONFIG->language_paths = array();
217  }
218  $this->CONFIG->language_paths[$path] = true;
219 
220  // Get the current language based on site defaults and user preference
221  $current_language = $this->getCurrentLanguage();
222  _elgg_services()->logger->info("Translations loaded from: $path");
223 
224  // only load these files unless $load_all is true.
225  $load_language_files = array(
226  'en.php',
227  "$current_language.php"
228  );
229 
230  $load_language_files = array_unique($load_language_files);
231 
232  $handle = opendir($path);
233  if (!$handle) {
234  _elgg_services()->logger->error("Could not open language path: $path");
235  return false;
236  }
237 
238  $return = true;
239  while (false !== ($language = readdir($handle))) {
240  // ignore bad files
241  if (substr($language, 0, 1) == '.' || substr($language, -4) !== '.php') {
242  continue;
243  }
244 
245  if (in_array($language, $load_language_files) || $load_all) {
246  $result = include_once($path . $language);
247  if ($result === false) {
248  $return = false;
249  continue;
250  } elseif (is_array($result)) {
251  $this->addTranslation(basename($language, '.php'), $result);
252  }
253  }
254  }
255 
256  return $return;
257  }
258 
269 
270 
271  static $LANG_RELOAD_ALL_RUN;
272  if ($LANG_RELOAD_ALL_RUN) {
273  return;
274  }
275 
276  if ($this->CONFIG->i18n_loaded_from_cache) {
277  $cache = elgg_get_system_cache();
278  $cache_dir = $cache->getVariable("cache_path");
279  $filenames = elgg_get_file_list($cache_dir, array(), array(), array(".lang"));
280  foreach ($filenames as $filename) {
281  // Look for files matching for example 'en.lang', 'cmn.lang' or 'pt_br.lang'.
282  // Note that this regex is just for the system cache. The original language
283  // files are allowed to have uppercase letters (e.g. pt_BR.php).
284  if (preg_match('/(([a-z]{2,3})(_[a-z]{2})?)\.lang$/', $filename, $matches)) {
285  $language = $matches[1];
286  $data = elgg_load_system_cache("$language.lang");
287  if ($data) {
288  $this->addTranslation($language, unserialize($data));
289  }
290  }
291  }
292  } else {
293  foreach ($this->CONFIG->language_paths as $path => $dummy) {
294  $this->registerTranslations($path, true);
295  }
296  }
297 
298  $LANG_RELOAD_ALL_RUN = true;
299  }
300 
308 
309 
310  // Ensure that all possible translations are loaded
311  $this->reloadAllTranslations();
312 
313  $installed = array();
314 
315  foreach ($this->CONFIG->translations as $k => $v) {
316  $installed[$k] = $this->translate($k, array(), $k);
317  if (_elgg_services()->session->isAdminLoggedIn()) {
318  $completeness = $this->getLanguageCompleteness($k);
319  if (($completeness < 100) && ($k != 'en')) {
320  $installed[$k] .= " (" . $completeness . "% " . $this->translate('complete') . ")";
321  }
322  }
323  }
324 
325  return $installed;
326  }
327 
336 
337 
338  // Ensure that all possible translations are loaded
339  $this->reloadAllTranslations();
340 
342 
343  $en = count($this->CONFIG->translations['en']);
344 
345  $missing = $this->getMissingLanguageKeys($language);
346  if ($missing) {
347  $missing = count($missing);
348  } else {
349  $missing = 0;
350  }
351 
352  //$lang = count($this->CONFIG->translations[$language]);
353  $lang = $en - $missing;
354 
355  return round(($lang / $en) * 100, 2);
356  }
357 
367 
368 
369  // Ensure that all possible translations are loaded
370  $this->reloadAllTranslations();
371 
372  $missing = array();
373 
374  foreach ($this->CONFIG->translations['en'] as $k => $v) {
375  if ((!isset($this->CONFIG->translations[$language][$k]))
376  || ($this->CONFIG->translations[$language][$k] == $this->CONFIG->translations['en'][$k])) {
377  $missing[] = $k;
378  }
379  }
380 
381  if (count($missing)) {
382  return $missing;
383  }
384 
385  return false;
386  }
387 
388 }
getInstalledTranslations()
Return an array of installed translations as an associative array "two letter code" => "native langua...
Definition: Translator.php:307
_elgg_load_translations_for_language($language)
Load both core and plugin translations for a specific language.
Definition: languages.php:74
$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:368
elgg_get_system_cache()
Returns an object suitable for caching system information.
Definition: cache.php:20
$data
Definition: opendd.php:13
if(!$autoload_available) _elgg_services()
Definition: autoloader.php:20
$return
Definition: opendd.php:15
$args
Some servers don&#39;t allow PHP to check the rewrite, so try via AJAX.
$string
getMissingLanguageKeys($language)
Return the translation keys missing from a given language, or those that are identical to the english...
Definition: Translator.php:366
addTranslation($country_code, $language_array)
Add a translation.
Definition: Translator.php:110
elgg_get_file_list($directory, $exceptions=array(), $list=array(), $extensions=null)
Returns a list of files in $directory.
Definition: elgglib.php:336
reloadAllTranslations()
Reload all translations from all registered paths.
Definition: Translator.php:268
registerTranslations($path, $load_all=false)
When given a full path, finds translation files and loads them.
Definition: Translator.php:211
loadTranslations()
private
Definition: Translator.php:173
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:1055
elgg global
Pointer to the global context.
Definition: elgglib.js:12
getCurrentLanguage()
Detect the current language being used by the current site or logged in user.
Definition: Translator.php:136
getLanguageCompleteness($language)
Return the level of completeness for a given language code (compared to english)
Definition: Translator.php:335
elgg_load_system_cache($type)
Retrieve the contents of a system cache.
Definition: cache.php:50
translate($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: Translator.php:42
__construct()
Initializes new translator.
Definition: Translator.php:25
$filename
Definition: crop.php:23
$language
$vars[&#39;language&#39;] $vars[&#39;lc&#39;] if present, client will be sent long expires headers ...
Definition: languages.php:7
getLanguage()
Gets the current language in use by the system or user.
Definition: Translator.php:151
$path
Definition: invalid.php:17
foreach(array('sitename','sitedescription', 'siteemail', 'default_limit') as $field) $languages