diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-10-13 21:34:15 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-10-13 21:34:15 +0000 |
commit | 04d0ef5c8ce2646aeb3879f656e34a3836110ff2 (patch) | |
tree | db4751e6435802ed060f3bd63114db3987977cba /includes | |
parent | 24c259cdd87e747187aecf94dc1f1834e7602adf (diff) | |
download | brdo-04d0ef5c8ce2646aeb3879f656e34a3836110ff2.tar.gz brdo-04d0ef5c8ce2646aeb3879f656e34a3836110ff2.tar.bz2 |
- Patch #11623 by sun, stella, Gábor Hojtsy, Pancho: add ability to localize date formats.
Diffstat (limited to 'includes')
-rw-r--r-- | includes/date.inc | 197 | ||||
-rw-r--r-- | includes/locale.inc | 84 |
2 files changed, 281 insertions, 0 deletions
diff --git a/includes/date.inc b/includes/date.inc new file mode 100644 index 000000000..6158ac22d --- /dev/null +++ b/includes/date.inc @@ -0,0 +1,197 @@ +<?php +// $Id$ + +/** + * @file + * Initialize the list of date formats and their locales. + */ + +/** + * Implements hook_date_formats(). + */ +function system_default_date_formats() { + $formats = array(); + + // Short date formats. + $formats[] = array( + 'type' => 'short', + 'format' => 'Y-m-d H:i', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'short', + 'format' => 'm/d/Y - H:i', + 'locales' => array('en-us'), + ); + $formats[] = array( + 'type' => 'short', + 'format' => 'd/m/Y - H:i', + 'locales' => array('en-gb', 'en-hk', 'en-ie', 'el-gr', 'es-es', 'fr-be', 'fr-fr', 'fr-lu', 'it-it', 'nl-be', 'pt-pt'), + ); + $formats[] = array( + 'type' => 'short', + 'format' => 'Y/m/d - H:i', + 'locales' => array('en-ca', 'fr-ca', 'no-no', 'sv-se'), + ); + $formats[] = array( + 'type' => 'short', + 'format' => 'd.m.Y - H:i', + 'locales' => array('de-ch', 'de-de', 'de-lu', 'fi-fi', 'fr-ch', 'is-is', 'pl-pl', 'ro-ro', 'ru-ru'), + ); + $formats[] = array( + 'type' => 'short', + 'format' => 'm/d/Y - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'short', + 'format' => 'd/m/Y - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'short', + 'format' => 'Y/m/d - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'short', + 'format' => 'M j Y - H:i', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'short', + 'format' => 'j M Y - H:i', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'short', + 'format' => 'Y M j - H:i', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'short', + 'format' => 'M j Y - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'short', + 'format' => 'j M Y - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'short', + 'format' => 'Y M j - g:ia', + 'locales' => array(), + ); + + // Medium date formats. + $formats[] = array( + 'type' => 'medium', + 'format' => 'D, Y-m-d H:i', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'medium', + 'format' => 'D, m/d/Y - H:i', + 'locales' => array('en-us'), + ); + $formats[] = array( + 'type' => 'medium', + 'format' => 'D, d/m/Y - H:i', + 'locales' => array('en-gb', 'en-hk', 'en-ie', 'el-gr', 'es-es', 'fr-be', 'fr-fr', 'fr-lu', 'it-it', 'nl-be', 'pt-pt'), + ); + $formats[] = array( + 'type' => 'medium', + 'format' => 'D, Y/m/d - H:i', + 'locales' => array('en-ca', 'fr-ca', 'no-no', 'sv-se'), + ); + $formats[] = array( + 'type' => 'medium', + 'format' => 'F j, Y - H:i', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'medium', + 'format' => 'j F, Y - H:i', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'medium', + 'format' => 'Y, F j - H:i', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'medium', + 'format' => 'D, m/d/Y - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'medium', + 'format' => 'D, d/m/Y - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'medium', + 'format' => 'D, Y/m/d - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'medium', + 'format' => 'F j, Y - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'medium', + 'format' => 'j F Y - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'medium', + 'format' => 'Y, F j - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'medium', + 'format' => 'j. F Y - G:i', + 'locales' => array(), + ); + + // Long date formats. + $formats[] = array( + 'type' => 'long', + 'format' => 'l, F j, Y - H:i', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'long', + 'format' => 'l, j F, Y - H:i', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'long', + 'format' => 'l, Y, F j - H:i', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'long', + 'format' => 'l, F j, Y - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'long', + 'format' => 'l, j F Y - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'long', + 'format' => 'l, Y, F j - g:ia', + 'locales' => array(), + ); + $formats[] = array( + 'type' => 'long', + 'format' => 'l, j. F Y - G:i', + 'locales' => array(), + ); + + return $formats; +} diff --git a/includes/locale.inc b/includes/locale.inc index cc95ef7f4..8768be7dc 100644 --- a/includes/locale.inc +++ b/includes/locale.inc @@ -3216,3 +3216,87 @@ function country_get_list() { return $countries; } +/** + * Save locale specific date formats to the database. + * + * @param $langcode + * Language code, can be 2 characters, e.g. 'en' or 5 characters, e.g. + * 'en-CA'. + * @param $type + * Date format type, e.g. 'short', 'medium'. + * @param $format + * The date format string. + */ +function locale_date_format_save($langcode, $type, $format) { + $locale_format = array(); + $locale_format['language'] = $langcode; + $locale_format['type'] = $type; + $locale_format['format'] = $format; + + $is_existing = (bool) db_query_range('SELECT 1 FROM {date_format_locale} WHERE language = :langcode AND type = :type', 0, 1, array(':langcode' => $langcode, ':type' => $type))->fetchField(); + if ($is_existing) { + $keys = array('type', 'language'); + drupal_write_record('date_format_locale', $locale_format, $keys); + } + else { + drupal_write_record('date_format_locale', $locale_format); + } +} + +/** + * Select locale date format details from database. + * + * @param $languages + * An array of language codes. + * @return + * An array of date formats. + */ +function locale_get_localized_date_format($languages) { + $formats = array(); + + // Get list of different format types. + $format_types = system_get_date_types(); + $short_default = variable_get('date_format_short', 'm/d/Y - H:i'); + + // Loop through each language until we find one with some date formats + // configured. + foreach ($languages as $language) { + $date_formats = system_date_format_locale($language); + if (!empty($date_formats)) { + // We have locale-specific date formats, so check for their types. If + // we're missing a type, use the default setting instead. + foreach ($format_types as $type => $type_info) { + // If format exists for this language, use it. + if (!empty($date_formats[$type])) { + $formats['date_format_' . $type] = $date_formats[$type]; + } + // Otherwise get default variable setting. If this is not set, default + // to the short format. + else { + $formats['date_format_' . $type] = variable_get('date_format_' . $type, $short_default); + } + } + + // Return on the first match. + return $formats; + } + } + + // No locale specific formats found, so use defaults. + $system_types = array('short', 'medium', 'long'); + // Handle system types separately as they have defaults if no variable exists. + $formats['date_format_short'] = $short_default; + $formats['date_format_medium'] = variable_get('date_format_medium', 'D, m/d/Y - H:i'); + $formats['date_format_long'] = variable_get('date_format_long', 'l, F j, Y - H:i'); + + // For non-system types, get the default setting, otherwise use the short + // format. + foreach ($format_types as $type => $type_info) { + if (!in_array($type, $system_types)) { + $formats['date_format_' . $type] = variable_get('date_format_' . $type, $short_default); + } + } + + return $formats; +} + |