locale.admin.inc 52 KB

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