Skip navigation
Help

locale.admin.inc

  1. drupal
    1. 7 drupal/modules/locale/locale.admin.inc

Administration functions for locale.module.

Functions & methods

NameDescription
locale_date_format_formProvide date localization configuration options to users.
locale_date_format_form_submitSubmit handler for configuring localized date formats on the locale_date_format_form.
locale_date_format_language_overview_pageDisplay edit date format links for each language.
locale_date_format_reset_formReset locale specific date formats to the global defaults.
locale_date_format_reset_form_submitReset date formats for a specific language to global defaults.
locale_languages_add_screenUser interface for the language addition screen.
locale_languages_configure_formSetting for language negotiation options
locale_languages_configure_form_submitSubmit handler for language negotiation settings.
locale_languages_custom_formCustom language addition form.
locale_languages_delete_formUser interface for the language deletion confirmation screen.
locale_languages_delete_form_submitProcess language deletion submissions.
locale_languages_edit_formEditing screen for a particular language.
locale_languages_edit_form_submitProcess the language editing form submission.
locale_languages_edit_form_validateValidate the language editing form. Reused for custom language addition too.
locale_languages_overview_formUser interface for the language overview screen.
locale_languages_overview_form_submitProcess language overview form submissions, updating existing languages.
locale_languages_predefined_formPredefined language setup form.
locale_languages_predefined_form_submitProcess the language addition form submission.
locale_languages_predefined_form_validateValidate the language addition form.
locale_language_providers_session_formThe URL language provider configuration form.
locale_language_providers_url_formThe URL language provider configuration form.
locale_translate_delete_formUser interface for the string deletion confirmation screen.
locale_translate_delete_form_submitProcess string deletion submissions.
locale_translate_delete_pageString deletion confirmation page.
locale_translate_edit_formUser interface for string editing.
locale_translate_edit_form_submitProcess string editing form submissions.
locale_translate_edit_form_validateValidate string editing form submissions.
locale_translate_export_pot_formTranslation template export form.
locale_translate_export_po_formForm to export PO files for the languages provided.
locale_translate_export_po_form_submitProcess a translation (or template) export form submission.
locale_translate_export_screenUser interface for the translation export screen.
locale_translate_import_formUser interface for the translation import screen.
locale_translate_import_form_submitProcess the locale import form submission.
locale_translate_overview_screenOverview screen for translations.
locale_translate_seek_screenString search screen.
locale_translation_filtersList locale translation filters that can be applied.
locale_translation_filter_formReturn form for locale translation filters.
locale_translation_filter_form_submitProcess result from locale translation filter form.
locale_translation_filter_form_validateValidate result from locale translation filter form.
theme_locale_date_format_formReturns HTML for a locale date format form.
theme_locale_languages_configure_formReturns HTML for a language configuration form.
theme_locale_languages_overview_formReturns HTML for the language overview form.
_locale_languages_common_controlsCommon elements of the language addition and editing form.
_locale_languages_configure_form_language_tableHelper function to build a language provider table.

File

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