diff --git a/main/inc/lib/internationalization.lib.php b/main/inc/lib/internationalization.lib.php new file mode 100644 index 0000000000..c04f2e61ff --- /dev/null +++ b/main/inc/lib/internationalization.lib.php @@ -0,0 +1,133 @@ +, Ghent University + * @author Christophe Gesche + * originally inspired from from PhpMyAdmin + * @author Ivan Tcholakov, 2009, code refactoring, adding support for predefined date/time formats. + * @param string/int $date_format The date pattern. See the php-manual about the function strftime(). + * Note: For $date_format the following integer constants may be used for using predefined date/time + * formats in the Dokeos system: TIME_NO_SEC_FORMAT, DATE_FORMAT_SHORT, DATE_FORMAT_LONG, DATE_TIME_FORMAT_LONG. + * @param int $time_stamp (optional) Time as an integer value. The default value -1 means now, the function time() is called internally. + * @param string $language (optional) Language indentificator. If it is omited, the current interface language is assumed. + * @return string Returns the formatted date. + * @link http://php.net/manual/en/function.strftime.php + */ +function api_format_date($date_format, $time_stamp = -1, $language = null) { + if ($time_stamp == -1) { + $time_stamp = time(); + } + if (is_int($date_format)) { + switch ($date_format) { + case TIME_NO_SEC_FORMAT: + $date_format = get_lang('timeNoSecFormat', '', $language); + break; + case DATE_FORMAT_SHORT: + $date_format = get_lang('dateFormatShort', '', $language); + break; + case DATE_FORMAT_LONG: + $date_format = get_lang('dateFormatShort', '', $language); + break; + case DATE_TIME_FORMAT_LONG: + $date_format = get_lang('dateTimeFormatLong', '', $language); + break; + default: + $date_format = get_lang('dateTimeFormatLong', '', $language); + } + } + // We replace %a %A %b %B masks of date format with translated strings. + $translated = &_api_get_day_month_names($language); + $date_format = str_replace(array('%A', '%a', '%B', '%b'), + array($translated['days_long'][(int)strftime('%w', $time_stamp)], + $translated['days_short'][(int)strftime('%w', $time_stamp)], + $translated['months_long'][(int)strftime('%m', $time_stamp) - 1], + $translated['months_short'][(int)strftime('%m', $time_stamp) - 1]), + $date_format); + return strftime($date_format, $time_stamp); +} + +/** + * Returns an array of translated week days in short names. + * @param string $language (optional) Language indentificator. If it is omited, the current interface language is assumed. + * @return string Returns an array of week days (short names). + * Example: api_get_week_days_short('english') means array('Sun', 'Mon', ... 'Sat'). + */ +function api_get_week_days_short($language = null) { + $days = &_api_get_day_month_names($language); + return $days['days_short']; +} + +/** + * Returns an array of translated week days. + * @param string $language (optional) Language indentificator. If it is omited, the current interface language is assumed. + * @return string Returns an array of week days. + * Example: api_get_week_days_long('english') means array('Sunday, 'Monday', ... 'Saturday'). + */ +function api_get_week_days_long($language = null) { + $days = &_api_get_day_month_names($language); + return $days['days_long']; +} + +/** + * Returns an array of translated months in short names. + * @param string $language (optional) Language indentificator. If it is omited, the current interface language is assumed. + * @return string Returns an array of months (short names). + * Example: api_get_months_short('english') means array('Jan', 'Feb', ... 'Dec'). + */ +function api_get_months_short($language = null) { + $months = &_api_get_day_month_names($language); + return $months['months_short']; +} + +/** + * Returns an array of translated months. + * @param string $language (optional) Language indentificator. If it is omited, the current interface language is assumed. + * @return string Returns an array of months. + * Example: api_get_months_long('english') means array('January, 'February' ... 'December'). + */ +function api_get_months_long($language = null) { + $months = &_api_get_day_month_names($language); + return $months['months_long']; +} + + +/** + * ---------------------------------------------------------------------------- + * Functions for internal use behind this API. + * ---------------------------------------------------------------------------- + */ + +require_once dirname(__FILE__).'/internationalization_internal.lib.php'; diff --git a/main/inc/lib/internationalization_internal.lib.php b/main/inc/lib/internationalization_internal.lib.php new file mode 100644 index 0000000000..fc46cc0999 --- /dev/null +++ b/main/inc/lib/internationalization_internal.lib.php @@ -0,0 +1,46 @@ + $value) { if ($properties[$key]['upper'] == $codepoint && count($properties[$key]['lower'][0]) === 1) { @@ -1215,7 +1215,7 @@ function api_strtoupper($string, $encoding = null) { $matched = true; } else { $matched = false; - $properties = _api_utf8_get_letter_case_properties($codepoint); + $properties = &_api_utf8_get_letter_case_properties($codepoint); $property_count = count($properties); if (!empty($properties)) { foreach ($properties as $key => $value) { diff --git a/main/inc/lib/multibyte_string_functions_internal.lib.php b/main/inc/lib/multibyte_string_functions_internal.lib.php index 29186e42f0..c3b5aa0846 100644 --- a/main/inc/lib/multibyte_string_functions_internal.lib.php +++ b/main/inc/lib/multibyte_string_functions_internal.lib.php @@ -61,13 +61,13 @@ function _api_convert_encoding($string, $to_encoding, $from_encoding) { return $string; } if (!isset($character_map[$to])) { - $character_map[$to] = _api_parse_character_map($to); + $character_map[$to] = &_api_parse_character_map($to); } if ($character_map[$to] === false) { return $string; } if (!isset($character_map[$from])) { - $character_map[$from] = _api_parse_character_map($from); + $character_map[$from] = &_api_parse_character_map($from); } if ($character_map[$from] === false) { return $string; @@ -372,7 +372,7 @@ function _api_html_entity_from_unicode($codepoint) { * @param string $type (optional) The type of initial case to be altered: 'lower' (default) or 'upper'. * @return array Returns an array with properties used to change case of the character. */ -function _api_utf8_get_letter_case_properties($codepoint, $type = 'lower') { +function &_api_utf8_get_letter_case_properties($codepoint, $type = 'lower') { static $config = array(); static $range = array(); if (!isset($range[$codepoint])) { diff --git a/main/inc/lib/text.lib.php b/main/inc/lib/text.lib.php index 9009de4bff..121f68aa7a 100644 --- a/main/inc/lib/text.lib.php +++ b/main/inc/lib/text.lib.php @@ -61,8 +61,9 @@ function make_clickable($string) * @return the formatted date */ -function format_locale_date($date_format, $time_stamp = -1) +function format_locale_date($date_format, $time_stamp = -1, $language = null) { + /* static $initialized = false; static $days_short, $days_long, $months_short, $months_long; @@ -91,7 +92,8 @@ function format_locale_date($date_format, $time_stamp = -1) $date = ereg_replace('%[b]', $months_short[(int)strftime('%m', $time_stamp)-1], $date); return strftime($date, $time_stamp); - + */ + return api_format_date($date_format, $time_stamp, $language); } // end function format_locale_date