i18n_string.i18n.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @file
  4. * Implementation of i18n hooks
  5. */
  6. /**
  7. * Implements hook_i18n_string_objects().
  8. *
  9. * Automate object list for object types that have a 'table' property
  10. */
  11. function i18n_string_i18n_string_objects($type) {
  12. if ($function = i18n_object_info($type, 'list callback')) {
  13. return call_user_func($function);
  14. }
  15. elseif ($table = i18n_string_object_info($type, 'table')) {
  16. $query = db_select($table, 's')->fields('s');
  17. return $query->execute()->fetchAll();
  18. }
  19. }
  20. /**
  21. * Implements hook_i18n_string_list().
  22. *
  23. * Collect all strings from objects of this group.
  24. */
  25. function i18n_string_i18n_string_list($group) {
  26. $strings = array();
  27. // It may be for one group or all groups
  28. $groups = $group == 'all' ? array_keys(i18n_string_group_info()) : array($group);
  29. foreach ($groups as $group) {
  30. // Compile strings for object types for this group
  31. foreach (i18n_string_group_object_types($group) as $type) {
  32. $type_strings = i18n_string_object_type_string_list($type);
  33. if ($type_strings && !empty($type_strings[$group])) {
  34. $strings[$group] = isset($strings[$group]) ? i18n_string_array_merge($strings[$group], $type_strings[$group]) : $type_strings[$group];
  35. }
  36. }
  37. }
  38. return $strings;
  39. }
  40. /**
  41. * Get object types for text group
  42. */
  43. function i18n_string_group_object_types($group) {
  44. $types = array();
  45. foreach (i18n_object_info() as $type => $type_info) {
  46. if (!empty($type_info['string translation']) && $type_info['string translation']['textgroup'] == $group) {
  47. $types[] = $type;
  48. }
  49. }
  50. return $types;
  51. }
  52. /**
  53. * Get object string list that are in this text group.
  54. *
  55. * @param $type
  56. * Object type
  57. */
  58. function i18n_string_object_type_string_list($type) {
  59. $strings = array();
  60. if ($objects = module_invoke_all('i18n_string_objects', $type)) {
  61. foreach ($objects as $object) {
  62. if ($object_strings = i18n_object($type, $object)->get_properties()) {
  63. $strings = i18n_string_array_merge($strings, $object_strings);
  64. }
  65. }
  66. }
  67. return $strings;
  68. }
  69. /**
  70. * Merges multiple arrays, recursively, and returns the merged array.
  71. *
  72. * This function is not equivalent to PHP's array_merge_recursive(),
  73. * as this version leaves integer keys intact.
  74. *
  75. * @see drupal_array_merge_deep(), @see array_merge_recursive()
  76. *
  77. * @param ...
  78. * Arrays to merge.
  79. * @return
  80. * The merged array.
  81. */
  82. function i18n_string_array_merge() {
  83. $arrays = func_get_args();
  84. $result = array();
  85. foreach ($arrays as $array) {
  86. foreach ($array as $key => $value) {
  87. // Recurse when both values are arrays.
  88. if (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
  89. $result[$key] = i18n_string_array_merge($result[$key], $value);
  90. }
  91. // Otherwise, use the latter value, overriding any previous value.
  92. else {
  93. $result[$key] = $value;
  94. }
  95. }
  96. }
  97. return $result;
  98. }