functions.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Functions for the language selector
  4. */
  5. /**
  6. * Returns the translations that are allowed to be used
  7. *
  8. * @return array
  9. */
  10. function language_selector_get_allowed_translations() {
  11. $configured_allowed = elgg_get_plugin_setting("allowed_languages", "language_selector");
  12. if (empty($configured_allowed)) {
  13. $allowed = array("en");
  14. $installed_languages = get_installed_translations();
  15. $min_completeness = (int) elgg_get_plugin_setting("min_completeness", "language_selector");
  16. if ($min_completeness > 0) {
  17. if (elgg_is_active_plugin("translation_editor")) {
  18. $completeness_function = "translation_editor_get_language_completeness";
  19. } else {
  20. $completeness_function = "get_language_completeness";
  21. }
  22. foreach ($installed_languages as $lang_id => $lang_description) {
  23. if ($lang_id != "en") {
  24. if (($completeness = $completeness_function($lang_id)) >= $min_completeness) {
  25. $allowed[] = $lang_id;
  26. }
  27. }
  28. }
  29. }
  30. elgg_set_plugin_setting("allowed_languages", implode(",", $allowed), "language_selector");
  31. } else {
  32. $allowed = string_to_tag_array($configured_allowed);
  33. }
  34. return $allowed;
  35. }
  36. /**
  37. * Sets the language for the logged out user
  38. *
  39. * @return void
  40. */
  41. function language_selector_set_logged_out_user_language() {
  42. global $CONFIG;
  43. if (elgg_is_logged_in()) {
  44. return;
  45. }
  46. if (!empty($_COOKIE['client_language'])) {
  47. // switched with language selector
  48. $new_lang = $_COOKIE['client_language'];
  49. } else {
  50. $browserlang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
  51. if (!empty($browserlang)) {
  52. // autodetect language
  53. if (elgg_get_plugin_setting("autodetect", "language_selector") == "yes") {
  54. $new_lang = $browserlang;
  55. }
  56. }
  57. }
  58. if (!empty($new_lang) && ($new_lang !== $CONFIG->language)) {
  59. $allowed = language_selector_get_allowed_translations();
  60. if (in_array($new_lang, $allowed)) {
  61. // set new language
  62. $CONFIG->language = $new_lang;
  63. // language has been change; reload language keys
  64. if (elgg_is_active_plugin("translation_editor")) {
  65. translation_editor_load_translations();
  66. } else {
  67. reload_all_translations();
  68. }
  69. }
  70. }
  71. }
  72. /**
  73. * Unset the plugin setting so it will be reset when used the next time
  74. *
  75. * @return void
  76. */
  77. function language_selector_invalidate_setting() {
  78. elgg_unset_plugin_setting("allowed_languages", "language_selector");
  79. }