languages.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * Elgg language module
  4. * Functions to manage language and translations.
  5. *
  6. * @package Elgg.Core
  7. * @subpackage Languages
  8. */
  9. /**
  10. * Given a message key, returns an appropriately translated full-text string
  11. *
  12. * @param string $message_key The short message code
  13. * @param array $args An array of arguments to pass through vsprintf().
  14. * @param string $language Optionally, the standard language code
  15. * (defaults to site/user default, then English)
  16. *
  17. * @return string Either the translated string, the English string,
  18. * or the original language string.
  19. */
  20. function elgg_echo($message_key, $args = array(), $language = "") {
  21. return _elgg_services()->translator->translate($message_key, $args, $language);
  22. }
  23. /**
  24. * Add a translation.
  25. *
  26. * Translations are arrays in the Zend Translation array format, eg:
  27. *
  28. * $english = array('message1' => 'message1', 'message2' => 'message2');
  29. * $german = array('message1' => 'Nachricht1','message2' => 'Nachricht2');
  30. *
  31. * @param string $country_code Standard country code (eg 'en', 'nl', 'es')
  32. * @param array $language_array Formatted array of strings
  33. *
  34. * @return bool Depending on success
  35. */
  36. function add_translation($country_code, $language_array) {
  37. return _elgg_services()->translator->addTranslation($country_code, $language_array);
  38. }
  39. /**
  40. * Detect the current language being used by the current site or logged in user.
  41. *
  42. * @return string The language code for the site/user or "en" if not set
  43. */
  44. function get_current_language() {
  45. return _elgg_services()->translator->getCurrentLanguage();
  46. }
  47. /**
  48. * Gets the current language in use by the system or user.
  49. *
  50. * @return string The language code (eg "en") or false if not set
  51. */
  52. function get_language() {
  53. return _elgg_services()->translator->getLanguage();
  54. }
  55. /**
  56. * Load both core and plugin translations for a specific language
  57. *
  58. * This can be used to load translations on-demand in case we need
  59. * to translate something to a language not loaded by default for
  60. * the current user.
  61. *
  62. * @param $language Language code
  63. * @return bool
  64. *
  65. * @since 1.9.4
  66. * @throws PluginException
  67. * @access private
  68. */
  69. function _elgg_load_translations_for_language($language) {
  70. global $CONFIG;
  71. // Try to load translations from system cache
  72. if (!empty($CONFIG->system_cache_enabled)) {
  73. $data = elgg_load_system_cache("$language.lang");
  74. if ($data) {
  75. $added = add_translation($language, unserialize($data));
  76. if ($added) {
  77. // Translations were successfully loaded from system cache
  78. return true;
  79. }
  80. }
  81. }
  82. // Read translations from the core languages directory
  83. _elgg_register_translations_for_language(dirname(dirname(dirname(__FILE__))) . "/languages/", $language);
  84. // Get active plugins
  85. $plugins = elgg_get_plugins('active');
  86. if (!$plugins) {
  87. // Active plugins were not found, so no need to register plugin translations
  88. return true;
  89. }
  90. foreach ($plugins as $plugin) {
  91. $languages_path = "{$plugin->getPath()}languages/";
  92. if (!is_dir($languages_path)) {
  93. // This plugin doesn't have anything to translate
  94. continue;
  95. }
  96. $language_file = "{$languages_path}{$language}.php";
  97. if (!file_exists($language_file)) {
  98. // This plugin doesn't have translations for the requested language
  99. $name = $plugin->getFriendlyName();
  100. elgg_log("Plugin $name is missing translations for $language language", 'NOTICE');
  101. continue;
  102. }
  103. // Register translations from the plugin languages directory
  104. if (!_elgg_register_translations_for_language($languages_path, $language)) {
  105. $msg = elgg_echo('ElggPlugin:Exception:CannotRegisterLanguages',
  106. array($plugin->getID(), $plugin->guid, $languages_path));
  107. throw new PluginException($msg);
  108. }
  109. }
  110. return true;
  111. }
  112. /**
  113. * When given a full path, finds translation files and loads them
  114. *
  115. * @param string $path Full path
  116. * @param bool $load_all If true all languages are loaded, if
  117. * false only the current language + en are loaded
  118. *
  119. * @return bool success
  120. */
  121. function register_translations($path, $load_all = false) {
  122. return _elgg_services()->translator->registerTranslations($path, $load_all);
  123. }
  124. /**
  125. * When given a full path, finds translation files for a language and loads them
  126. *
  127. * This function was added in 1.9.4 to make it possible to load translations
  128. * for individual languages on-demand. This is needed in order to send
  129. * notifications in the recipient's language (see #3151 and #7241).
  130. *
  131. * @todo Replace this function in 1.10 by adding $language as the third parameter
  132. * to register_translations().
  133. *
  134. * @access private
  135. * @since 1.9.4
  136. *
  137. * @param string $path Full path of the directory (with trailing slash)
  138. * @param string $language Language code
  139. * @return bool success
  140. */
  141. function _elgg_register_translations_for_language($path, $language) {
  142. global $CONFIG;
  143. $path = sanitise_filepath($path);
  144. // Make a note of this path just in case we need to register this language later
  145. if (!isset($CONFIG->language_paths)) {
  146. $CONFIG->language_paths = array();
  147. }
  148. $CONFIG->language_paths[$path] = true;
  149. $language_file = "{$path}{$language}.php";
  150. if (!file_exists($language_file)) {
  151. elgg_log("Could not find language file: $language_file", 'NOTICE');
  152. return false;
  153. }
  154. $result = include_once($language_file);
  155. elgg_log("Translations loaded from: $language_file", "INFO");
  156. // The old (< 1.9) translation files call add_translation() independently.
  157. // The new ones however just return the translations array. In this case
  158. // we need to add the translation here.
  159. if (is_array($result)) {
  160. return add_translation($language, $result);
  161. }
  162. return true;
  163. }
  164. /**
  165. * Reload all translations from all registered paths.
  166. *
  167. * This is only called by functions which need to know all possible translations.
  168. *
  169. * @todo Better on demand loading based on language_paths array
  170. *
  171. * @return void
  172. */
  173. function reload_all_translations() {
  174. return _elgg_services()->translator->reloadAllTranslations();
  175. }
  176. /**
  177. * Return an array of installed translations as an associative
  178. * array "two letter code" => "native language name".
  179. *
  180. * @return array
  181. */
  182. function get_installed_translations() {
  183. return _elgg_services()->translator->getInstalledTranslations();
  184. }
  185. /**
  186. * Return the level of completeness for a given language code (compared to english)
  187. *
  188. * @param string $language Language
  189. *
  190. * @return int
  191. */
  192. function get_language_completeness($language) {
  193. return _elgg_services()->translator->getLanguageCompleteness($language);
  194. }
  195. /**
  196. * Return the translation keys missing from a given language,
  197. * or those that are identical to the english version.
  198. *
  199. * @param string $language The language
  200. *
  201. * @return mixed
  202. */
  203. function get_missing_language_keys($language) {
  204. return _elgg_services()->translator->getMissingLanguageKeys($language);
  205. }
  206. /**
  207. * Check if a give language key exists
  208. *
  209. * @param string $key The translation key
  210. * @param string $language The language
  211. *
  212. * @return bool
  213. * @since 1.11
  214. */
  215. function elgg_language_key_exists($key, $language = 'en') {
  216. return _elgg_services()->translator->languageKeyExists($key, $language);
  217. }
  218. /**
  219. * Initializes simplecache views for translations
  220. *
  221. * @return void
  222. */
  223. function _elgg_translations_init() {
  224. $translations = \Elgg\I18n\Translator::getAllLanguageCodes();
  225. foreach ($translations as $language_code) {
  226. // make the js view available for each language
  227. elgg_extend_view("js/languages/$language_code.js", "js/languages");
  228. // register the js view for use in simplecache
  229. elgg_register_simplecache_view("js/languages/$language_code.js");
  230. }
  231. }
  232. return function(\Elgg\EventsService $events) {
  233. $events->registerHandler('init', 'system', '_elgg_translations_init');
  234. };