1<?php
2
3/**
4 * @file
5 * Administration functions for locale.module.
6 */
7
8/**
9 * @defgroup locale-language-administration Language administration interface
10 * @{
11 * Administration interface for languages.
12 *
13 * These functions provide the user interface to show, add, edit and
14 * delete languages as well as providing options for language negotiation.
15 */
16
17/**
18 * User interface for the language overview screen.
19 */
20function locale_languages_overview_form() {
21  drupal_static_reset('language');
22  $languages = language_list('language');
23
24  $options = array();
25  $form['weight'] = array('#tree' => TRUE);
26  foreach ($languages as $langcode => $language) {
27
28    $options[$langcode] = '';
29    if ($language->enabled) {
30      $enabled[] = $langcode;
31    }
32    $form['weight'][$langcode] = array(
33      '#type' => 'weight',
34      '#title' => t('Weight for @title', array('@title' => $language->name)),
35      '#title_display' => 'invisible',
36      '#default_value' => $language->weight,
37      '#attributes' => array('class' => array('language-order-weight')),
38    );
39    $form['name'][$langcode] = array('#markup' => check_plain($language->name));
40    $form['native'][$langcode] = array('#markup' => check_plain($language->native));
41    $form['direction'][$langcode] = array('#markup' => ($language->direction == LANGUAGE_RTL ? t('Right to left') : t('Left to right')));
42  }
43  $form['enabled'] = array(
44    '#type' => 'checkboxes',
45    '#title' => t('Enabled languages'),
46    '#title_display' => 'invisible',
47    '#options' => $options,
48    '#default_value' => $enabled,
49  );
50  $form['site_default'] = array(
51    '#type' => 'radios',
52    '#title' => t('Default language'),
53    '#title_display' => 'invisible',
54    '#options' => $options,
55    '#default_value' => language_default('language'),
56  );
57  $form['actions'] = array('#type' => 'actions');
58  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
59  $form['#theme'] = 'locale_languages_overview_form';
60
61  return $form;
62}
63
64/**
65 * Returns HTML for the language overview form.
66 *
67 * @param $variables
68 *   An associative array containing:
69 *   - form: A render element representing the form.
70 *
71 * @ingroup themeable
72 */
73function theme_locale_languages_overview_form($variables) {
74  $form = $variables['form'];
75  $default = language_default();
76  foreach ($form['name'] as $key => $element) {
77    // Do not take form control structures.
78    if (is_array($element) && element_child($key)) {
79      // Disable checkbox for the default language, because it cannot be disabled.
80      if ($key == $default->language) {
81        $form['enabled'][$key]['#attributes']['disabled'] = 'disabled';
82      }
83
84      // Add invisible labels for the checkboxes and radio buttons in the table
85      // for accessibility. These changes are only required and valid when the
86      // form is themed as a table, so it would be wrong to perform them in the
87      // form constructor.
88      $title = drupal_render($form['name'][$key]);
89      $form['enabled'][$key]['#title'] = t('Enable !title', array('!title' => $title));
90      $form['enabled'][$key]['#title_display'] = 'invisible';
91      $form['site_default'][$key]['#title'] = t('Set !title as default', array('!title' => $title));
92      $form['site_default'][$key]['#title_display'] = 'invisible';
93      $rows[] = array(
94        'data' => array(
95          '<strong>' . $title . '</strong>',
96          drupal_render($form['native'][$key]),
97          check_plain($key),
98          drupal_render($form['direction'][$key]),
99          array('data' => drupal_render($form['enabled'][$key]), 'align' => 'center'),
100          drupal_render($form['site_default'][$key]),
101          drupal_render($form['weight'][$key]),
102          l(t('edit'), 'admin/config/regional/language/edit/' . $key) . (($key != 'en' && $key != $default->language) ? ' ' . l(t('delete'), 'admin/config/regional/language/delete/' . $key) : '')
103        ),
104        'class' => array('draggable'),
105      );
106    }
107  }
108  $header = array(array('data' => t('English name')), array('data' => t('Native name')), array('data' => t('Code')), array('data' => t('Direction')), array('data' => t('Enabled')), array('data' => t('Default')), array('data' => t('Weight')), array('data' => t('Operations')));
109  $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'language-order')));
110  $output .= drupal_render_children($form);
111
112  drupal_add_tabledrag('language-order', 'order', 'sibling', 'language-order-weight');
113
114  return $output;
115}
116
117/**
118 * Process language overview form submissions, updating existing languages.
119 */
120function locale_languages_overview_form_submit($form, &$form_state) {
121  $languages = language_list();
122  $default = language_default();
123  $url_prefixes = variable_get('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX) == LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX;
124  $enabled_count = 0;
125
126  foreach ($languages as $langcode => $language) {
127    if ($form_state['values']['site_default'] == $langcode || $default->language == $langcode) {
128      // Automatically enable the default language and the language
129      // which was default previously (because we will not get the
130      // value from that disabled checkbox).
131      $form_state['values']['enabled'][$langcode] = 1;
132    }
133
134    // If language URL prefixes are enabled we must clear language domains and
135    // assign a valid prefix to each non-default language.
136    if ($url_prefixes) {
137      $language->domain = '';
138      if (empty($language->prefix) && $form_state['values']['site_default'] != $langcode) {
139        $language->prefix = $langcode;
140      }
141    }
142
143    if ($form_state['values']['enabled'][$langcode]) {
144      $enabled_count++;
145      $language->enabled = 1;
146    }
147    else {
148      $language->enabled = 0;
149    }
150
151    $language->weight = $form_state['values']['weight'][$langcode];
152
153    db_update('languages')
154      ->fields(array(
155        'enabled' => $language->enabled,
156        'weight' => $language->weight,
157        'prefix' => $language->prefix,
158        'domain' => $language->domain,
159      ))
160      ->condition('language', $langcode)
161      ->execute();
162
163    $languages[$langcode] = $language;
164  }
165
166  variable_set('language_default', $languages[$form_state['values']['site_default']]);
167  variable_set('language_count', $enabled_count);
168  drupal_set_message(t('Configuration saved.'));
169
170  // Changing the language settings impacts the interface.
171  cache_clear_all('*', 'cache_page', TRUE);
172  module_invoke_all('multilingual_settings_changed');
173
174  $form_state['redirect'] = 'admin/config/regional/language';
175  return;
176}
177
178/**
179 * User interface for the language addition screen.
180 */
181function locale_languages_add_screen() {
182  $build['predefined'] = drupal_get_form('locale_languages_predefined_form');
183  $build['custom'] = drupal_get_form('locale_languages_custom_form');
184  return $build;
185}
186
187/**
188 * Predefined language setup form.
189 */
190function locale_languages_predefined_form($form) {
191  $predefined = _locale_prepare_predefined_list();
192  $form['language list'] = array('#type' => 'fieldset',
193    '#title' => t('Predefined language'),
194    '#collapsible' => TRUE,
195  );
196  $form['language list']['langcode'] = array('#type' => 'select',
197    '#title' => t('Language name'),
198    '#default_value' => key($predefined),
199    '#options' => $predefined,
200    '#description' => t('Use the <em>Custom language</em> section below if your desired language does not appear in this list.'),
201  );
202  $form['language list']['actions'] = array('#type' => 'actions');
203  $form['language list']['actions']['submit'] = array('#type' => 'submit', '#value' => t('Add language'));
204  return $form;
205}
206
207/**
208 * Custom language addition form.
209 */
210function locale_languages_custom_form($form) {
211  $form['custom language'] = array('#type' => 'fieldset',
212    '#title' => t('Custom language'),
213    '#collapsible' => TRUE,
214    '#collapsed' => TRUE,
215  );
216  _locale_languages_common_controls($form['custom language']);
217  $form['custom language']['actions'] = array('#type' => 'actions');
218  $form['custom language']['actions']['submit'] = array(
219    '#type' => 'submit',
220    '#value' => t('Add custom language')
221  );
222  // Reuse the validation and submit functions of the predefined language setup form.
223  $form['#submit'][] = 'locale_languages_predefined_form_submit';
224  $form['#validate'][] = 'locale_languages_predefined_form_validate';
225  return $form;
226}
227
228/**
229 * Editing screen for a particular language.
230 *
231 * @param $langcode
232 *   Language code of the language to edit.
233 */
234function locale_languages_edit_form($form, &$form_state, $langcode) {
235  if ($language = db_query("SELECT * FROM {languages} WHERE language = :language", array(':language' => $langcode))->fetchObject()) {
236    _locale_languages_common_controls($form, $language);
237    $form['actions'] = array('#type' => 'actions');
238    $form['actions']['submit'] = array(
239      '#type' => 'submit',
240      '#value' => t('Save language')
241    );
242    $form['#submit'][] = 'locale_languages_edit_form_submit';
243    $form['#validate'][] = 'locale_languages_edit_form_validate';
244    return $form;
245  }
246  else {
247    drupal_not_found();
248    drupal_exit();
249  }
250}
251
252/**
253 * Common elements of the language addition and editing form.
254 *
255 * @param $form
256 *   A parent form item (or empty array) to add items below.
257 * @param $language
258 *   Language object to edit.
259 */
260function _locale_languages_common_controls(&$form, $language = NULL) {
261  if (!is_object($language)) {
262    $language = new stdClass();
263  }
264  if (isset($language->language)) {
265    $form['langcode_view'] = array(
266      '#type' => 'item',
267      '#title' => t('Language code'),
268      '#markup' => $language->language
269    );
270    $form['langcode'] = array(
271      '#type' => 'value',
272      '#value' => $language->language
273    );
274  }
275  else {
276    $form['langcode'] = array('#type' => 'textfield',
277      '#title' => t('Language code'),
278      '#maxlength' => 12,
279      '#required' => TRUE,
280      '#default_value' => @$language->language,
281      '#disabled' => (isset($language->language)),
282      '#description' => t('<a href="@rfc4646">RFC 4646</a> compliant language identifier. Language codes typically use a country code, and optionally, a script or regional variant name. <em>Examples: "en", "en-US" and "zh-Hant".</em>', array('@rfc4646' => 'http://www.ietf.org/rfc/rfc4646.txt')),
283    );
284  }
285  $form['name'] = array('#type' => 'textfield',
286    '#title' => t('Language name in English'),
287    '#maxlength' => 64,
288    '#default_value' => @$language->name,
289    '#required' => TRUE,
290    '#description' => t('Name of the language in English. Will be available for translation in all languages.'),
291  );
292  $form['native'] = array('#type' => 'textfield',
293    '#title' => t('Native language name'),
294    '#maxlength' => 64,
295    '#default_value' => @$language->native,
296    '#required' => TRUE,
297    '#description' => t('Name of the language in the language being added.'),
298  );
299  $form['prefix'] = array('#type' => 'textfield',
300    '#title' => t('Path prefix language code'),
301    '#maxlength' => 64,
302    '#default_value' => @$language->prefix,
303    '#description' => t('Language code or other custom text to use as a path prefix for URL language detection, if your <em>Detection and selection</em> settings use URL path prefixes. For the default language, this value may be left blank. <strong>Modifying this value may break existing URLs. Use with caution in a production environment.</strong> Example: Specifying "deutsch" as the path prefix code for German results in URLs like "example.com/deutsch/contact".')
304  );
305  $form['domain'] = array('#type' => 'textfield',
306    '#title' => t('Language domain'),
307    '#maxlength' => 128,
308    '#default_value' => @$language->domain,
309    '#description' => t('The domain name to use for this language if URL domains are used for <em>Detection and selection</em>. Leave blank for the default language. <strong>Changing this value may break existing URLs.</strong> Example: Specifying "de.example.com" as language domain for German will result in an URL like "http://de.example.com/contact".'),
310  );
311  $form['direction'] = array('#type' => 'radios',
312    '#title' => t('Direction'),
313    '#required' => TRUE,
314    '#description' => t('Direction that text in this language is presented.'),
315    '#default_value' => @$language->direction,
316    '#options' => array(LANGUAGE_LTR => t('Left to right'), LANGUAGE_RTL => t('Right to left'))
317  );
318  return $form;
319}
320
321/**
322 * Validate the language addition form.
323 */
324function locale_languages_predefined_form_validate($form, &$form_state) {
325  $langcode = $form_state['values']['langcode'];
326
327  if (($duplicate = db_query("SELECT COUNT(*) FROM {languages} WHERE language = :language", array(':language' => $langcode))->fetchField()) != 0) {
328    form_set_error('langcode', t('The language %language (%code) already exists.', array('%language' => $form_state['values']['name'], '%code' => $langcode)));
329  }
330
331  if (!isset($form_state['values']['name'])) {
332    // Predefined language selection.
333    include_once DRUPAL_ROOT . '/includes/iso.inc';
334    $predefined = _locale_get_predefined_list();
335    if (!isset($predefined[$langcode])) {
336      form_set_error('langcode', t('Invalid language code.'));
337    }
338  }
339  else {
340    // Reuse the editing form validation routine if we add a custom language.
341    locale_languages_edit_form_validate($form, $form_state);
342  }
343}
344
345/**
346 * Process the language addition form submission.
347 */
348function locale_languages_predefined_form_submit($form, &$form_state) {
349  $langcode = $form_state['values']['langcode'];
350  if (isset($form_state['values']['name'])) {
351    // Custom language form.
352    locale_add_language($langcode, $form_state['values']['name'], $form_state['values']['native'], $form_state['values']['direction'], $form_state['values']['domain'], $form_state['values']['prefix']);
353    drupal_set_message(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => t($form_state['values']['name']), '@locale-help' => url('admin/help/locale'))));
354  }
355  else {
356    // Predefined language selection.
357    include_once DRUPAL_ROOT . '/includes/iso.inc';
358    $predefined = _locale_get_predefined_list();
359    locale_add_language($langcode);
360    drupal_set_message(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => t($predefined[$langcode][0]), '@locale-help' => url('admin/help/locale'))));
361  }
362
363  // See if we have language files to import for the newly added
364  // language, collect and import them.
365  if ($batch = locale_batch_by_language($langcode, '_locale_batch_language_finished')) {
366    batch_set($batch);
367  }
368
369  $form_state['redirect'] = 'admin/config/regional/language';
370}
371
372/**
373 * Validate the language editing form. Reused for custom language addition too.
374 */
375function locale_languages_edit_form_validate($form, &$form_state) {
376  // Ensure sane field values for langcode, name, and native.
377  if (!isset($form['langcode_view']) && preg_match('@[^a-zA-Z_-]@', $form_state['values']['langcode'])) {
378    form_set_error('langcode', t('%field may only contain characters a-z, underscores, or hyphens.', array('%field' => $form['langcode']['#title'])));
379  }
380  if ($form_state['values']['name'] != check_plain($form_state['values']['name'])) {
381    form_set_error('name', t('%field cannot contain any markup.', array('%field' => $form['name']['#title'])));
382  }
383  if ($form_state['values']['native'] != check_plain($form_state['values']['native'])) {
384    form_set_error('native', t('%field cannot contain any markup.', array('%field' => $form['native']['#title'])));
385  }
386
387  if (!empty($form_state['values']['domain']) && !empty($form_state['values']['prefix'])) {
388    form_set_error('prefix', t('Domain and path prefix values should not be set at the same time.'));
389  }
390  if (!empty($form_state['values']['domain']) && $duplicate = db_query("SELECT language FROM {languages} WHERE domain = :domain AND language <> :language", array(':domain' => $form_state['values']['domain'], ':language' => $form_state['values']['langcode']))->fetchField()) {
391    form_set_error('domain', t('The domain (%domain) is already tied to a language (%language).', array('%domain' => $form_state['values']['domain'], '%language' => $duplicate)));
392  }
393  if (empty($form_state['values']['prefix']) && language_default('language') != $form_state['values']['langcode'] && empty($form_state['values']['domain'])) {
394    form_set_error('prefix', t('Only the default language can have both the domain and prefix empty.'));
395  }
396  if (!empty($form_state['values']['prefix']) && $duplicate = db_query("SELECT language FROM {languages} WHERE prefix = :prefix AND language <> :language", array(':prefix' => $form_state['values']['prefix'], ':language' => $form_state['values']['langcode']))->fetchField()) {
397    form_set_error('prefix', t('The prefix (%prefix) is already tied to a language (%language).', array('%prefix' => $form_state['values']['prefix'], '%language' => $duplicate)));
398  }
399}
400
401/**
402 * Process the language editing form submission.
403 */
404function locale_languages_edit_form_submit($form, &$form_state) {
405  db_update('languages')
406    ->fields(array(
407      'name' => $form_state['values']['name'],
408      'native' => $form_state['values']['native'],
409      'domain' => $form_state['values']['domain'],
410      'prefix' => $form_state['values']['prefix'],
411      'direction' => $form_state['values']['direction'],
412    ))
413    ->condition('language',  $form_state['values']['langcode'])
414    ->execute();
415  $default = language_default();
416  if ($default->language == $form_state['values']['langcode']) {
417    $properties = array('name', 'native', 'direction', 'enabled', 'plurals', 'formula', 'domain', 'prefix', 'weight');
418    foreach ($properties as $keyname) {
419      if (isset($form_state['values'][$keyname])) {
420        $default->$keyname = $form_state['values'][$keyname];
421      }
422    }
423    variable_set('language_default', $default);
424  }
425  $form_state['redirect'] = 'admin/config/regional/language';
426  return;
427}
428
429/**
430 * User interface for the language deletion confirmation screen.
431 */
432function locale_languages_delete_form($form, &$form_state, $langcode) {
433
434  // Do not allow deletion of English locale.
435  if ($langcode == 'en') {
436    drupal_set_message(t('The English language cannot be deleted.'));
437    drupal_goto('admin/config/regional/language');
438  }
439
440  if (language_default('language') == $langcode) {
441    drupal_set_message(t('The default language cannot be deleted.'));
442    drupal_goto('admin/config/regional/language');
443  }
444
445  // For other languages, warn user that data loss is ahead.
446  $languages = language_list();
447
448  if (!isset($languages[$langcode])) {
449    drupal_not_found();
450    drupal_exit();
451  }
452  else {
453    $form['langcode'] = array('#type' => 'value', '#value' => $langcode);
454    return confirm_form($form, t('Are you sure you want to delete the language %name?', array('%name' => t($languages[$langcode]->name))), 'admin/config/regional/language', t('Deleting a language will remove all interface translations associated with it, and posts in this language will be set to be language neutral. This action cannot be undone.'), t('Delete'), t('Cancel'));
455  }
456}
457
458/**
459 * Process language deletion submissions.
460 */
461function locale_languages_delete_form_submit($form, &$form_state) {
462  $languages = language_list();
463  if (isset($languages[$form_state['values']['langcode']])) {
464    // Remove translations first.
465    db_delete('locales_target')
466      ->condition('language', $form_state['values']['langcode'])
467      ->execute();
468    cache_clear_all('locale:' . $form_state['values']['langcode'], 'cache');
469    // With no translations, this removes existing JavaScript translations file.
470    _locale_rebuild_js($form_state['values']['langcode']);
471    // Remove the language.
472    db_delete('languages')
473      ->condition('language', $form_state['values']['langcode'])
474      ->execute();
475    db_update('node')
476      ->fields(array('language' => ''))
477      ->condition('language', $form_state['values']['langcode'])
478      ->execute();
479    if ($languages[$form_state['values']['langcode']]->enabled) {
480      variable_set('language_count', variable_get('language_count', 1) - 1);
481    }
482    module_invoke_all('multilingual_settings_changed');
483    $variables = array('%locale' => $languages[$form_state['values']['langcode']]->name);
484    drupal_set_message(t('The language %locale has been removed.', $variables));
485    watchdog('locale', 'The language %locale has been removed.', $variables);
486  }
487
488  // Changing the language settings impacts the interface:
489  cache_clear_all('*', 'cache_page', TRUE);
490
491  $form_state['redirect'] = 'admin/config/regional/language';
492  return;
493}
494
495/**
496 * Setting for language negotiation options
497 */
498function locale_languages_configure_form() {
499  include_once DRUPAL_ROOT . '/includes/language.inc';
500
501  $form = array(
502    '#submit' => array('locale_languages_configure_form_submit'),
503    '#theme' => 'locale_languages_configure_form',
504    '#language_types' => language_types_configurable(FALSE),
505    '#language_types_info' => language_types_info(),
506    '#language_providers' => language_negotiation_info(),
507  );
508
509  foreach ($form['#language_types'] as $type) {
510    _locale_languages_configure_form_language_table($form, $type);
511  }
512
513  $form['actions'] = array('#type' => 'actions');
514  $form['actions']['submit'] = array(
515    '#type' => 'submit',
516    '#value' => t('Save settings'),
517  );
518
519  return $form;
520}
521
522/**
523 * Helper function to build a language provider table.
524 */
525function _locale_languages_configure_form_language_table(&$form, $type) {
526  $info = $form['#language_types_info'][$type];
527
528  $table_form = array(
529    '#title' => t('@type language detection', array('@type' => $info['name'])),
530    '#tree' => TRUE,
531    '#description' => $info['description'],
532    '#language_providers' => array(),
533    '#show_operations' => FALSE,
534    'weight' => array('#tree' => TRUE),
535    'enabled' => array('#tree' => TRUE),
536  );
537
538  $language_providers = $form['#language_providers'];
539  $enabled_providers = variable_get("language_negotiation_$type", array());
540  $providers_weight = variable_get("locale_language_providers_weight_$type", array());
541
542  // Add missing data to the providers lists.
543  foreach ($language_providers as $id => $provider) {
544    if (!isset($providers_weight[$id])) {
545      $providers_weight[$id] = language_provider_weight($provider);
546    }
547  }
548
549  // Order providers list by weight.
550  asort($providers_weight);
551
552  foreach ($providers_weight as $id => $weight) {
553    // A language provider might be no more available if the defining module has
554    // been disabled after the last configuration saving.
555    if (!isset($language_providers[$id])) {
556      continue;
557    }
558
559    $enabled = isset($enabled_providers[$id]);
560    $provider = $language_providers[$id];
561
562    // List the provider only if the current type is defined in its 'types' key.
563    // If it is not defined default to all the configurable language types.
564    $types = array_flip(isset($provider['types']) ? $provider['types'] : $form['#language_types']);
565
566    if (isset($types[$type])) {
567      $table_form['#language_providers'][$id] = $provider;
568
569      $table_form['weight'][$id] = array(
570        '#type' => 'weight',
571        '#title' => t('Weight for @title', array('@title' => $provider['name'])),
572        '#title_display' => 'invisible',
573        '#default_value' => $weight,
574        '#attributes' => array('class' => array("language-provider-weight-$type")),
575      );
576
577      $table_form['title'][$id] = array('#markup' => check_plain($provider['name']));
578
579      $table_form['enabled'][$id] = array(
580        '#type' => 'checkbox',
581        '#title' => t('@title language provider', array('@title' => $provider['name'])),
582        '#title_display' => 'invisible',
583        '#default_value' => $enabled,
584      );
585      if ($id === LANGUAGE_NEGOTIATION_DEFAULT) {
586        $table_form['enabled'][$id]['#default_value'] = TRUE;
587        $table_form['enabled'][$id]['#attributes'] = array('disabled' => 'disabled');
588      }
589
590      $table_form['description'][$id] = array('#markup' => filter_xss_admin($provider['description']));
591
592      $config_op = array();
593      if (isset($provider['config'])) {
594        $config_op = array('#type' => 'link', '#title' => t('Configure'), '#href' => $provider['config']);
595        // If there is at least one operation enabled show the operation column.
596        $table_form['#show_operations'] = TRUE;
597      }
598      $table_form['operation'][$id] = $config_op;
599    }
600  }
601
602  $form[$type] = $table_form;
603}
604
605/**
606 * Returns HTML for a language configuration form.
607 *
608 * @param $variables
609 *   An associative array containing:
610 *   - form: A render element representing the form.
611 *
612 * @ingroup themeable
613 */
614function theme_locale_languages_configure_form($variables) {
615  $form = $variables['form'];
616  $output = '';
617
618  foreach ($form['#language_types'] as $type) {
619    $rows = array();
620    $info = $form['#language_types_info'][$type];
621    $title = '<label>' . $form[$type]['#title'] . '</label>';
622    $description = '<div class="description">' . $form[$type]['#description'] . '</div>';
623
624    foreach ($form[$type]['title'] as $id => $element) {
625      // Do not take form control structures.
626      if (is_array($element) && element_child($id)) {
627        $row = array(
628          'data' => array(
629            '<strong>' . drupal_render($form[$type]['title'][$id]) . '</strong>',
630            drupal_render($form[$type]['description'][$id]),
631            drupal_render($form[$type]['enabled'][$id]),
632            drupal_render($form[$type]['weight'][$id]),
633          ),
634          'class' => array('draggable'),
635        );
636        if ($form[$type]['#show_operations']) {
637          $row['data'][] = drupal_render($form[$type]['operation'][$id]);
638        }
639        $rows[] = $row;
640      }
641    }
642
643    $header = array(
644      array('data' => t('Detection method')),
645      array('data' => t('Description')),
646      array('data' => t('Enabled')),
647      array('data' => t('Weight')),
648    );
649
650    // If there is at least one operation enabled show the operation column.
651    if ($form[$type]['#show_operations']) {
652      $header[] = array('data' => t('Operations'));
653    }
654
655    $variables = array(
656      'header' => $header,
657      'rows' => $rows,
658      'attributes' => array('id' => "language-negotiation-providers-$type"),
659    );
660    $table  = theme('table', $variables);
661    $table .= drupal_render_children($form[$type]);
662
663    drupal_add_tabledrag("language-negotiation-providers-$type", 'order', 'sibling', "language-provider-weight-$type");
664
665    $output .= '<div class="form-item">' . $title . $description . $table . '</div>';
666  }
667
668  $output .= drupal_render_children($form);
669  return $output;
670}
671
672/**
673 * Submit handler for language negotiation settings.
674 */
675function locale_languages_configure_form_submit($form, &$form_state) {
676  $configurable_types = $form['#language_types'];
677
678  foreach ($configurable_types as $type) {
679    $negotiation = array();
680    $enabled_providers = $form_state['values'][$type]['enabled'];
681    $enabled_providers[LANGUAGE_NEGOTIATION_DEFAULT] = TRUE;
682    $providers_weight = $form_state['values'][$type]['weight'];
683
684    foreach ($providers_weight as $id => $weight) {
685      if ($enabled_providers[$id]) {
686        $provider = $form[$type]['#language_providers'][$id];
687        $provider['weight'] = $weight;
688        $negotiation[$id] = $provider;
689      }
690    }
691
692    language_negotiation_set($type, $negotiation);
693    variable_set("locale_language_providers_weight_$type", $providers_weight);
694  }
695
696  // Update non-configurable language types and the related language negotiation
697  // configuration.
698  language_types_set();
699
700  $form_state['redirect'] = 'admin/config/regional/language/configure';
701  drupal_set_message(t('Language negotiation configuration saved.'));
702}
703
704/**
705 * The URL language provider configuration form.
706 */
707function locale_language_providers_url_form($form, &$form_state) {
708  $form['locale_language_negotiation_url_part'] = array(
709    '#title' => t('Part of the URL that determines language'),
710    '#type' => 'radios',
711    '#options' => array(
712      LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX => t('Path prefix'),
713      LOCALE_LANGUAGE_NEGOTIATION_URL_DOMAIN => t('Domain'),
714    ),
715    '#default_value' => variable_get('locale_language_negotiation_url_part', LOCALE_LANGUAGE_NEGOTIATION_URL_PREFIX),
716    '#description' => t('<em>Path prefix</em>: URLs like http://example.com/de/contact set language to German (de). <em>Domain</em>: URLs like http://de.example.com/contact set the language to German. <strong>Warning: Changing this setting may break incoming URLs. Use with caution on a production site.</strong>'),
717  );
718
719  $form_state['redirect'] = 'admin/config/regional/language/configure';
720
721  return system_settings_form($form);
722}
723
724/**
725 * The URL language provider configuration form.
726 */
727function locale_language_providers_session_form($form, &$form_state) {
728  $form['locale_language_negotiation_session_param'] = array(
729    '#title' => t('Request/session parameter'),
730    '#type' => 'textfield',
731    '#default_value' => variable_get('locale_language_negotiation_session_param', 'language'),
732    '#description' => t('Name of the request/session parameter used to determine the desired language.'),
733  );
734
735  $form_state['redirect'] = 'admin/config/regional/language/configure';
736
737  return system_settings_form($form);
738}
739
740/**
741 * @} End of "locale-language-administration"
742 */
743
744/**
745 * @defgroup locale-translate-administration-screens Translation administration screens
746 * @{
747 * Screens for translation administration.
748 *
749 * These functions provide various screens as administration interface
750 * to import, export and view translations.
751 */
752
753/**
754 * Overview screen for translations.
755 */
756function locale_translate_overview_screen() {
757  drupal_static_reset('language_list');
758  $languages = language_list('language');
759  $groups = module_invoke_all('locale', 'groups');
760
761  // Build headers with all groups in order.
762  $headers = array_merge(array(t('Language')), array_values($groups));
763
764  // Collect summaries of all source strings in all groups.
765  $sums = db_query("SELECT COUNT(*) AS strings, textgroup FROM {locales_source} GROUP BY textgroup");
766  $groupsums = array();
767  foreach ($sums as $group) {
768    $groupsums[$group->textgroup] = $group->strings;
769  }
770
771  // Set up overview table with default values, ensuring common order for values.
772  $rows = array();
773  foreach ($languages as $langcode => $language) {
774    $rows[$langcode] = array('name' => ($langcode == 'en' ? t('English (built-in)') : t($language->name)));
775    foreach ($groups as $group => $name) {
776      $rows[$langcode][$group] = ($langcode == 'en' ? t('n/a') : '0/' . (isset($groupsums[$group]) ? $groupsums[$group] : 0) . ' (0%)');
777    }
778  }
779
780  // Languages with at least one record in the locale table.
781  $translations = db_query("SELECT COUNT(*) AS translation, t.language, s.textgroup FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid GROUP BY textgroup, language");
782  foreach ($translations as $data) {
783    $ratio = (!empty($groupsums[$data->textgroup]) && $data->translation > 0) ? round(($data->translation/$groupsums[$data->textgroup]) * 100.0, 2) : 0;
784    $rows[$data->language][$data->textgroup] = $data->translation . '/' . $groupsums[$data->textgroup] . " ($ratio%)";
785  }
786
787  return theme('table', array('header' => $headers, 'rows' => $rows));
788}
789
790/**
791 * String search screen.
792 */
793function locale_translate_seek_screen() {
794  // Add CSS.
795  drupal_add_css(drupal_get_path('module', 'locale') . '/locale.css');
796
797  $elements = drupal_get_form('locale_translation_filter_form');
798  $output = drupal_render($elements);
799  $output .= _locale_translate_seek();
800  return $output;
801}
802
803/**
804 * List locale translation filters that can be applied.
805 */
806function locale_translation_filters() {
807  $filters = array();
808
809  // Get all languages, except English
810  drupal_static_reset('language_list');
811  $languages = locale_language_list('name');
812  unset($languages['en']);
813
814  $filters['string'] = array(
815    'title' => t('String contains'),
816    'description' => t('Leave blank to show all strings. The search is case sensitive.'),
817  );
818
819  $filters['language'] = array(
820    'title' => t('Language'),
821    'options' => array_merge(array('all' => t('All languages'), 'en' => t('English (provided by Drupal)')), $languages),
822  );
823
824  $filters['translation'] = array(
825    'title' => t('Search in'),
826    'options' => array('all' => t('Both translated and untranslated strings'), 'translated' => t('Only translated strings'), 'untranslated' => t('Only untranslated strings')),
827  );
828
829  $groups = module_invoke_all('locale', 'groups');
830  $filters['group'] = array(
831    'title' => t('Limit search to'),
832    'options' => array_merge(array('all' => t('All text groups')), $groups),
833  );
834
835  return $filters;
836}
837
838/**
839 * Return form for locale translation filters.
840 *
841 * @ingroup forms
842 */
843function locale_translation_filter_form() {
844  $filters = locale_translation_filters();
845
846  $form['filters'] = array(
847    '#type' => 'fieldset',
848    '#title' => t('Filter translatable strings'),
849    '#collapsible' => TRUE,
850    '#collapsed' => FALSE,
851  );
852  foreach ($filters as $key => $filter) {
853    // Special case for 'string' filter.
854    if ($key == 'string') {
855      $form['filters']['status']['string'] = array(
856        '#type' => 'textfield',
857        '#title' => $filter['title'],
858        '#description' => $filter['description'],
859      );
860    }
861    else {
862      $form['filters']['status'][$key] = array(
863        '#title' => $filter['title'],
864        '#type' => 'select',
865        '#empty_value' => 'all',
866        '#empty_option' => $filter['options']['all'],
867        '#size' => 0,
868        '#options' => $filter['options'],
869      );
870    }
871    if (!empty($_SESSION['locale_translation_filter'][$key])) {
872      $form['filters']['status'][$key]['#default_value'] = $_SESSION['locale_translation_filter'][$key];
873    }
874  }
875
876  $form['filters']['actions'] = array(
877    '#type' => 'actions',
878    '#attributes' => array('class' => array('container-inline')),
879  );
880  $form['filters']['actions']['submit'] = array(
881    '#type' => 'submit',
882    '#value' => t('Filter'),
883  );
884  if (!empty($_SESSION['locale_translation_filter'])) {
885    $form['filters']['actions']['reset'] = array(
886      '#type' => 'submit',
887      '#value' => t('Reset')
888    );
889  }
890
891  return $form;
892}
893
894/**
895 * Validate result from locale translation filter form.
896 */
897function locale_translation_filter_form_validate($form, &$form_state) {
898  if ($form_state['values']['op'] == t('Filter') && empty($form_state['values']['language']) && empty($form_state['values']['group'])) {
899    form_set_error('type', t('You must select something to filter by.'));
900  }
901}
902
903/**
904 * Process result from locale translation filter form.
905 */
906function locale_translation_filter_form_submit($form, &$form_state) {
907  $op = $form_state['values']['op'];
908  $filters = locale_translation_filters();
909  switch ($op) {
910    case t('Filter'):
911      foreach ($filters as $name => $filter) {
912        if (isset($form_state['values'][$name])) {
913          $_SESSION['locale_translation_filter'][$name] = $form_state['values'][$name];
914        }
915      }
916      break;
917    case t('Reset'):
918      $_SESSION['locale_translation_filter'] = array();
919      break;
920  }
921
922  $form_state['redirect'] = 'admin/config/regional/translate/translate';
923}
924
925/**
926 * User interface for the translation import screen.
927 */
928function locale_translate_import_form($form) {
929  // Get all languages, except English
930  drupal_static_reset('language_list');
931  $names = locale_language_list('name');
932  unset($names['en']);
933
934  if (!count($names)) {
935    $languages = _locale_prepare_predefined_list();
936    $default = key($languages);
937  }
938  else {
939    $languages = array(
940      t('Already added languages') => $names,
941      t('Languages not yet added') => _locale_prepare_predefined_list()
942    );
943    $default = key($names);
944  }
945
946  $form['import'] = array('#type' => 'fieldset',
947    '#title' => t('Import translation'),
948  );
949  $form['import']['file'] = array('#type' => 'file',
950    '#title' => t('Language file'),
951    '#size' => 50,
952    '#description' => t('A Gettext Portable Object (<em>.po</em>) file.'),
953  );
954  $form['import']['langcode'] = array('#type' => 'select',
955    '#title' => t('Import into'),
956    '#options' => $languages,
957    '#default_value' => $default,
958    '#description' => t('Choose the language you want to add strings into. If you choose a language which is not yet set up, it will be added.'),
959  );
960  $form['import']['group'] = array('#type' => 'radios',
961    '#title' => t('Text group'),
962    '#default_value' => 'default',
963    '#options' => module_invoke_all('locale', 'groups'),
964    '#description' => t('Imported translations will be added to this text group.'),
965  );
966  $form['import']['mode'] = array('#type' => 'radios',
967    '#title' => t('Mode'),
968    '#default_value' => LOCALE_IMPORT_KEEP,
969    '#options' => array(
970      LOCALE_IMPORT_OVERWRITE => t('Strings in the uploaded file replace existing ones, new ones are added. The plural format is updated.'),
971      LOCALE_IMPORT_KEEP => t('Existing strings and the plural format are kept, only new strings are added.')
972    ),
973  );
974  $form['import']['submit'] = array('#type' => 'submit', '#value' => t('Import'));
975
976  return $form;
977}
978
979/**
980 * Process the locale import form submission.
981 */
982function locale_translate_import_form_submit($form, &$form_state) {
983  $validators = array('file_validate_extensions' => array('po'));
984  // Ensure we have the file uploaded
985  if ($file = file_save_upload('file', $validators)) {
986
987    // Add language, if not yet supported
988    drupal_static_reset('language_list');
989    $languages = language_list('language');
990    $langcode = $form_state['values']['langcode'];
991    if (!isset($languages[$langcode])) {
992      include_once DRUPAL_ROOT . '/includes/iso.inc';
993      $predefined = _locale_get_predefined_list();
994      locale_add_language($langcode);
995      drupal_set_message(t('The language %language has been created.', array('%language' => t($predefined[$langcode][0]))));
996    }
997
998    // Now import strings into the language
999    if ($return = _locale_import_po($file, $langcode, $form_state['values']['mode'], $form_state['values']['group']) == FALSE) {
1000      $variables = array('%filename' => $file->filename);
1001      drupal_set_message(t('The translation import of %filename failed.', $variables), 'error');
1002      watchdog('locale', 'The translation import of %filename failed.', $variables, WATCHDOG_ERROR);
1003    }
1004  }
1005  else {
1006    drupal_set_message(t('File to import not found.'), 'error');
1007    $form_state['redirect'] = 'admin/config/regional/translate/import';
1008    return;
1009  }
1010
1011  $form_state['redirect'] = 'admin/config/regional/translate';
1012  return;
1013}
1014
1015/**
1016 * User interface for the translation export screen.
1017 */
1018function locale_translate_export_screen() {
1019  // Get all languages, except English
1020  drupal_static_reset('language_list');
1021  $names = locale_language_list('name');
1022  unset($names['en']);
1023  $output = '';
1024  // Offer translation export if any language is set up.
1025  if (count($names)) {
1026    $elements = drupal_get_form('locale_translate_export_po_form', $names);
1027    $output = drupal_render($elements);
1028  }
1029  $elements = drupal_get_form('locale_translate_export_pot_form');
1030  $output .= drupal_render($elements);
1031  return $output;
1032}
1033
1034/**
1035 * Form to export PO files for the languages provided.
1036 *
1037 * @param $names
1038 *   An associate array with localized language names
1039 */
1040function locale_translate_export_po_form($form, &$form_state, $names) {
1041  $form['export_title'] = array('#type' => 'item',
1042    '#title' => t('Export translation'),
1043  );
1044  $form['langcode'] = array('#type' => 'select',
1045    '#title' => t('Language name'),
1046    '#options' => $names,
1047    '#description' => t('Select the language to export in Gettext Portable Object (<em>.po</em>) format.'),
1048  );
1049  $form['group'] = array('#type' => 'radios',
1050    '#title' => t('Text group'),
1051    '#default_value' => 'default',
1052    '#options' => module_invoke_all('locale', 'groups'),
1053  );
1054  $form['actions'] = array('#type' => 'actions');
1055  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Export'));
1056  return $form;
1057}
1058
1059/**
1060 * Translation template export form.
1061 */
1062function locale_translate_export_pot_form() {
1063  // Complete template export of the strings
1064  $form['export_title'] = array('#type' => 'item',
1065    '#title' => t('Export template'),
1066    '#description' => t('Generate a Gettext Portable Object Template (<em>.pot</em>) file with all strings from the Drupal locale database.'),
1067  );
1068  $form['group'] = array('#type' => 'radios',
1069    '#title' => t('Text group'),
1070    '#default_value' => 'default',
1071    '#options' => module_invoke_all('locale', 'groups'),
1072  );
1073  $form['actions'] = array('#type' => 'actions');
1074  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Export'));
1075  // Reuse PO export submission callback.
1076  $form['#submit'][] = 'locale_translate_export_po_form_submit';
1077  return $form;
1078}
1079
1080/**
1081 * Process a translation (or template) export form submission.
1082 */
1083function locale_translate_export_po_form_submit($form, &$form_state) {
1084  // If template is required, language code is not given.
1085  $language = NULL;
1086  if (isset($form_state['values']['langcode'])) {
1087    $languages = language_list();
1088    $language = $languages[$form_state['values']['langcode']];
1089  }
1090  _locale_export_po($language, _locale_export_po_generate($language, _locale_export_get_strings($language, $form_state['values']['group'])));
1091}
1092/**
1093 * @} End of "locale-translate-administration-screens"
1094 */
1095
1096/**
1097 * @defgroup locale-translate-edit-delete Translation editing/deletion interface
1098 * @{
1099 * Edit and delete translation strings.
1100 *
1101 * These functions provide the user interface to edit and delete
1102 * translation strings.
1103 */
1104
1105/**
1106 * User interface for string editing.
1107 */
1108function locale_translate_edit_form($form, &$form_state, $lid) {
1109  // Fetch source string, if possible.
1110  $source = db_query('SELECT source, context, textgroup, location FROM {locales_source} WHERE lid = :lid', array(':lid' => $lid))->fetchObject();
1111  if (!$source) {
1112    drupal_set_message(t('String not found.'), 'error');
1113    drupal_goto('admin/config/regional/translate/translate');
1114  }
1115
1116  // Add original text to the top and some values for form altering.
1117  $form['original'] = array(
1118    '#type'  => 'item',
1119    '#title' => t('Original text'),
1120    '#markup' => check_plain(wordwrap($source->source, 0)),
1121  );
1122  if (!empty($source->context)) {
1123    $form['context'] = array(
1124      '#type' => 'item',
1125      '#title' => t('Context'),
1126      '#markup' => check_plain($source->context),
1127    );
1128  }
1129  $form['lid'] = array(
1130    '#type'  => 'value',
1131    '#value' => $lid
1132  );
1133  $form['textgroup'] = array(
1134    '#type'  => 'value',
1135    '#value' => $source->textgroup,
1136  );
1137  $form['location'] = array(
1138    '#type'  => 'value',
1139    '#value' => $source->location
1140  );
1141
1142  // Include both translated and not yet translated target languages in the
1143  // list. The source language is English for built-in strings and the default
1144  // language for other strings.
1145  $languages = language_list();
1146  $default = language_default();
1147  $omit = $source->textgroup == 'default' ? 'en' : $default->language;
1148  unset($languages[($omit)]);
1149  $form['translations'] = array('#tree' => TRUE);
1150  // Approximate the number of rows to use in the default textarea.
1151  $rows = min(ceil(str_word_count($source->source) / 12), 10);
1152  foreach ($languages as $langcode => $language) {
1153    $form['translations'][$langcode] = array(
1154      '#type' => 'textarea',
1155      '#title' => t($language->name),
1156      '#rows' => $rows,
1157      '#default_value' => '',
1158    );
1159  }
1160
1161  // Fetch translations and fill in default values in the form.
1162  $result = db_query("SELECT DISTINCT translation, language FROM {locales_target} WHERE lid = :lid AND language <> :omit", array(':lid' => $lid, ':omit' => $omit));
1163  foreach ($result as $translation) {
1164    $form['translations'][$translation->language]['#default_value'] = $translation->translation;
1165  }
1166
1167  $form['actions'] = array('#type' => 'actions');
1168  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save translations'));
1169  return $form;
1170}
1171
1172/**
1173 * Validate string editing form submissions.
1174 */
1175function locale_translate_edit_form_validate($form, &$form_state) {
1176  // Locale string check is needed for default textgroup only.
1177  $safe_check_needed = $form_state['values']['textgroup'] == 'default';
1178  foreach ($form_state['values']['translations'] as $key => $value) {
1179    if ($safe_check_needed && !locale_string_is_safe($value)) {
1180      form_set_error('translations', t('The submitted string contains disallowed HTML: %string', array('%string' => $value)));
1181      watchdog('locale', 'Attempted submission of a translation string with disallowed HTML: %string', array('%string' => $value), WATCHDOG_WARNING);
1182    }
1183  }
1184}
1185
1186/**
1187 * Process string editing form submissions.
1188 *
1189 * Saves all translations of one string submitted from a form.
1190 */
1191function locale_translate_edit_form_submit($form, &$form_state) {
1192  $lid = $form_state['values']['lid'];
1193  foreach ($form_state['values']['translations'] as $key => $value) {
1194    $translation = db_query("SELECT translation FROM {locales_target} WHERE lid = :lid AND language = :language", array(':lid' => $lid, ':language' => $key))->fetchField();
1195    if (!empty($value)) {
1196      // Only update or insert if we have a value to use.
1197      if (is_string($translation)) {
1198        db_update('locales_target')
1199          ->fields(array(
1200            'translation' => $value,
1201          ))
1202          ->condition('lid', $lid)
1203          ->condition('language', $key)
1204          ->execute();
1205      }
1206      else {
1207        db_insert('locales_target')
1208          ->fields(array(
1209            'lid' => $lid,
1210            'translation' => $value,
1211            'language' => $key,
1212          ))
1213          ->execute();
1214      }
1215    }
1216    elseif (!empty($translation)) {
1217      // Empty translation entered: remove existing entry from database.
1218      db_delete('locales_target')
1219        ->condition('lid', $lid)
1220        ->condition('language', $key)
1221        ->execute();
1222    }
1223
1224    // Force JavaScript translation file recreation for this language.
1225    _locale_invalidate_js($key);
1226  }
1227
1228  drupal_set_message(t('The string has been saved.'));
1229
1230  // Clear locale cache.
1231  _locale_invalidate_js();
1232  cache_clear_all('locale:', 'cache', TRUE);
1233
1234  $form_state['redirect'] = 'admin/config/regional/translate/translate';
1235  return;
1236}
1237
1238/**
1239 * String deletion confirmation page.
1240 */
1241function locale_translate_delete_page($lid) {
1242  if ($source = db_query('SELECT lid, source FROM {locales_source} WHERE lid = :lid', array(':lid' => $lid))->fetchObject()) {
1243    return drupal_get_form('locale_translate_delete_form', $source);
1244  }
1245  return MENU_NOT_FOUND;
1246}
1247
1248/**
1249 * User interface for the string deletion confirmation screen.
1250 */
1251function locale_translate_delete_form($form, &$form_state, $source) {
1252  $form['lid'] = array('#type' => 'value', '#value' => $source->lid);
1253  return confirm_form($form, t('Are you sure you want to delete the string "%source"?', array('%source' => $source->source)), 'admin/config/regional/translate/translate', t('Deleting the string will remove all translations of this string in all languages. This action cannot be undone.'), t('Delete'), t('Cancel'));
1254}
1255
1256/**
1257 * Process string deletion submissions.
1258 */
1259function locale_translate_delete_form_submit($form, &$form_state) {
1260  db_delete('locales_source')
1261    ->condition('lid', $form_state['values']['lid'])
1262    ->execute();
1263  db_delete('locales_target')
1264    ->condition('lid', $form_state['values']['lid'])
1265    ->execute();
1266  // Force JavaScript translation file recreation for all languages.
1267  _locale_invalidate_js();
1268  cache_clear_all('locale:', 'cache', TRUE);
1269  drupal_set_message(t('The string has been removed.'));
1270  $form_state['redirect'] = 'admin/config/regional/translate/translate';
1271}
1272/**
1273 * @} End of "locale-translate-edit-delete"
1274 */
1275
1276/**
1277 * Returns HTML for a locale date format form.
1278 *
1279 * @param $variables
1280 *   An associative array containing:
1281 *   - form: A render element representing the form.
1282 *
1283 * @ingroup themeable
1284 */
1285function theme_locale_date_format_form($variables) {
1286  $form = $variables['form'];
1287  $header = array(
1288    t('Date type'),
1289    t('Format'),
1290  );
1291
1292  foreach (element_children($form['date_formats']) as $key) {
1293    $row = array();
1294    $row[] = $form['date_formats'][$key]['#title'];
1295    unset($form['date_formats'][$key]['#title']);
1296    $row[] = array('data' => drupal_render($form['date_formats'][$key]));
1297    $rows[] = $row;
1298  }
1299
1300  $output = drupal_render($form['language']);
1301  $output .= theme('table', array('header' => $header, 'rows' => $rows));
1302  $output .= drupal_render_children($form);
1303
1304  return $output;
1305}
1306
1307/**
1308 * Display edit date format links for each language.
1309 */
1310function locale_date_format_language_overview_page() {
1311  $header = array(
1312    t('Language'),
1313    array('data' => t('Operations'), 'colspan' => '2'),
1314  );
1315
1316  // Get list of languages.
1317  $languages = locale_language_list('native');
1318
1319  foreach ($languages as $langcode => $info) {
1320    $row = array();
1321    $row[] = $languages[$langcode];
1322    $row[] = l(t('edit'), 'admin/config/regional/date-time/locale/' . $langcode . '/edit');
1323    $row[] = l(t('reset'), 'admin/config/regional/date-time/locale/' . $langcode . '/reset');
1324    $rows[] = $row;
1325  }
1326
1327  return theme('table', array('header' => $header, 'rows' => $rows));
1328}
1329
1330/**
1331 * Provide date localization configuration options to users.
1332 */
1333function locale_date_format_form($form, &$form_state, $langcode) {
1334  $languages = locale_language_list('native');
1335  $language_name = $languages[$langcode];
1336
1337  // Display the current language name.
1338  $form['language'] = array(
1339    '#type' => 'item',
1340    '#title' => t('Language'),
1341    '#markup' => check_plain($language_name),
1342    '#weight' => -10,
1343  );
1344  $form['langcode'] = array(
1345    '#type' => 'value',
1346    '#value' => $langcode,
1347  );
1348
1349  // Get list of date format types.
1350  $types = system_get_date_types();
1351
1352  // Get list of available formats.
1353  $formats = system_get_date_formats();
1354  $choices = array();
1355  foreach ($formats as $type => $list) {
1356    foreach ($list as $f => $format) {
1357      $choices[$f] = format_date(REQUEST_TIME, 'custom', $f);
1358    }
1359  }
1360  reset($formats);
1361
1362  // Get configured formats for each language.
1363  $locale_formats = system_date_format_locale($langcode);
1364  // Display a form field for each format type.
1365  foreach ($types as $type => $type_info) {
1366    if (!empty($locale_formats) && in_array($type, array_keys($locale_formats))) {
1367      $default = $locale_formats[$type];
1368    }
1369    else {
1370      $default = variable_get('date_format_' . $type, key($formats));
1371    }
1372
1373    // Show date format select list.
1374    $form['date_formats']['date_format_' . $type] = array(
1375      '#type' => 'select',
1376      '#title' => check_plain($type_info['title']),
1377      '#attributes' => array('class' => array('date-format')),
1378      '#default_value' => (isset($choices[$default]) ? $default : 'custom'),
1379      '#options' => $choices,
1380    );
1381  }
1382
1383  $form['actions'] = array('#type' => 'actions');
1384  $form['actions']['submit'] = array(
1385    '#type' => 'submit',
1386    '#value' => t('Save configuration'),
1387  );
1388
1389  return $form;
1390}
1391
1392/**
1393 * Submit handler for configuring localized date formats on the locale_date_format_form.
1394 */
1395function locale_date_format_form_submit($form, &$form_state) {
1396  include_once DRUPAL_ROOT . '/includes/locale.inc';
1397  $langcode = $form_state['values']['langcode'];
1398
1399  // Get list of date format types.
1400  $types = system_get_date_types();
1401  foreach ($types as $type => $type_info) {
1402    $format = $form_state['values']['date_format_' . $type];
1403    if ($format == 'custom') {
1404      $format = $form_state['values']['date_format_' . $type . '_custom'];
1405    }
1406    locale_date_format_save($langcode, $type, $format);
1407  }
1408  drupal_set_message(t('Configuration saved.'));
1409  $form_state['redirect'] = 'admin/config/regional/date-time/locale';
1410}
1411
1412/**
1413 * Reset locale specific date formats to the global defaults.
1414 *
1415 * @param $langcode
1416 *   Language code, e.g. 'en'.
1417 */
1418function locale_date_format_reset_form($form, &$form_state, $langcode) {
1419  $form['langcode'] = array('#type' => 'value', '#value' => $langcode);
1420  $languages = language_list();
1421  return confirm_form($form,
1422    t('Are you sure you want to reset the date formats for %language to the global defaults?', array('%language' => $languages[$langcode]->name)),
1423    'admin/config/regional/date-time/locale',
1424    t('Resetting will remove all localized date formats for this language. This action cannot be undone.'),
1425    t('Reset'), t('Cancel'));
1426}
1427
1428/**
1429 * Reset date formats for a specific language to global defaults.
1430 */
1431function locale_date_format_reset_form_submit($form, &$form_state) {
1432  db_delete('date_format_locale')
1433    ->condition('language', $form_state['values']['langcode'])
1434    ->execute();
1435  $form_state['redirect'] = 'admin/config/regional/date-time/locale';
1436}
1437