locale.admin.inc 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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)));
  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)));
  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 both translated and not yet translated target languages in the
  1020. // list. The source language is English for built-in strings and the default
  1021. // language for other strings.
  1022. $languages = language_list();
  1023. $default = language_default();
  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 (is_string($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. return MENU_NOT_FOUND;
  1114. }
  1115. /**
  1116. * User interface for the string deletion confirmation screen.
  1117. */
  1118. function locale_translate_delete_form($form, &$form_state, $source) {
  1119. $form['lid'] = array('#type' => 'value', '#value' => $source->lid);
  1120. 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'));
  1121. }
  1122. /**
  1123. * Process string deletion submissions.
  1124. */
  1125. function locale_translate_delete_form_submit($form, &$form_state) {
  1126. db_delete('locales_source')
  1127. ->condition('lid', $form_state['values']['lid'])
  1128. ->execute();
  1129. db_delete('locales_target')
  1130. ->condition('lid', $form_state['values']['lid'])
  1131. ->execute();
  1132. // Force JavaScript translation file recreation for all languages.
  1133. _locale_invalidate_js();
  1134. cache_clear_all('locale:', 'cache', TRUE);
  1135. drupal_set_message(t('The string has been removed.'));
  1136. $form_state['redirect'] = 'admin/config/regional/translate/translate';
  1137. }
  1138. /**
  1139. * @} End of "locale-translate-edit-delete"
  1140. */
  1141. /**
  1142. * Returns HTML for a locale date format form.
  1143. *
  1144. * @param $variables
  1145. * An associative array containing:
  1146. * - form: A render element representing the form.
  1147. *
  1148. * @ingroup themeable
  1149. */
  1150. function theme_locale_date_format_form($variables) {
  1151. $form = $variables['form'];
  1152. $header = array(
  1153. t('Date type'),
  1154. t('Format'),
  1155. );
  1156. foreach (element_children($form['date_formats']) as $key) {
  1157. $row = array();
  1158. $row[] = $form['date_formats'][$key]['#title'];
  1159. unset($form['date_formats'][$key]['#title']);
  1160. $row[] = array('data' => drupal_render($form['date_formats'][$key]));
  1161. $rows[] = $row;
  1162. }
  1163. $output = drupal_render($form['language']);
  1164. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  1165. $output .= drupal_render_children($form);
  1166. return $output;
  1167. }
  1168. /**
  1169. * Display edit date format links for each language.
  1170. */
  1171. function locale_date_format_language_overview_page() {
  1172. $header = array(
  1173. t('Language'),
  1174. array('data' => t('Operations'), 'colspan' => '2'),
  1175. );
  1176. // Get list of languages.
  1177. $languages = locale_language_list('native');
  1178. foreach ($languages as $langcode => $info) {
  1179. $row = array();
  1180. $row[] = $languages[$langcode];
  1181. $row[] = l(t('edit'), 'admin/config/regional/date-time/locale/' . $langcode . '/edit');
  1182. $row[] = l(t('reset'), 'admin/config/regional/date-time/locale/' . $langcode . '/reset');
  1183. $rows[] = $row;
  1184. }
  1185. return theme('table', array('header' => $header, 'rows' => $rows));
  1186. }
  1187. /**
  1188. * Provide date localization configuration options to users.
  1189. */
  1190. function locale_date_format_form($form, &$form_state, $langcode) {
  1191. $languages = locale_language_list('native');
  1192. $language_name = $languages[$langcode];
  1193. // Display the current language name.
  1194. $form['language'] = array(
  1195. '#type' => 'item',
  1196. '#title' => t('Language'),
  1197. '#markup' => check_plain($language_name),
  1198. '#weight' => -10,
  1199. );
  1200. $form['langcode'] = array(
  1201. '#type' => 'value',
  1202. '#value' => $langcode,
  1203. );
  1204. // Get list of date format types.
  1205. $types = system_get_date_types();
  1206. // Get list of available formats.
  1207. $formats = system_get_date_formats();
  1208. $choices = array();
  1209. foreach ($formats as $type => $list) {
  1210. foreach ($list as $f => $format) {
  1211. $choices[$f] = format_date(REQUEST_TIME, 'custom', $f);
  1212. }
  1213. }
  1214. reset($formats);
  1215. // Get configured formats for each language.
  1216. $locale_formats = system_date_format_locale($langcode);
  1217. // Display a form field for each format type.
  1218. foreach ($types as $type => $type_info) {
  1219. if (!empty($locale_formats) && in_array($type, array_keys($locale_formats))) {
  1220. $default = $locale_formats[$type];
  1221. }
  1222. else {
  1223. $default = variable_get('date_format_' . $type, key($formats));
  1224. }
  1225. // Show date format select list.
  1226. $form['date_formats']['date_format_' . $type] = array(
  1227. '#type' => 'select',
  1228. '#title' => check_plain($type_info['title']),
  1229. '#attributes' => array('class' => array('date-format')),
  1230. '#default_value' => (isset($choices[$default]) ? $default : 'custom'),
  1231. '#options' => $choices,
  1232. );
  1233. }
  1234. $form['actions'] = array('#type' => 'actions');
  1235. $form['actions']['submit'] = array(
  1236. '#type' => 'submit',
  1237. '#value' => t('Save configuration'),
  1238. );
  1239. return $form;
  1240. }
  1241. /**
  1242. * Submit handler for configuring localized date formats on the locale_date_format_form.
  1243. */
  1244. function locale_date_format_form_submit($form, &$form_state) {
  1245. include_once DRUPAL_ROOT . '/includes/locale.inc';
  1246. $langcode = $form_state['values']['langcode'];
  1247. // Get list of date format types.
  1248. $types = system_get_date_types();
  1249. foreach ($types as $type => $type_info) {
  1250. $format = $form_state['values']['date_format_' . $type];
  1251. if ($format == 'custom') {
  1252. $format = $form_state['values']['date_format_' . $type . '_custom'];
  1253. }
  1254. locale_date_format_save($langcode, $type, $format);
  1255. }
  1256. drupal_set_message(t('Configuration saved.'));
  1257. $form_state['redirect'] = 'admin/config/regional/date-time/locale';
  1258. }
  1259. /**
  1260. * Reset locale specific date formats to the global defaults.
  1261. *
  1262. * @param $langcode
  1263. * Language code, e.g. 'en'.
  1264. */
  1265. function locale_date_format_reset_form($form, &$form_state, $langcode) {
  1266. $form['langcode'] = array('#type' => 'value', '#value' => $langcode);
  1267. $languages = language_list();
  1268. return confirm_form($form,
  1269. t('Are you sure you want to reset the date formats for %language to the global defaults?', array('%language' => $languages[$langcode]->name)),
  1270. 'admin/config/regional/date-time/locale',
  1271. t('Resetting will remove all localized date formats for this language. This action cannot be undone.'),
  1272. t('Reset'), t('Cancel'));
  1273. }
  1274. /**
  1275. * Reset date formats for a specific language to global defaults.
  1276. */
  1277. function locale_date_format_reset_form_submit($form, &$form_state) {
  1278. db_delete('date_format_locale')
  1279. ->condition('language', $form_state['values']['langcode'])
  1280. ->execute();
  1281. $form_state['redirect'] = 'admin/config/regional/date-time/locale';
  1282. }