internationalization_internal.lib.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * ==============================================================================
  4. * File: internationalization_internal.lib.php
  5. * Main API extension library for Dokeos 1.8.6+ LMS,
  6. * contains functions for internal use only.
  7. * License: GNU/GPL version 2 or later (Free Software Foundation)
  8. * @author: Ivan Tcholakov, ivantcholakov@gmail.com, 2009
  9. * @package dokeos.library
  10. * ==============================================================================
  11. *
  12. * Note: All functions and data structures here are not to be used directly.
  13. * See the file internationalization.lib.php which contains the "public" API.
  14. */
  15. /**
  16. * ----------------------------------------------------------------------------
  17. * Appendix to "Language support"
  18. * ----------------------------------------------------------------------------
  19. */
  20. /**
  21. * Upgrades the function get_lang() with the following logic:
  22. * 1. Checks whether the retrieved human language string is UTF-8 valid or not.
  23. * 2. If the system encoding is UTF-8 and the string is not UTF-8, the function
  24. * performs conversion from supposed non UTF-8 encodeng.
  25. * 3. If the system encoding is non UTF-8 but the string is valid UTF-8, then
  26. * conversion from UTF-8 is performed.
  27. * 4. At the end the string is purified from HTML entities.
  28. * @param string $string This is the retrieved human language string.
  29. * @param string $language A language identiticator.
  30. * @return string Returns the human language string, checked for proper encoding and purified.
  31. */
  32. function & _get_lang_purify(& $string, & $language) {
  33. $system_encoding = api_get_system_encoding();
  34. if (api_is_utf8($system_encoding)) {
  35. if (!api_is_valid_utf8($string)) {
  36. $string = api_utf8_encode($string, api_get_non_utf8_encoding($language));
  37. }
  38. } else {
  39. if (api_is_valid_utf8($string)) {
  40. $string = api_utf8_decode($string, $system_encoding);
  41. }
  42. }
  43. return api_html_entity_decode($string, ENT_QUOTES, $system_encoding);
  44. }
  45. /**
  46. * ----------------------------------------------------------------------------
  47. * Appendix to "Date and time formats"
  48. * ----------------------------------------------------------------------------
  49. */
  50. /**
  51. * Returns an array of translated week days and months, short and normal names.
  52. * @param string $language (optional) Language indentificator. If it is omited, the current interface language is assumed.
  53. * @return array Returns a multidimensional array with translated week days and months.
  54. */
  55. function &_api_get_day_month_names($language = null) {
  56. static $date_parts = array();
  57. if (empty($language)) {
  58. $language = api_get_interface_language();
  59. }
  60. if (!isset($date_parts[$language])) {
  61. $week_day = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  62. $month = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
  63. for ($i = 0; $i < 7; $i++) {
  64. $date_parts[$language]['days_short'][] = get_lang($week_day[$i].'Short', '', $language);
  65. $date_parts[$language]['days_long'][] = get_lang($week_day[$i].'Long', '', $language);
  66. }
  67. for ($i = 0; $i < 12; $i++) {
  68. $date_parts[$language]['months_short'][] = get_lang($month[$i].'Short', '', $language);
  69. $date_parts[$language]['months_long'][] = get_lang($month[$i].'Long', '', $language);
  70. }
  71. }
  72. return $date_parts[$language];
  73. }
  74. /**
  75. * ----------------------------------------------------------------------------
  76. * Appendix to "Name order conventions"
  77. * ----------------------------------------------------------------------------
  78. */
  79. /**
  80. * Returns returns person name convention for a given language.
  81. * @param string $language The input language.
  82. * @param string $type The type of the requested convention. It may be 'format' for name order convention or 'sort_by' for name sorting convention.
  83. * @return mixed Depending of the requested type, the returned result may be string or boolean; null is returned on error;
  84. */
  85. function _api_get_person_name_convention($language, $type) {
  86. static $conventions;
  87. $language = api_refine_language_id($language);
  88. if (!isset($conventions)) {
  89. $file = dirname(__FILE__) . '/internationalization_database/name_order_conventions.php';
  90. if (file_exists($file)) {
  91. $conventions = include ($file);
  92. } else {
  93. $conventions = array('english' => array('format' => 'title first_name last_name', 'sort_by' => 'first_name'));
  94. }
  95. $search = array('first_name', 'last_name', 'title');
  96. $replacement = array('%f', '%l', '%t');
  97. foreach (array_keys($conventions) as $key) {
  98. $conventions[$key]['format'] = _api_validate_person_name_format(_api_clean_person_name(str_replace('%', ' %', str_ireplace($search, $replacement, $conventions[$key]['format']))));
  99. $conventions[$key]['sort_by'] = strtolower($conventions[$key]['sort_by']) != 'last_name' ? true : false;
  100. }
  101. }
  102. switch ($type) {
  103. case 'format':
  104. return is_string($conventions[$language]['format']) ? $conventions[$language]['format'] : '%t %f %l';
  105. case 'sort_by':
  106. return is_bool($conventions[$language]['sort_by']) ? $conventions[$language]['sort_by'] : true;
  107. }
  108. return null;
  109. }
  110. /**
  111. * Replaces non-valid formats for person names with the default (English) format.
  112. * @param string $format The input format to be verified.
  113. * @return bool Returns the same format if is is valid, otherwise returns a valid English format.
  114. */
  115. function _api_validate_person_name_format($format) {
  116. if (empty($format) || strpos($format, '%f') === false || strpos($format, '%l') === false) {
  117. return '%t %f %l';
  118. }
  119. return $format;
  120. }
  121. /**
  122. * Removes leading, trailing and duplicate whitespace and/or commas in a full person name.
  123. * Cleaning is needed for the cases when not all parts of the name are available or when the name is constructed using a "dirty" pattern.
  124. * @param string $person_name The input person name.
  125. * @return string Returns cleaned person name.
  126. */
  127. function _api_clean_person_name($person_name) {
  128. return preg_replace(array('/\s+/', '/, ,/', '/,+/', '/^[ ,]/', '/[ ,]$/'), array(' ', ', ', ',', '', ''), $person_name);
  129. }