Commit 142778af authored by webchick's avatar webchick

Issue #1635134 by hairqles, floretan, webflo: Move date format translation...

Issue #1635134 by hairqles, floretan, webflo: Move date format translation functions from locale to system.
parent 47055769
<?php
/**
* @file
* Administration functions for locale.module.
*/
/**
* Returns HTML for a locale date format form.
*
* @param $variables
* An associative array containing:
* - form: A render element representing the 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'),
);
$languages = language_list();
foreach ($languages as $langcode => $language) {
$row = array();
$row[] = $language->name;
$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) {
// Display the current language name.
$form['language'] = array(
'#type' => 'item',
'#title' => t('Language'),
'#markup' => language_load($langcode)->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);
}
}
reset($formats);
// 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, key($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['actions'] = array('#type' => 'actions');
$form['actions']['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) {
$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);
return confirm_form($form,
t('Are you sure you want to reset the date formats for %language to the global defaults?', array('%language' => language_load($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';
}
......@@ -142,60 +142,9 @@ function locale_menu() {
'file' => 'locale.bulk.inc',
);
// 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.admin.inc',
);
$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'),
'file' => 'locale.admin.inc',
);
$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'),
'file' => 'locale.admin.inc',
);
return $items;
}
/**
* Implements hook_init().
*
* Initialize date formats according to the user's current locale.
*/
function locale_init() {
global $conf;
$language_interface = drupal_container()->get(LANGUAGE_TYPE_INTERFACE);
// 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(current_path(), 'admin/config/regional/date-time/formats') !== 0) {
$languages = array($language_interface->langcode);
// Setup appropriate date formats for this locale.
$formats = locale_get_localized_date_format($languages);
foreach ($formats as $format_type => $format) {
$conf[$format_type] = $format;
}
}
}
/**
* Implements hook_permission().
*/
......@@ -213,9 +162,6 @@ function locale_permission() {
*/
function locale_theme() {
return array(
'locale_date_format_form' => array(
'render element' => 'form',
),
'locale_translate_edit_form_strings' => array(
'render element' => 'form',
'file' => 'locale.pages.inc',
......@@ -929,88 +875,3 @@ function _locale_rebuild_js($langcode = NULL) {
return TRUE;
}
}
/**
* 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;
}
......@@ -2,28 +2,28 @@
/**
* @file
* Definition of Drupal\locale\Tests\LocaleDateFormatsTest.
* Definition of Drupal\system\Tests\System\DateFormatsLanguageTest.
*/
namespace Drupal\locale\Tests;
namespace Drupal\system\Tests\System;
use Drupal\simpletest\WebTestBase;
/**
* Functional tests for localizing date formats.
*/
class LocaleDateFormatsTest extends WebTestBase {
class DateFormatsLanguageTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'Localize date formats',
'description' => 'Tests for the localization of date formats.',
'group' => 'Locale',
'group' => 'System',
);
}
function setUp() {
parent::setUp(array('node', 'locale'));
parent::setUp(array('node', 'language'));
// Create Article node type.
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
......
......@@ -3163,3 +3163,185 @@ function system_actions_remove_orphans() {
actions_synchronize(TRUE);
drupal_goto('admin/config/system/actions/manage');
}
/**
* Display edit date format links for each language.
*/
function system_date_format_language_overview_page() {
$header = array(
t('Language'),
array('data' => t('Operations'), 'colspan' => '2'),
);
$languages = language_list();
foreach ($languages as $langcode => $language) {
$row = array();
$row[] = $language->name;
$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 system_date_format_localize_form($form, &$form_state, $langcode) {
// Display the current language name.
$form['language'] = array(
'#type' => 'item',
'#title' => t('Language'),
'#markup' => language_load($langcode)->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);
}
}
reset($formats);
// 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, key($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['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
return $form;
}
/**
* Submit handler for configuring localized date formats on the locale_date_format_form.
*/
function system_date_format_localize_form_submit($form, &$form_state) {
$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'];
}
system_date_format_localize_form_save($langcode, $type, $format);
}
drupal_set_message(t('Configuration saved.'));
$form_state['redirect'] = 'admin/config/regional/date-time/locale';
}
/**
* Returns HTML for a locale date format form.
*
* @param $variables
* An associative array containing:
* - form: A render element representing the form.
*
* @ingroup themeable
*/
function theme_system_date_format_localize_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;
}
/**
* Reset locale specific date formats to the global defaults.
*
* @param $langcode
* Language code, e.g. 'en'.
*/
function system_date_format_localize_reset_form($form, &$form_state, $langcode) {
$form['langcode'] = array('#type' => 'value', '#value' => $langcode);
return confirm_form($form,
t('Are you sure you want to reset the date formats for %language to the global defaults?', array('%language' => language_load($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 system_date_format_localize_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';
}
/**
* 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 system_date_format_localize_form_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);
}
}
......@@ -3586,7 +3586,7 @@ function hook_date_format_types_alter(&$types) {
* used with the associated date type -- a user has to choose a format for each
* date type in the administrative interface. There is one exception: locale
* initialization chooses a locale-specific format for the three core-provided
* types (see locale_get_localized_date_format() for details). If your module
* types (see system_get_localized_date_format() for details). If your module
* needs to ensure that a date type it defines has a format associated with it,
* call @code variable_set('date_format_' . $type, $format); @endcode
* where $type is the machine-readable name defined in hook_date_format_types(),
......
......@@ -199,6 +199,9 @@ function system_theme() {
'render element' => 'form',
'file' => 'system.admin.inc',
),
'system_date_format_localize_form' => array(
'render element' => 'form',
),
));
}
......@@ -1110,6 +1113,36 @@ function system_menu() {
'type' => MENU_CALLBACK,
'file' => 'system.admin.inc',
);
// Localize date formats.
if (module_exists('language')) {
$items['admin/config/regional/date-time/locale'] = array(
'title' => 'Localize',
'description' => 'Configure date formats for each locale',
'page callback' => 'system_date_format_language_overview_page',
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK,
'weight' => -8,
'file' => 'system.admin.inc',
);
$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('system_date_format_localize_form', 5),
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
);
$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('system_date_format_localize_reset_form', 5),
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
);
}
return $items;
}
......@@ -1967,6 +2000,24 @@ function system_filetransfer_info() {
* Implements hook_init().
*/
function system_init() {
global $conf;
$language_interface = drupal_container()->get(LANGUAGE_TYPE_INTERFACE);
// 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(current_path(), 'admin/config/regional/date-time/formats') !== 0) {
$languages = array($language_interface->langcode);
// Setup appropriate date formats for this locale.
$formats = system_get_localized_date_format($languages);
foreach ($formats as $format_type => $format) {
$conf[$format_type] = $format;
}
}
$path = drupal_get_path('module', 'system');
// Add the CSS for this module. These aren't in system.info, because they
// need to be in the CSS_SYSTEM group rather than the CSS_DEFAULT group.
......@@ -2002,6 +2053,64 @@ function system_init() {
system_add_module_assets();
}
/**
* Select locale date format details from database.
*
* @param $languages
* An array of language codes.
*
* @return
* An array of date formats.
*/
function system_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