Elgg  Version 1.11
languages.php
Go to the documentation of this file.
1 <?php
21 function elgg_echo($message_key, $args = array(), $language = "") {
22  return _elgg_services()->translator->translate($message_key, $args, $language);
23 }
24 
38 function add_translation($country_code, $language_array) {
39  return _elgg_services()->translator->addTranslation($country_code, $language_array);
40 }
41 
48  return _elgg_services()->translator->getCurrentLanguage();
49 }
50 
56 function get_language() {
57  return _elgg_services()->translator->getLanguage();
58 }
59 
76 
77  // Try to load translations from system cache
78  if (!empty($CONFIG->system_cache_enabled)) {
79  $data = elgg_load_system_cache("$language.lang");
80  if ($data) {
81  $added = add_translation($language, unserialize($data));
82 
83  if ($added) {
84  // Translations were successfully loaded from system cache
85  return true;
86  }
87  }
88  }
89 
90  // Read translations from the core languages directory
91  _elgg_register_translations_for_language(dirname(dirname(dirname(__FILE__))) . "/languages/", $language);
92 
93  // Get active plugins
94  $plugins = elgg_get_plugins('active');
95 
96  if (!$plugins) {
97  // Active plugins were not found, so no need to register plugin translations
98  return true;
99  }
100 
101  foreach ($plugins as $plugin) {
102  $languages_path = "{$plugin->getPath()}languages/";
103 
104  if (!is_dir($languages_path)) {
105  // This plugin doesn't have anything to translate
106  continue;
107  }
108 
109  $language_file = "{$languages_path}{$language}.php";
110 
111  if (!file_exists($language_file)) {
112  // This plugin doesn't have translations for the requested language
113 
114  $name = $plugin->getFriendlyName();
115  elgg_log("Plugin $name is missing translations for $language language", 'NOTICE');
116 
117  continue;
118  }
119 
120  // Register translations from the plugin languages directory
121  if (!_elgg_register_translations_for_language($languages_path, $language)) {
122  $msg = elgg_echo('ElggPlugin:Exception:CannotRegisterLanguages',
123  array($plugin->getID(), $plugin->guid, $languages_path));
124  throw new PluginException($msg);
125  }
126  }
127 
128  return true;
129 }
130 
140 function register_translations($path, $load_all = false) {
141  return _elgg_services()->translator->registerTranslations($path, $load_all);
142 }
143 
162  global $CONFIG;
163 
165 
166  // Make a note of this path just in case we need to register this language later
167  if (!isset($CONFIG->language_paths)) {
168  $CONFIG->language_paths = array();
169  }
170  $CONFIG->language_paths[$path] = true;
171 
172  $language_file = "{$path}{$language}.php";
173 
174  if (!file_exists($language_file)) {
175  elgg_log("Could not find language file: $language_file", 'NOTICE');
176 
177  return false;
178  }
179 
180  $result = include_once($language_file);
181 
182  elgg_log("Translations loaded from: $language_file", "INFO");
183 
184  // The old (< 1.9) translation files call add_translation() independently.
185  // The new ones however just return the translations array. In this case
186  // we need to add the translation here.
187  if (is_array($result)) {
189  }
190 
191  return true;
192 }
193 
204  return _elgg_services()->translator->reloadAllTranslations();
205 }
206 
214  return _elgg_services()->translator->getInstalledTranslations();
215 }
216 
225  return _elgg_services()->translator->getLanguageCompleteness($language);
226 }
227 
237  return _elgg_services()->translator->getMissingLanguageKeys($language);
238 }
239 
250  return _elgg_services()->translator->languageKeyExists($key, $language);
251 }
252 
259  $translations = _elgg_services()->translator->getAllLanguageCodes();
260  foreach ($translations as $language_code) {
261  // make the js view available for each language
262  elgg_extend_view("js/languages/$language_code.js", "js/languages");
263 
264  // register the js view for use in simplecache
265  elgg_register_simplecache_view("js/languages/$language_code.js");
266  }
267 }
268 
269 return function(\Elgg\EventsService $events) {
270  $events->registerHandler('init', 'system', '_elgg_translations_init');
271 };
get_current_language()
Detect the current language being used by the current site or logged in user.
Definition: languages.php:47
$plugin
_elgg_load_translations_for_language($language)
Load both core and plugin translations for a specific language.
Definition: languages.php:74
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:224
elgg_register_simplecache_view($view_name)
Registers a view to simple cache.
Definition: cache.php:98
sanitise_filepath($path, $append_slash=true)
Sanitise file paths ensuring that they begin and end with slashes etc.
Definition: elgglib.php:368
$translations
Definition: languages.php:23
elgg_language_key_exists($key, $language= 'en')
Check if a give language key exists.
Definition: languages.php:249
$data
Definition: opendd.php:13
reload_all_translations()
Reload all translations from all registered paths.
Definition: languages.php:203
$args
Some servers don&#39;t allow PHP to check the rewrite, so try via AJAX.
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:236
$plugins
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$key
Definition: summary.php:34
_elgg_services()
Definition: autoloader.php:14
get_language()
Gets the current language in use by the system or user.
Definition: languages.php:56
global $CONFIG
elgg_extend_view($view, $view_extension, $priority=501, $viewtype= '')
Extends a view with another view.
Definition: views.php:401
elgg global
Pointer to the global context.
Definition: elgglib.js:12
add_translation($country_code, $language_array)
Add a translation.
Definition: languages.php:38
elgg_log($message, $level= 'NOTICE')
Display or log a message.
Definition: elgglib.php:933
_elgg_register_translations_for_language($path, $language)
When given a full path, finds translation files for a language and loads them.
Definition: languages.php:161
elgg_get_plugins($status= 'active', $site_guid=null)
Returns an ordered list of plugins.
Definition: plugins.php:162
elgg_load_system_cache($type)
Retrieve the contents of a system cache.
Definition: cache.php:50
_elgg_translations_init()
Initializes simplecache views for translations.
Definition: languages.php:258
register_translations($path, $load_all=false)
When given a full path, finds translation files and loads them.
Definition: languages.php:140
get_installed_translations()
Return an array of installed translations as an associative array "two letter code" => "native langua...
Definition: languages.php:213
$language
$vars[&#39;language&#39;]
Definition: languages.php:6
$path
Definition: invalid.php:17