select.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @file
  4. * Webform localizations for select component.
  5. */
  6. /**
  7. * Translate a single option from component.
  8. *
  9. * @param array $component
  10. * The select component
  11. * @param string $option
  12. * Untranslated option string.
  13. *
  14. * @return string
  15. * The translated option string, if found.
  16. */
  17. function webform_localization_translate_select_option($component, $option) {
  18. // Find the source for data value and translate it.
  19. $item_key_lookup = _webform_localization_string_to_key($component['extra']['items']);
  20. foreach ($component['extra']['translated_strings'] as $name) {
  21. $name_list = explode(':', $name);
  22. // Translate options.
  23. if (strpos($name_list[3], '-') !== FALSE) {
  24. list (, $key) = explode('-', $name_list[3]);
  25. if (isset($item_key_lookup[$key]) && $option == $item_key_lookup[$key]) {
  26. return i18n_string($name, $option);
  27. }
  28. }
  29. }
  30. return $option;
  31. }
  32. /**
  33. * Implements _webform_localization_csv_header_component().
  34. */
  35. function _webform_localization_csv_header_select($header, $component) {
  36. if (!isset($component['extra']['translated_strings']) || !is_array($component['extra']['translated_strings'])) {
  37. return $header;
  38. }
  39. // Each component has own methods and tricks to add different items to header
  40. // rows. Attempt to translate whatever we can.
  41. foreach ($component['extra']['translated_strings'] as $name) {
  42. $name_list = explode(':', $name);
  43. // Translate header from #title property, this is rather common scenario.
  44. if ($name_list[3] == '#title' && $component['name'] == $header[2][0]) {
  45. $header[2] = i18n_string($name, $component['name']);
  46. break;
  47. }
  48. // Title could be found from position [1][0] and in this case the select
  49. // options are on row 2.
  50. if ($name_list[3] == '#title' && $component['name'] == $header[1][0]) {
  51. $header[1] = i18n_string($name, $component['name']);
  52. foreach ($header[2] as $i => $option) {
  53. $header[2][$i] = webform_localization_translate_select_option($component, $option);
  54. }
  55. break;
  56. }
  57. }
  58. return $header;
  59. }
  60. /**
  61. * Implements _webform_localization_csv_data_component().
  62. */
  63. function _webform_localization_csv_data_select($data, $component, $submission) {
  64. // If data is an array then answers are being marked as X:es and there is no
  65. // need to translate these.
  66. if (is_array($data)) {
  67. return $data;
  68. }
  69. if (!isset($component['extra']['translated_strings']) || !is_array($component['extra']['translated_strings'])) {
  70. return $data;
  71. }
  72. return webform_localization_translate_select_option($component, $data);
  73. }
  74. /**
  75. * Implements _webform_localization_analysis_data_component().
  76. */
  77. function _webform_localization_analysis_data_select($data, $node, $component) {
  78. if (!isset($component['extra']['translated_strings']) || !is_array($component['extra']['translated_strings'])) {
  79. return $data;
  80. }
  81. $item_key_lookup = _webform_localization_string_to_key($component['extra']['items']);
  82. foreach ($component['extra']['translated_strings'] as $name) {
  83. $name_list = explode(':', $name);
  84. // Translate options.
  85. if (strpos($name_list[3], '-') !== FALSE) {
  86. list (, $key) = explode('-', $name_list[3]);
  87. if (isset($item_key_lookup[$key])) {
  88. foreach ($data['table_rows'] as $index => $row) {
  89. if ($row[0] == $item_key_lookup[$key]) {
  90. $data['table_rows'][$index][0] = i18n_string($name, $row[0]);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. return $data;
  97. }