diff options
Diffstat (limited to 'modules/locale/locale.module')
-rw-r--r-- | modules/locale/locale.module | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/modules/locale/locale.module b/modules/locale/locale.module index 18dc0a5a1..a24198182 100644 --- a/modules/locale/locale.module +++ b/modules/locale/locale.module @@ -224,10 +224,67 @@ function locale_menu() { 'file path' => 'includes', ); + // Localize date formats. + $items['admin/config/regional/date-time/locale'] = array( + 'title' => 'Localize', + 'description' => 'Configure date formats for each locale', + 'page callback' => 'locale_date_format_language_overview_page', + 'access arguments' => array('administer site configuration'), + 'type' => MENU_LOCAL_TASK, + 'weight' => -8, + 'file' => 'locale.inc', + 'file path' => 'includes', + ); + $items['admin/config/regional/date-time/locale/%/edit'] = array( + 'title' => 'Localize date formats', + 'description' => 'Configure date formats for each locale', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('locale_date_format_form', 5), + 'access arguments' => array('administer site configuration'), + 'type' => MENU_CALLBACK, + 'file' => 'locale.inc', + 'file path' => 'includes', + ); + $items['admin/config/regional/date-time/locale/%/reset'] = array( + 'title' => 'Reset date formats', + 'description' => 'Reset localized date formats to global defaults', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('locale_date_format_reset_form', 5), + 'access arguments' => array('administer site configuration'), + 'type' => MENU_CALLBACK, + 'file' => 'locale.inc', + 'file path' => 'includes', + ); + return $items; } /** + * Implements hook_init(). + * + * Initialize date formats according to the user's current locale. + */ +function locale_init() { + global $conf, $language; + include_once DRUPAL_ROOT . '/includes/locale.inc'; + + // For each date type (e.g. long, short), get the localized date format + // for the user's current language and override the default setting for it + // in $conf. This should happen on all pages except the date and time formats + // settings page, where we want to display the site default and not the + // localized version. + if (strpos($_GET['q'], 'admin/config/regional/date-time/formats') !== 0) { + $languages = array($language->language); + + // Setup appropriate date formats for this locale. + $formats = locale_get_localized_date_format($languages); + foreach ($formats as $format_type => $format) { + $conf[$format_type] = $format; + } + } +} + +/** * Wrapper function to be able to set callbacks in locale.inc */ function locale_inc_callback() { @@ -376,6 +433,9 @@ function locale_theme() { 'locale_translation_filters' => array( 'arguments' => array('form' => array()), ), + 'locale_date_format_form' => array( + 'arguments' => array('form' => array()), + ), ); } @@ -798,3 +858,160 @@ function theme_locale_translation_filters($variables) { $output .= '<div id="locale-translation-buttons">' . drupal_render($form['buttons']) . '</div>'; return $output; } + +/** + * Theme locale date format form. + * + * @ingroup themeable + */ +function theme_locale_date_format_form($variables) { + $form = $variables['form']; + $header = array( + t('Date type'), + t('Format'), + ); + + foreach (element_children($form['date_formats']) as $key) { + $row = array(); + $row[] = $form['date_formats'][$key]['#title']; + unset($form['date_formats'][$key]['#title']); + $row[] = array('data' => drupal_render($form['date_formats'][$key])); + $rows[] = $row; + } + + $output = drupal_render($form['language']); + $output .= theme('table', array('header' => $header, 'rows' => $rows)); + $output .= drupal_render_children($form); + + return $output; +} + +/** + * Display edit date format links for each language. + */ +function locale_date_format_language_overview_page() { + $header = array( + t('Language'), + array('data' => t('Operations'), 'colspan' => '2'), + ); + + // Get list of languages. + $languages = locale_language_list('native'); + + foreach ($languages as $langcode => $info) { + $row = array(); + $row[] = $languages[$langcode]; + $row[] = l(t('edit'), 'admin/config/regional/date-time/locale/' . $langcode . '/edit'); + $row[] = l(t('reset'), 'admin/config/regional/date-time/locale/' . $langcode . '/reset'); + $rows[] = $row; + } + + return theme('table', array('header' => $header, 'rows' => $rows)); +} + +/** + * Provide date localization configuration options to users. + */ +function locale_date_format_form($form, &$form_state, $langcode) { + $languages = locale_language_list('native'); + $language_name = $languages[$langcode]; + + // Display the current language name. + $form['language'] = array( + '#type' => 'item', + '#title' => t('Language'), + '#markup' => check_plain($language_name), + '#weight' => -10, + ); + $form['langcode'] = array( + '#type' => 'value', + '#value' => $langcode, + ); + + // Get list of date format types. + $types = system_get_date_types(); + + // Get list of available formats. + $formats = system_get_date_formats(); + $choices = array(); + foreach ($formats as $type => $list) { + foreach ($list as $f => $format) { + $choices[$f] = format_date(REQUEST_TIME, 'custom', $f); + } + } + + // Get configured formats for each language. + $locale_formats = system_date_format_locale($langcode); + // Display a form field for each format type. + foreach ($types as $type => $type_info) { + if (!empty($locale_formats) && in_array($type, array_keys($locale_formats))) { + $default = $locale_formats[$type]; + } + else { + $default = variable_get('date_format_' . $type, array_shift(array_keys($formats))); + } + + // Show date format select list. + $form['date_formats']['date_format_' . $type] = array( + '#type' => 'select', + '#title' => check_plain($type_info['title']), + '#attributes' => array('class' => array('date-format')), + '#default_value' => (isset($choices[$default]) ? $default : 'custom'), + '#options' => $choices, + ); + } + + $form['buttons']['submit'] = array( + '#type' => 'submit', + '#value' => t('Save configuration'), + ); + + return $form; +} + +/** + * Submit handler for configuring localized date formats on the locale_date_format_form. + */ +function locale_date_format_form_submit($form, &$form_state) { + include_once DRUPAL_ROOT . '/includes/locale.inc'; + $langcode = $form_state['values']['langcode']; + + // Get list of date format types. + $types = system_get_date_types(); + foreach ($types as $type => $type_info) { + $format = $form_state['values']['date_format_' . $type]; + if ($format == 'custom') { + $format = $form_state['values']['date_format_' . $type . '_custom']; + } + locale_date_format_save($langcode, $type, $format); + } + drupal_set_message(t('Configuration saved.')); + $form_state['redirect'] = 'admin/config/regional/date-time/locale'; +} + +/** + * Reset locale specific date formats to the global defaults. + * + * @param $langcode + * Language code, e.g. 'en'. + */ +function locale_date_format_reset_form($form, &$form_state, $langcode) { + $form['langcode'] = array('#type' => 'value', '#value' => $langcode); + $languages = language_list(); + return confirm_form($form, + t('Are you sure you want to reset the date formats for %language to the global defaults?', array('%language' => $languages[$langcode]->name)), + 'admin/config/regional/date-time/locale', + t('Resetting will remove all localized date formats for this language. This action cannot be undone.'), + t('Reset'), t('Cancel')); +} + +/** + * Reset date formats for a specific language to global defaults. + */ +function locale_date_format_reset_form_submit($form, &$form_state) { + db_delete('date_format_locale') + ->condition('language', $form_state['values']['langcode']) + ->execute(); + $form_state['redirect'] = 'admin/config/regional/date-time/locale'; +} + |